CSS Navigation Bar
In this page:
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li a {
display: block;
padding: length;
}
Semantic List Structure
A professional navigation bar starts with a clean HTML structure. By wrapping a list of links inside semantic nav and ul tags, you tell search engines and screen readers exactly how your site is structured.
Example: Semantic List Structure
<style>
nav ul { list-style: none; margin: 0; padding: 0; background: #333; }
nav li { display: inline-block; }
nav a { display: block; padding: 10px 16px; color: #fff; text-decoration: none; }
</style>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
</nav>
Horizontal Navigation with inline-block
By default, list items (li) are block elements that stack vertically. You can align them horizontally into a clean menu bar by setting their display property to inline-block.
Example: Horizontal Navigation with inline-block
<style>
li {
display: inline-block;
margin-right: 10px;
}
</style>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
Horizontal Navigation with Flexbox
Using Flexbox is the modern, standard way to build navigation menus. By setting display: flex on the parent ul tag, you can align elements side-by-side cleanly and control their spacing with properties like justify-content.
Example: Horizontal Navigation with Flexbox
<style>
ul {
display: flex;
justify-content: space-between;
list-style: none;
padding: 0;
}
li {
background: steelblue;
padding: 8px 12px;
}
li a {
color: white;
}
</style>
<ul>
<li>Logo</li>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
Persistent Fixed Navigation Bars
To keep your navigation bar visible at the top of the screen as the user scrolls, you can set its position to fixed and lock its width to 100%. This keeps your site easy to navigate even on long pages.
Example: Persistent Fixed Navigation Bars
<style>
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: darkorange;
color: white;
padding: 10px;
text-align: center;
}
.content {
margin-top: 60px;
}
.content p {
background: #f0f0f0;
padding: 12px;
margin-bottom: 10px;
}
</style>
<nav>Always visible while scrolling</nav>
<div class="content">
<p>Scroll down to see the navbar stay fixed. Paragraph 1.</p>
<p>Paragraph 2.</p>
<p>Paragraph 3.</p>
<p>Paragraph 4.</p>
<p>Paragraph 5.</p>
<p>Paragraph 6.</p>
<p>Paragraph 7.</p>
<p>Paragraph 8.</p>
</div>
Active Link indicators
When users navigate your site, they need to see which page they are currently on. You can create an active link indicator by applying a custom class (like class="active") to the current page's menu link and styling it with unique colors.
Example: Active Link indicators
<style>
a.active {
border-bottom: 2px solid blue;
}
</style>
<a href="#" class="active">Home</a>
<a href="#">About</a>
- Using plain, generic div elements to build navigation menus instead of semantic nav and list tags.
- Forgetting to add top padding to the body container when using a fixed top navigation bar, causing content cutoff.
- Setting fixed pixel dimensions on mobile navigation containers, causing layout breakages on smaller screens.
- Start your navigation bars with a semantic HTML structure using nav, ul, and li elements.
- Align list items horizontally into a clean, responsive menu bar using Flexbox or inline-block.
- Keep navigation visible using fixed positioning, and use unique active classes to indicate the user's current location.
Standard navigation bar layouts and Flexbox displays are supported natively by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline