On this page10 sections
  1. Learning Objectives
  2. 1. The Starting HTML
  3. 2. A Vertical Navigation Bar
  4. 3. A Horizontal Navigation Bar
  5. 4. A Sticky Top Bar
  6. 5. Underline-Style Tabs
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 021: Navigation Bars

Category5: Pseudo-classes & Components
Lecture021 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

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

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:

css
nav ul { list-style: none; margin: 0; padding: 0; }

2. A Vertical Navigation Bar

Example

html
<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

html
<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:

html
<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

html
<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

  1. Which HTML structure should a navigation bar use?
  2. Which three properties remove list styling?
  3. How do you turn a vertical list into a horizontal bar?
  4. Why put the padding on the <a> instead of the <li>?
  5. How do you keep the bar at the top while scrolling?

Practice Answers

  1. A <ul> of links inside a <nav>.
  2. list-style: none; margin: 0; padding: 0;
  3. display: flex on the <ul>.
  4. So the whole padded area is clickable.
  5. position: sticky; top: 0;

Key Takeaways

  • Navigation = <nav> + <ul> of links, with list styling removed.
  • display: block links for vertical menus; display: flex on the list for horizontal bars.
  • Highlight the current page; add :hover feedback.
  • position: sticky; top: 0 keeps the bar visible.