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.
<ul> with multiple <li> items.<a> tag.The same menu items can be reused for both horizontal and vertical navbars by changing only the CSS.
<!-- 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>
Use different classes to switch between a horizontal top menu and a vertical sidebar menu.
/* 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;
}
This is how a simple horizontal navbar structure looks in HTML:
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.
flexbox to quickly build responsive horizontal navbars.<a> tags to increase the clickable area.list-style: none; on the <ul>.linear-gradient().::before pseudo-element.