CSS Navbar — PBA Institute CSS Tutorial
Chapter 15 · CSS Tutorial Series
10 min read Beginner

CSS Navbar

A CSS Navbar (navigation bar) is the primary way users move through a website. With CSS, you can create horizontal, vertical, fixed, sticky, dropdown, and fully responsive mobile-friendly navbars using just flexbox, color, and hover effects.

Why Navbars Matter

  • Navigation: Helps users find pages quickly.
  • Brand identity: Logo + colors reinforce brand.
  • Discoverability: Improves SEO by exposing important links.
  • Responsiveness: Modern navbars adapt for mobile screens.

Types of Navbars

➡️

Horizontal

Most common — links arranged in a row at the top.

⬇️

Vertical

Links arranged in a column on the side.

📌

Fixed / Sticky

Stays visible while scrolling.

📂

Dropdown

Reveals sub-menus on hover or click.

📱

Responsive

Collapses into a hamburger menu on mobile.

🌟

Mega Menu

Multi-column dropdown with rich content.

Navbar Syntax

Basic Navbar Syntax
nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #1a73e8;
    padding: 14px 30px;
}
nav a {
    color: #fff;
    text-decoration: none;
    padding: 8px 16px;
    border-radius: 6px;
    transition: background .3s ease;
}
nav a:hover { background: rgba(255,255,255,.15); }

Full Responsive Navbar Example

HTML + CSS Code
<!DOCTYPE html>
<html>
<head>
    <title>CSS Navbar</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: Arial; }
        nav {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: #1a73e8;
            color: white;
            padding: 14px 30px;
        }
        .logo { font-weight: 700; font-size: 22px; }
        .links { display: flex; gap: 8px; }
        .links a {
            color: white;
            text-decoration: none;
            padding: 8px 14px;
            border-radius: 6px;
            transition: all .3s ease;
        }
        .links a:hover { background: rgba(255,255,255,.18); }

        @media (max-width: 700px) {
            .links { display: none; }
        }
    </style>
</head>
<body>
    <nav>
        <div class="logo">PBA Institute</div>
        <div class="links">
            <a href="#">Home</a>
            <a href="#">Courses</a>
            <a href="#">About</a>
            <a href="#">Contact</a>
        </div>
    </nav>
</body>
</html>
Output A clean blue horizontal navbar with logo and hover effects on links, responsive on mobile.

Code Explanation

  • display: flex: Arranges logo and links in a single row.
  • justify-content: space-between: Pushes logo to the left and links to the right.
  • padding: Adds breathing room inside the navbar.
  • :hover: Soft background change on hover.
  • Media query: Hides links below 700px width for mobile menus.

CSS Navbar

A navbar in CSS helps organize website navigation links in a clean and user-friendly way. CSS properties like background-color, padding, alignment, hover effects, and responsive layouts make navigation bars attractive and interactive.

Example 1 : Simple Navbar

This example creates a centered navigation bar with hover effects and styled links.

Full HTML + CSS Code
<!DOCTYPE html>

<html>

<head>

<style>

body{

    font-family:Arial;
    margin:0;
    padding:0;

    background-color:#95C3E5;

}

nav{

    background-color:#333;

    text-align:center;

    padding:10px 0;

}

nav ul{

    list-style-type:none;

    margin:0;
    padding:0;

}

nav ul li{

    display:inline;

    margin-right:20px;

}

nav ul li a{

    text-decoration:none;

    color:white;

    font-size:18px;

    padding:10px 15px;

}

nav ul li a:hover{

    background-color:tomato;

}

</style>

</head>

<body>

<nav>

<ul>

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

</nav>

</body>

</html>
Output
  • text-align: Centers navbar links.
  • display:inline: Displays menu items horizontally.
  • :hover: Adds hover background effect.

Example 2 : Scrollable Navbar

This example creates a horizontal scrollable navigation menu.

Full HTML + CSS Code
<html>

<head>

<style>

div.s{

    background-color:lightyellow;

    overflow:auto;

    white-space:nowrap;

}

div.s a{

    display:inline-block;

    color:black;

    text-align:center;

    padding:14px;

    text-decoration:none;

}

div.s a:hover{

    background-color:tomato;

}

</style>

</head>

<body>

<div class="s">

<a href="#">Home</a>

<a href="#">News</a>

<a href="#">Contact</a>

<a href="#">About</a>

<a href="#">Support</a>

<a href="#">Blog</a>

<a href="#">Tools</a>

</div>

</body>

</html>
Output
  • overflow:auto: Enables horizontal scrolling.
  • white-space:nowrap: Prevents links from wrapping.
  • display:inline-block: Keeps navbar links aligned horizontally.

Conclusion

CSS navbars improve website navigation by organizing links clearly and attractively. They provide better user experience through hover effects, responsive layouts, and visually structured navigation systems.

Navbar Properties

Property Purpose Example
display Layout flex
justify-content Horizontal alignment space-between
align-items Vertical alignment center
position Sticky / fixed navbar sticky / fixed
z-index Layer stacking 1000
@media Responsive behavior max-width: 700px

Navbar Categories

Horizontal

Use flexbox row layout.

Vertical

Use flexbox column layout.

Fixed

position: fixed; top: 0; for always-visible navbar.

Navbar Symbols

Layout
display: flex gap justify-content align-items
Position
fixed sticky absolute
Responsive
@media hamburger toggle

Important Notes

  • Use semantic <nav>: Helps screen readers and SEO.
  • Keep it simple: Limit top-level links to 5-7.
  • Always make it responsive: Add a hamburger menu for small screens.
  • Highlight active page: Use an .active class to show current page.

Real-World Use Cases

Websites

Top navigation on every website.

Mobile Apps

Bottom navbar in PWAs and apps.

E-commerce

Category navbar plus search and cart.

Practice Questions

  • Create a horizontal navbar using flexbox.
  • Make a sticky navbar that stays on top while scrolling.
  • Add hover effects to navigation links.
  • Create a responsive navbar that hides links on mobile.
  • Highlight the active link with a different background.

Frequently Asked Questions

  • How to make a navbar sticky: Use position: sticky; top: 0; on the nav.
  • How to make a responsive navbar: Use a media query and a hamburger menu toggle for small screens.
  • How to highlight active page in navbar: Add an active class to the current link and style it.
  • Should I use nav or div for navbar: Use the semantic <nav> tag — it improves accessibility and SEO.

Conclusion

Navbars are the front door of every website. With flexbox, smart spacing, hover states, and responsive media queries, you can build navigation that is both beautiful and effortless to use.

CSS All Topics

Continue Learning

Previous

Go to Cursor Type Chapter

Next

Go to Layout Chapter