HTML Navigation Bar | HTML Tutorial - Learn with VOKS
Back Next

HTML Navigation Bar


Introduction to HTML Navigation Bar


A navigation bar (navbar) is a section of a website that allows users to navigate between different pages or sections.

It is usually placed at the top of the webpage and contains links to important sections such as Home, About, Services, Contact, etc.

Navigation bars improve user experience and help organize website structure.


Basic Structure of a Navigation Bar


A navigation bar is typically created using:

  • <nav> – Semantic HTML5 element for navigation
  • <ul> – Unordered list to structure links
  • <li> – Each menu item
  • <a> – Hyperlink to pages

Example:

<nav>
    <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="services.html">Services</a></li>
        <li><a href="contact.html">Contact</a></li>
    </ul>
</nav>


Styling the Navigation Bar with CSS


To make it horizontal and visually appealing, we use CSS:

<style>
nav ul {
    list-style-type: none; /* Remove bullets */
    margin: 0;
    padding: 0;
    background-color: #333; /* Navbar background */
    overflow: hidden;
}

nav ul li {
    float: left; /* Horizontal layout */
}

nav ul li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 20px;
    text-decoration: none;
}

nav ul li a:hover {
    background-color: #111; /* Hover effect */
}
</style>

How it Works:

  • <ul> holds all the links
  • <li> represents each menu item
  • float: left makes the items horizontal
  • background-color sets the navbar color
  • hover effect improves interactivity


Responsive Navigation (Optional)

Modern websites require mobile-friendly navbars.

A simple responsive approach is using flexbox:

nav ul {
    display: flex;
    flex-wrap: wrap; /* Wrap items on small screens */
}

nav ul li {
    flex: 1; /* Evenly distributed */
}

For fully responsive menus, JavaScript is often used for toggle buttons on mobile.


Practice Code Example

Create a basic horizontal navigation bar with hover effects:

Example Code:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML Navigation Bar Practice</title>
    <style>
        nav ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            background-color: #2c3e50;
            overflow: hidden;
        }

        nav ul li {
            float: left;
        }

        nav ul li a {
            display: block;
            color: white;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
            font-family: Arial, sans-serif;
        }

        nav ul li a:hover {
            background-color: #1abc9c;
        }
    </style>
</head>
<body>

    <nav>
        <ul>
            <li><a href="index.html">Home</a></li>
            <li><a href="about.html">About</a></li>
            <li><a href="services.html">Services</a></li>
            <li><a href="contact.html">Contact</a></li>
        </ul>
    </nav>

    <h1>Welcome to My Website</h1>
    <p>This is a sample page with a navigation bar.</p>

</body>
</html>
HTML
Introduction HTML Images HTML Colors HTML Fonts HTML Hyperlinks HTML Heading HTML Navigation Bar HTML Lists HTML Tables HTML Forms HTML Form Validation HTML Summary
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL