On this page10 sections
Lecture 021: Navigation Bars
| Category | 5: Pseudo-classes & Components |
| Lecture | 021 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
Every site has a navigation bar, and almost all of them start the same way: a list of links. With a few CSS rules, that list becomes a vertical sidebar menu or a horizontal top bar, complete with hover effects and a highlighted current page.
Learning Objectives
- Turn a
<ul>of links into a navigation menu - Build vertical and horizontal navigation bars
- Highlight the active page and add hover effects
- Make a sticky top bar
1. The Starting HTML
<nav>
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">Courses</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>A navigation bar is a list of links, so a <ul> inside a <nav> is the right HTML. First
remove the list styling:
nav ul { list-style: none; margin: 0; padding: 0; }2. A Vertical Navigation Bar
Example
<style>
.side ul { list-style: none; margin: 0; padding: 0; width: 200px; background: #f1f3f5; border-radius: 8px; overflow: hidden; }
.side a { display: block; padding: 10px 16px; color: #343a40; text-decoration: none; }
.side a:hover { background: #dee2e6; }
.side a.active { background: #1971c2; color: white; }
.side li + li { border-top: 1px solid #dee2e6; }
</style>
<nav class="side">
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">Courses</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>display: block makes the whole row clickable, not just the words.
3. A Horizontal Navigation Bar
Example
<style>
.top ul { list-style: none; margin: 0; padding: 0; display: flex; background: #212529; }
.top a { display: block; padding: 14px 18px; color: #dee2e6; text-decoration: none; }
.top a:hover { background: #343a40; color: white; }
.top a.active { background: #2f9e44; color: white; }
.top .right { margin-left: auto; }
</style>
<nav class="top">
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">Courses</a></li>
<li><a href="#">Blog</a></li>
<li class="right"><a href="#">Log in</a></li>
</ul>
</nav>display: flex on the list lines the items up; margin-left: auto pushes "Log in" to the right.
4. A Sticky Top Bar
Example: scroll inside the preview:
<style>
body { margin: 0; }
.top { position: sticky; top: 0; background: #1c7ed6; }
.top ul { list-style: none; margin: 0; padding: 0; display: flex; }
.top a { display: block; padding: 12px 16px; color: white; text-decoration: none; }
.content { padding: 16px; height: 900px; background: linear-gradient(#f8f9fa, #ced4da); }
</style>
<nav class="top">
<ul><li><a href="#">Home</a></li><li><a href="#">Shop</a></li><li><a href="#">Help</a></li></ul>
</nav>
<div class="content">Scroll down: the bar stays at the top.</div>5. Underline-Style Tabs
Example
<style>
.tabs { display: flex; gap: 4px; border-bottom: 2px solid #dee2e6; }
.tabs a { padding: 10px 14px; color: #495057; text-decoration: none; border-bottom: 3px solid transparent; margin-bottom: -2px; }
.tabs a:hover { color: #1971c2; }
.tabs a.active { color: #1971c2; border-bottom-color: #1971c2; font-weight: 600; }
</style>
<nav class="tabs">
<a href="#" class="active">Overview</a><a href="#">Reviews</a><a href="#">Questions</a>
</nav>Common Beginner Mistakes
1. Links that are only clickable on the words. Make them display: block and put the padding on the <a>, not the <li>.
2. No sign of the current page. Add an active class (and aria-current="page" for screen readers).
3. Too many top-level links. Seven or fewer is easier to scan; group the rest in a dropdown (Lecture 022).
Practice Questions
- Which HTML structure should a navigation bar use?
- Which three properties remove list styling?
- How do you turn a vertical list into a horizontal bar?
- Why put the padding on the
<a>instead of the<li>? - How do you keep the bar at the top while scrolling?
Practice Answers
- A
<ul>of links inside a<nav>. list-style: none; margin: 0; padding: 0;display: flexon the<ul>.- So the whole padded area is clickable.
position: sticky; top: 0;
Key Takeaways
- Navigation =
<nav>+<ul>of links, with list styling removed. display: blocklinks for vertical menus;display: flexon the list for horizontal bars.- Highlight the current page; add
:hoverfeedback. position: sticky; top: 0keeps the bar visible.