← Back to Chapters

CSS Navbar

? CSS Navbar

⚡ Quick Overview

A CSS navbar (navigation bar) is a menu that helps users move between pages or sections of a website. Using CSS, you can easily build clean, responsive horizontal or vertical navbars that look modern and are easy to use.

? Key Concepts

  • A navbar is usually built using a list: <ul> with multiple <li> items.
  • Links are placed inside each item using the <a> tag.
  • CSS is used to remove bullets, add spacing, colors, and hover effects.
  • Flexbox is commonly used to create horizontal navbars.
  • Vertical navbars are great for side menus or dashboards.

? Basic Structure (Syntax)

The same menu items can be reused for both horizontal and vertical navbars by changing only the CSS.

? View Code Example
<!-- Basic structure for horizontal and vertical navbars -->
<ul class="navbar-horizontal">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>

<ul class="navbar-vertical">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>

? Styling Horizontal & Vertical Navbars (CSS)

Use different classes to switch between a horizontal top menu and a vertical sidebar menu.

? View Code Example
/* CSS for horizontal and vertical navbars */
.navbar-horizontal {
display: flex;
background-color: #007bff;
border-radius: 5px;
justify-content: center;
list-style: none;
padding: 0;
margin: 0;
}
.navbar-horizontal li a {
display: block;
padding: 12px 20px;
color: white;
text-decoration: none;
transition: background-color 0.3s ease;
}
.navbar-horizontal li a:hover {
background-color: #0056b3;
}
.navbar-vertical {
width: 180px;
background-color: #28a745;
border-radius: 5px;
list-style: none;
padding: 0;
margin: 0;
}
.navbar-vertical li a {
display: block;
padding: 12px 20px;
color: white;
text-decoration: none;
border-bottom: 1px solid #1e7e34;
transition: background-color 0.3s ease;
}
.navbar-vertical li a:hover {
background-color: #1e7e34;
}

? Live Output / Explanation

Horizontal Navbar

This is how a simple horizontal navbar structure looks in HTML:

Vertical Navbar

A vertical navbar is often used as a sidebar menu:

With proper CSS applied (as shown in the code examples), the horizontal navbar appears as a blue bar across the top, while the vertical navbar becomes a green sidebar menu.

? Tips & Best Practices

  • Use flexbox to quickly build responsive horizontal navbars.
  • Apply padding on <a> tags to increase the clickable area.
  • Add hover and active styles so users know which link is selected.
  • Remove default bullets using list-style: none; on the <ul>.
  • Use sticky or fixed positioning with vertical navbars for persistent side menus.

? Practice Tasks

  1. Create a horizontal navbar with 5 links including Blog and FAQ.
  2. Convert the vertical navbar’s background into a gradient using linear-gradient().
  3. Add small icons before each link using the ::before pseudo-element.
  4. Make the horizontal navbar stack vertically on small screens using a media query.