SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
Lesson 24 of 30

CSS Navigation Bar

4 min read · Free · No login needed

Almost every website has a navigation bar — the row of links at the top that takes you to Home, Courses, About, or Contact. Without it, a visitor would be lost on the very first page. A navigation bar is just an HTML list of links, styled with CSS until it looks like a proper menu instead of a bullet list.

Diagram — see how it fits together
Diagram: CSS Navigation Bar

The trick is simple: take a <ul>, remove its bullets and spacing, lay its items out in a row, and style the links inside them as blocks with padding and a background colour. Once that base is in place, you can add hover effects, mark the current page, and even build dropdown menus.

Turning a list into a bar

Start with a <ul> inside a <nav> element. Remove the bullets with list-style-type: none and the default spacing with margin: 0 and padding: 0. Then give the <ul> a dark background colour and make it a flex container with display: flex — the list items line up in a row automatically.

Syntax — a horizontal bar

ul.nav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: #333;
  display: flex;
}
ul.nav a {
  display: block;
  color: white;
  padding: 14px 20px;
  text-decoration: none;
}

Each link is display: block, so the whole padded area is clickable — not just the letters. That matters on phones, where a finger is much wider than a mouse pointer.

Hover and the active page

A link should react when the mouse passes over it. The :hover pseudo-class changes its background colour, which tells the visitor "you can click here". The page the visitor is currently on gets a special class, usually called active, with a different colour — often green — so they always know where they are.

ul.nav a:hover {
  background-color: #111;
}
ul.nav a.active {
  background-color: #04aa6d;
}
Note: The active class is not automatic — you add class="active" to the link of the current page in your HTML. Each page of your site marks its own link as active.

Dropdown menus

A dropdown is a small menu that appears when you hover over one link, like "Courses" opening to show HTML, CSS, and Python. It works with two ideas: the parent <li> is position: relative, and the submenu is position: absolute and hidden with display: none. A :hover rule on the parent flips it to display: block, and the submenu drops down underneath.

.dropdown-content {
  display: none;
  position: absolute;
  background-color: white;
  min-width: 160px;
}
.dropdown:hover .dropdown-content {
  display: block;
}
Tip: Use display: flex on the <ul> instead of floating the <li> elements. Floats need a clearfix and are easy to break; flex lines the items up in a row with one line of CSS.

Common mistakes

  • Forgetting list-style-type: none — the bullets stay and the bar looks like a plain list.
  • Forgetting margin: 0 and padding: 0 on the <ul> — the browser's default spacing pushes the bar away from the edges.
  • Styling the <li> instead of the <a> — only the letters become clickable, and the padding area does nothing.
  • Positioning the dropdown without position: relative on its parent — the submenu appears in a random place on the page instead of under its link.
  • Making the hover colour the same as the normal colour — the visitor gets no feedback that the link can be clicked.

Remember

  • A nav bar is a <ul> with bullets and spacing removed, laid out in a row.
  • Make links display: block with padding, so the whole area is clickable.
  • :hover changes the link's background; .active marks the current page.
  • A dropdown hides with display: none and appears on :hover.
  • The dropdown's parent needs position: relative and the menu needs position: absolute.

Example explained

  • ul.nav — the whole menu: bullets and default spacing removed, a dark background, and display: flex so the items sit in a row.
  • ul.nav a — every link is a block with white text and wide padding, so the full coloured area is clickable.
  • ul.nav a:hover — when the mouse passes over a link, its background turns darker, showing it can be clicked.
  • ul.nav a.active — the Home link carries the active class, so it is green and the visitor knows which page they are on.
  • .dropdown-content — the Courses submenu is hidden with display: none and position: absolute; .dropdown:hover .dropdown-content shows it when the mouse hovers over Courses.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Chai Shop Navigation</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; }
ul.nav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-color: #333;
  display: flex;
}
ul.nav li { position: relative; }
ul.nav a {
  display: block;
  color: white;
  padding: 14px 20px;
  text-decoration: none;
}
ul.nav a:hover { background-color: #111; }
ul.nav a.active { background-color: #04aa6d; }
.dropdown-content {
  display: none;
  position: absolute;
  background-color: white;
  min-width: 160px;
  box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}
.dropdown-content a { color: black; padding: 12px 16px; }
.dropdown-content a:hover { background-color: #f1f1f1; color: black; }
.dropdown:hover .dropdown-content { display: block; }
main { padding: 20px; }
h1 { color: #333; }
</style>
</head>
<body>

<ul class="nav">
<li><a class="active" href="#">Home</a></li>
<li class="dropdown"><a href="#">Courses</a>
  <div class="dropdown-content">
  <a href="#">HTML</a>
  <a href="#">CSS</a>
  <a href="#">Python</a>
  </div>
</li>
<li><a href="#">Menu</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>

<main>
<h1>Welcome to Kamal Chai Shop</h1>
<p>Hover over <strong>Courses</strong> in the bar above to see the dropdown menu open.</p>
<p>The green link is the page you are on right now.</p>
</main>

</body>
</html>

Output

Output — how it looks in the browser
Output of CSS Navigation Bar

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 30 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

AI Prompt Engineering Webinar 2026
View
Advanced Cyber Security Industrial Training
₹5,000
View
Website Design & Development Workshop 2026
₹299
View
Browse all courses
CSS — Lessons
1. CSS Introduction 2. CSS Syntax 3. CSS Selectors 4. CSS How To 5. CSS Colors 6. CSS Backgrounds 7. CSS Borders 8. CSS Margins 9. CSS Padding 10. CSS Height and Width 11. CSS Box Model 12. CSS Text 13. CSS Fonts 14. CSS Links 15. CSS Lists 16. CSS Tables 17. CSS Display 18. CSS Position 19. CSS Overflow 20. CSS Float 21. CSS Pseudo-classes 22. CSS Pseudo-elements 23. CSS Forms 24. CSS Navigation Bar 25. CSS Website Layout 26. CSS Flexbox 27. CSS Grid 28. CSS Responsive 29. CSS Transitions and Animations 30. CSS Variables
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🛡️ 7-Day Money-Back Guarantee
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Download karne ke liye login karo. Login ke baad yahi page pe rahoge!

Naya ho? Register karo →