CSS Navigation Bar

A navigation bar is the foundation of any website layout. It guides your user through the content like a compass pointing the user in the right direction. In CSS we can design a list of links to look just like a beautiful navigable component.

What is a Navigation Bar?

A navigation bar (or nav bar) is a user interface that contains links to the major pages of the website: Home, About, Services, and Contact. The nav bar helps your user navigate.

Types of Navigation Bars

Basic HTML Structure for a Navigation Bar.


                 <nav> 
                    <ul> 
                        <li> <a href="# ">  Home </li> 
                        <li> <a href="# ">  About  </li> 
                        <li> <a href="# ">  Services  </li> 
                        <li> <a href="# ">  Contact  </li> 
                    </ul>
                 </nav>

Styling a Horizontal Navigation Bar with CSS


                nav {
                    background-color: #333;
                    padding: 0.5rem 1rem;
                }

                nav ul {
                    list-style-type: none;
                    margin: 0;
                    padding: 0;
                    display: flex;
                }

                nav ul li {
                       margin-right: 1.5rem;
                }
                
                nav ul li a {
                        color: white;
                        text-decoration: none;
                        font-weight: bold;
                        padding: 0.5rem 1rem;
                        display: block;
                }

              nav ul li a : hover{
                    background-color: #555;
                    border-radius: 4px;
              }

Creating a Vertical Navigation Bar

By changing the flex direction or removing it altogether, you can stack links vertically.

nav ul {
  display: block;
}

            nav ul {
                  display: block;
            }

            nav ul li {
                  margin: 0 0 1rem 0;
            }
          

Responsive Navigation with Media Queries

Make your nav bar adaptable to mobile screens using media queries:


                @media (max-width: 768px) {
                  nav ul  {
                      flex-direction: column;
                }
                
                  nav ul li  {
                        margin-bottom: 1rem;
                  }
                }

Advanced Ideas for Navbars

Best Practices for Navigation Bars

Conclusion

A nice clean, responsive and well-stylised navigation bar will make a lasting impression. It improves not only the looks of your website but also usability. With a little CSS rule, run-of-the-mill menus can be turned into elegant, enjoyable, interactive experiences to traverse as intended by you for the user experience. Learning to build the navigation bar is probably one of the most essential UI components for you as a web developer - so take the time and effort to craft it well Bhai!

Try it Yourself