Paramclasses Academy - Blog

Tutorials Details

How to Create a Responsive Navigation Bar with Active Links Using HTML & CSS

How to Create a Responsive Navigation Bar with Active Links Using HTML & CSS

November 7, 2024

This code creates a simple navigation bar with active link styling.

HTML(index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Navbar with Active Links</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

<!-- Navigation Bar -->
<nav class="navbar">
    <ul>
        <li><a href="#" class="active">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Portfolio</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
</nav>

<!-- Main Content -->
<div class="content">
    <h1>Responsive Navbar with Active Links</h1>
    <p>This is a demo of a responsive navbar with active link highlighting.</p>
</div>

</body>
</html>

CSS Code

The CSS handles responsiveness and styling for the active link.

/* Basic Reset */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Arial, sans-serif;
}

/* Navbar Styling */
.navbar {
    background-color: #333;
    text-align: center;
}

.navbar ul {
    list-style: none;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    padding: 10px;
    margin: 0;
}

.navbar ul li {
    margin: 0 15px;
}

.navbar ul li a {
    color: white;
    text-decoration: none;
    padding: 10px 15px;
    display: inline-block;
    transition: background 0.3s;
}

.navbar ul li a:hover {
    background-color: #575757;
    border-radius: 5px;
}

/* Active Link Styling */
.navbar ul li a.active {
    background-color: #ff6b6b;
    border-radius: 5px;
}

/* Responsive Design */
@media (max-width: 768px) {
    .navbar ul {
        flex-direction: column;
    }

    .navbar ul li {
        margin: 10px 0;
    }
}

Explanation

  1. Active Link Styling: The .active class is applied to highlight the current page link. It has a distinct background color (#ff6b6b).
  2. Responsive Layout: The navbar switches to a vertical layout on screens smaller than 768px, creating a mobile-friendly design.
  3. Hover Effect: Links change color when hovered over, providing a subtle interaction effect.

This code gives you a flexible navigation bar with active link highlighting and responsive adjustments, perfect for smaller and larger screens.

Copyright © Paramwebinfo Academy.All Rights Reserved

Designed by PARAMWEBINFO