CSS

Responsive Navigation Bar with Flexbox and JavaScript Toggle

Build a fully responsive navigation bar using Flexbox, featuring dynamic item distribution and a JavaScript-powered hamburger menu toggle for mobile views.

/* HTML structure for context:
<nav class="navbar">
    <a href="#" class="navbar__brand">MyBrand</a>
    <button class="navbar__toggler" aria-label="Toggle navigation">&#9776;</button>
    <div class="navbar__menu">
        <ul class="navbar__list">
            <li class="navbar__item"><a href="#" class="navbar__link">Home</a></li>
            <li class="navbar__item"><a href="#" class="navbar__link">About</a></li>
            <li class="navbar__item"><a href="#" class="navbar__link">Services</a></li>
            <li class="navbar__item"><a href="#" class="navbar__link">Contact</a></li>
        </ul>
    </div>
</nav>

<!-- Example JS for toggler: -->
<!--
<script>
    document.addEventListener('DOMContentLoaded', () => {
        const toggler = document.querySelector('.navbar__toggler');
        const menu = document.querySelector('.navbar__menu');
        toggler.addEventListener('click', () => {
            menu.classList.toggle('is-active');
            toggler.setAttribute('aria-expanded', menu.classList.contains('is-active'));
        });
    });
</script>
-->
*/

body { margin: 0; font-family: sans-serif; }

.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #282c34;
    padding: 1rem;
    color: white;
}

.navbar__brand {
    color: white;
    text-decoration: none;
    font-size: 1.5rem;
    font-weight: bold;
}

.navbar__toggler {
    display: none; /* Hidden by default on desktop */
    background: none;
    border: none;
    color: white;
    font-size: 2rem;
    cursor: pointer;
    padding: 0.5rem;
}

.navbar__menu {
    display: flex; /* Flexbox for menu items */
    flex-grow: 1; /* Allow menu to take available space */
    justify-content: flex-end; /* Align items to the right */
}

.navbar__list {
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
}

.navbar__item {
    margin-left: 1.5rem;
}

.navbar__link {
    color: white;
    text-decoration: none;
    padding: 0.5rem 0;
    display: block;
    transition: color 0.3s ease;
}

.navbar__link:hover { color: #61dafb; }

@media (max-width: 768px) {
    .navbar {
        flex-wrap: wrap; /* Allow items to wrap */
    }
    .navbar__toggler {
        display: block; /* Show toggler on mobile */
    }
    .navbar__menu {
        flex-basis: 100%; /* Menu takes full width */
        flex-direction: column; /* Stack items vertically */
        align-items: flex-start; /* Align items to the left */
        display: none; /* Hide by default on mobile */
        margin-top: 1rem; /* Space below brand/toggler */
    }
    .navbar__menu.is-active {
        display: flex; /* Show when active via JS */
    }
    .navbar__list {
        flex-direction: column;
        width: 100%;
    }
    .navbar__item {
        margin: 0.5rem 0;
        width: 100%;
    }
    .navbar__link {
        padding: 0.8rem 1rem;
        background-color: #3a3f47;
        border-radius: 4px;
    }
}
How it works: This snippet demonstrates how to build a responsive navigation bar using CSS Flexbox. On larger screens, the brand is on the left, and menu items are horizontally distributed to the right using `justify-content: flex-end` and `flex-grow: 1` on the menu container. For smaller screens (via a media query), the standard menu is hidden, and a "hamburger" toggle button is displayed. When the toggle is activated (typically via JavaScript, adding an `is-active` class to `.navbar__menu`), the menu transforms into a vertically stacked list (`flex-direction: column`) that takes up the full width, making it mobile-friendly. This approach efficiently handles different screen sizes with clean Flexbox layouts for both states.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs