#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Navigation Bar

A navigation bar is the strip of links that lets visitors move between the main sections of a site. In HTML it is simply a list of links inside a <nav> element; CSS turns that plain vertical list into a horizontal or vertical menu with hover states, spacing and a highlight for the current page.

Starting with meaningful markup

A list is the right structure because a menu is a group of related items. Screen readers announce how many links the list holds, and the <nav> element marks the region as navigation so users can jump straight to it.

<nav class="site-nav" aria-label="Main">
  <ul>
    <li><a href="/" aria-current="page">Home</a></li>
    <li><a href="/tutorials">Tutorials</a></li>
    <li><a href="/blog">Blog</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>

Turning the list into a horizontal bar

Flexbox is the simplest way to lay items out in a row. Remove the default bullets and spacing, then style the links as blocks so the whole padded area is clickable, not just the text.

.site-nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  background: #1f2937;
}

.site-nav a {
  display: block;
  padding: 14px 18px;
  color: #f9fafb;
  text-decoration: none;
}

.site-nav a:hover,
.site-nav a:focus-visible {
  background: #374151;
}

.site-nav a[aria-current="page"] {
  background: #2563eb;
}

/* push the last item to the right edge */
.site-nav li:last-child {
  margin-left: auto;
}

Older tutorials float each <li> left or set it to display: inline-block. Both still work, but floats need a clearfix and inline-block leaves small gaps from the whitespace between tags. Flexbox avoids both problems and makes alignment easy.

Vertical menus and sticky bars

For a sidebar menu, leave the list as a column (drop display: flex or use flex-direction: column) and give the <nav> a fixed width. Links stacked as blocks already fill the width, so hover backgrounds span the whole row.

To keep a top bar visible while scrolling, add position: sticky; top: 0; to the <nav>. Unlike position: fixed, sticky keeps the bar in the normal flow, so content below does not slide underneath it. Give it a z-index so other positioned elements do not cover it.

Making it work on small screens

Four short links fit on a phone, but seven or eight will not. A few options, from simplest to most involved:

  • Allow wrapping with flex-wrap: wrap, as in the example, so items flow onto a second line.
  • Let the list scroll sideways inside the bar with overflow-x: auto, keeping the page itself from scrolling.
  • Use a media query to hide the list behind a menu button, which needs a little JavaScript or a checkbox trick to toggle.

Common mistakes

The first is styling only :hover. Keyboard users move through links with Tab, and they need a visible state too, so style :focus-visible alongside it and never remove outlines without a replacement. The second is putting padding on the <li> instead of the <a>, which leaves a dead zone around the text that does not respond to clicks. The third is marking the current page with colour alone; adding aria-current="page" tells assistive technology which link is active, and you can style that attribute directly.

Summing up the pattern

Build the menu as a <nav> containing a list of links, reset the list styles, lay items out with flexbox, put padding on the links themselves, and style hover, focus and current states. Add sticky positioning and a wrap or toggle for small screens, and you have a navigation bar that works for every visitor.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course