Responsive Navbar using CSS Popover

This responsive navbar HTML CSS tutorial shows you how to build a clean navbar with a brand, a link list, and a call to action button. On mobile, the menu uses the native popover attribute, so it opens and closes without a JavaScript toggle. The hamburger icon swaps to a close icon using the :has() selector, and the whole layout uses nested CSS. A small script closes the menu when a link is clicked or the screen grows.

Live preview:

Skip to view source code


Steps for Responsive Navbar using CSS Popover:

1. Set Up the Header and Base Styles

Link the DM Sans font from Google Font in the head.

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap" rel="stylesheet">

Let’s start with a header element with the class navbar. Inside it, I am adding a .navbar-container div. This container will hold the brand, the menu, and the toggle button.

<header class="navbar">
    <div class="navbar-container"></div>
</header>

Then reset the margins, and define the colors, the border color, and the box shadow as CSS variables on :root, so you can reuse them throughout. Set DM Sans as the font family on the body, and remove the underline from every link. From now on, I am using nested CSS, so every rule lives inside its parent.

* {
    margin: 0;
    box-sizing: border-box;
}

:root {
    --text-dark: hsla(80, 8%, 15%, 1);
    --text-light: hsla(0, 0%, 100%, 1);
    --bg-dark: hsla(86, 9%, 16%, 1);
    --bg-light: hsla(60, 18%, 97%, 1);
    --border-light: hsla(68, 11%, 86%, 1);
    --box-shadow: 0 10px 15px -3px hsla(0, 0%, 0%, .1),
        0 4px 6px -4px hsla(0, 0%, 0%, .1);
}

body {
    font-family: "DM Sans", sans-serif;
    background-color: var(--bg-light);
}

a {
    text-decoration: none;
    color: inherit;
}

2. Style the Navbar Container and add the Brand

Next, add the base styles for .navbar, and center the container with the margin inline property set to auto and a max width. Use flexbox with the justify content property set to space-between, so the brand and the menu sit at opposite ends. Then add a bottom border, since it separates the navbar from the page.

.navbar {
    padding-inline: 5%;
    color: var(--text-dark);

    .navbar-container {
        padding-block: 1.25rem;
        max-width: 80rem;
        margin-inline: auto;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
        border-bottom: 1px solid var(--border-light);
    }
}

Now add the brand link inside .navbar-container. It holds an SVG logo, and the brand name. I added aria-hidden and focusable=”false” on the SVG, because the icon is decorative and screen readers should skip it.

Add this inside .navbar-container.

<a href="#" class="navbar-brand">
    <svg aria-hidden="true" focusable="false" width="28" height="28" viewBox="0 0 28 28" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M3 3H25V9H9V14H22V20H9V25H3V3Z" fill="#272923" />
    </svg>
    flonx
</a>

Use flexbox with the align items property set to center, so the logo and the name sit on one line. Add a negative letter spacing, since it makes the brand name feel tight.

.navbar-container {
    .navbar-brand {
        font-weight: 600;
        font-size: 1.5rem;
        line-height: 1.5;
        letter-spacing: -1.2px;
        display: flex;
        align-items: center;
        gap: 0.625rem;
    }
}

Output:

3. Add the Navigation Menu

Then add the nav after the brand. It has the popover attribute, and the id navbar-menu. Inside it, add a list of links and a call to action link. The popover attribute is what makes the mobile menu work without any JavaScript toggle code.

Add this inside .navbar-container.

<nav class="navbar-menu" id="navbar-menu" popover aria-label="Main navigation">
    <ul class="navbar-links" role="list">
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Work</a></li>
        <li><a href="#">Pricing</a></li>
    </ul>

    <a href="#" class="navbar-cta">Let's talk
        <svg aria-hidden="true" focusable="false" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M4 12L12 4M12 12V4H4" stroke="white" />
        </svg>
    </a>
</nav>

Set the display property to contents on .navbar-menu. As a result, the list and the button behave as direct children of the container on desktop, so the flex layout keeps working. Then style the list with flexbox and a gap, and remove the default list style.

.navbar-container {
    .navbar-menu {
        display: contents;

        .navbar-links {
            list-style: none;
            display: flex;
            align-items: center;
            gap: 2.25rem;
        }
    }
}

Output:

4. Style the Nav Links and the Button

Now give each list item a transparent bottom border, and change the border color on hover. I am also using :focus-within, so keyboard users get the same underline. Then style the call to action as a dark button with a small opacity change on hover.

Add the .navbar-links li rule inside the existing .navbar-links block, and add .navbar-cta after it inside .navbar-menu.

.navbar-menu {
    .navbar-links {
        li {
            border-block-end: 1px solid transparent;
            transition: .15s border;

            &:hover,
            &:focus-within {
                border-color: var(--text-dark);
            }

            a {
                padding-block: .75rem;
                font-size: .875rem;
                line-height: 1.42;
            }
        }
    }

    .navbar-cta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1.25rem;
        padding: .75rem 1.25rem;
        font-size: .875rem;
        line-height: 1.42;
        border-radius: 0.375rem;
        background-color: var(--bg-dark);
        color: var(--text-light);
        transition: .15s opacity;

        &:hover,
        &:focus-visible {
            opacity: .85;
        }
    }
}

Output:

5. Add the Toggle Button

Next, add the toggle button after the nav. The popovertarget attribute points to the menu id, so clicking the button opens and closes the menu. The SVG holds two paths, one for the open icon and one for the close icon.

<button class="navbar-toggle" popovertarget="navbar-menu" aria-label="Toggle menu">
    <svg aria-hidden="true" focusable="false" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5">
        <path class="icon-open" d="M4 8H20M4 16H20" />
        <path class="icon-close" d="M5 5L19 19M19 5L5 19" />
    </svg>
</button>

Hide the button by default, since desktop does not need it. Remove the default border and background, and hide the close icon until the menu is open.

.navbar-container {
    .navbar-toggle {
        display: none;
        background: none;
        border: 0;
        padding: 0;
        color: inherit;
        cursor: pointer;

        .icon-close {
            display: none;
        }
    }
}

Output:

6. Build the Mobile Menu

Now for the mobile layout. I wrapped everything in @supports selector(:popover-open), so browsers without popover support keep the desktop layout instead of a broken menu. Inside it, use a media query for 768px and below. Then show the toggle button, and hide the menu while it is closed.

When the popover is open, set the position property to fixed, and use the inset property to pin the menu right under the navbar. Give it a light background, a bottom border, and a box shadow, so it floats above the page. After that, stack the links in a column, and add a top margin to the button.

.navbar-container {
    @supports selector(:popover-open) {
        @media (max-width: 768px) {
            .navbar-toggle {
                display: flex;
            }

            .navbar-menu:not(:popover-open) {
                display: none;
            }

            .navbar-menu:popover-open {
                display: block;
                width: auto;
                position: fixed;
                inset: 4.8125rem 0 auto 0;
                margin: 0;
                padding: 1.5rem 5%;
                border: 0;
                border-bottom: 1px solid var(--border-light);
                background: var(--bg-light);
                color: var(--text-dark);
                box-shadow: var(--box-shadow);

                .navbar-links {
                    flex-direction: column;
                    align-items: flex-start;
                    gap: 1.25rem;
                    padding: 0;

                    li a {
                        padding: 0;
                        display: block;
                    }
                }

                .navbar-cta {
                    margin-top: 1.5rem;
                }
            }
        }
    }
}

Output:

7. Swap the Icon When the Menu Opens

Finally, use the :has() selector to check if the menu is open. If it is, hide the hamburger icon and show the close icon. As a result, the button reflects the menu state without any JavaScript. Add this inside the media query from the previous step.

@media (max-width: 768px) {
    &:has(.navbar-menu:popover-open) {
        .icon-open {
            display: none;
        }

        .icon-close {
            display: block;
        }
    }
}

The popover handles opening and closing on its own. But two small cases need JavaScript. Link script.js before the closing body tag. First, if someone opens the menu on mobile and then resizes to desktop, close it. Use matchMedia with a change listener for that. Second, close the menu when a link is clicked, since the page would otherwise stay covered by the menu.

document.addEventListener("DOMContentLoaded", () => {
    const menu = document.getElementById('navbar-menu');

    window.matchMedia('(min-width: 769px)').addEventListener('change', (e) => {
        if (e.matches && menu.matches(':popover-open')) menu.hidePopover();
    });

    menu.querySelectorAll('a').forEach((a) => {
        a.addEventListener('click', () => menu.hidePopover());
    });
});

Output:


Final Source Code for Responsive Navbar HTML CSS Popover:

HTML

<header class="navbar">
    <div class="navbar-container">

        <a href="#" class="navbar-brand">
            <svg aria-hidden="true" focusable="false" width="28" height="28" viewBox="0 0 28 28" fill="none"
                xmlns="http://www.w3.org/2000/svg">
                <path d="M3 3H25V9H9V14H22V20H9V25H3V3Z" fill="#272923" />
            </svg>
            flonx
        </a>

        <nav class="navbar-menu" id="navbar-menu" popover aria-label="Main navigation">
            <ul class="navbar-links" role="list">
                <li><a href="#">About</a></li>
                <li><a href="#">Services</a></li>
                <li><a href="#">Work</a></li>
                <li><a href="#">Pricing</a></li>
            </ul>

            <a href="#" class="navbar-cta">Let's talk
                <svg aria-hidden="true" focusable="false" width="16" height="16" viewBox="0 0 16 16" fill="none"
                    xmlns="http://www.w3.org/2000/svg">
                    <path d="M4 12L12 4M12 12V4H4" stroke="white" />
                </svg>
            </a>
        </nav>

        <button class="navbar-toggle" popovertarget="navbar-menu" aria-label="Toggle menu">
            <svg aria-hidden="true" focusable="false" width="24" height="24" viewBox="0 0 24 24" fill="none"
                xmlns="http://www.w3.org/2000/svg" stroke="currentColor" stroke-width="1.5">
                <path class="icon-open" d="M4 8H20M4 16H20" />
                <path class="icon-close" d="M5 5L19 19M19 5L5 19" />
            </svg>
        </button>

    </div>
</header>

CSS

* {
    margin: 0;
    box-sizing: border-box;
}

:root {
    --text-dark: hsla(80, 8%, 15%, 1);
    --text-light: hsla(0, 0%, 100%, 1);
    --bg-dark: hsla(86, 9%, 16%, 1);
    --bg-light: hsla(60, 18%, 97%, 1);
    --border-light: hsla(68, 11%, 86%, 1);
    --box-shadow: 0 10px 15px -3px hsla(0, 0%, 0%, .1),
        0 4px 6px -4px hsla(0, 0%, 0%, .1);
}

body {
    font-family: "DM Sans", sans-serif;
    background-color: var(--bg-light);
}

a {
    text-decoration: none;
    color: inherit;
}

.navbar {
    padding-inline: 5%;
    color: var(--text-dark);

    .navbar-container {
        padding-block: 1.25rem;
        max-width: 80rem;
        margin-inline: auto;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
        border-bottom: 1px solid var(--border-light);

        .navbar-brand {
            font-weight: 600;
            font-size: 1.5rem;
            line-height: 1.5;
            letter-spacing: -1.2px;
            display: flex;
            align-items: center;
            gap: 0.625rem;
        }

        .navbar-menu {
            display: contents;

            .navbar-links {
                list-style: none;
                display: flex;
                align-items: center;
                gap: 2.25rem;

                li {
                    border-block-end: 1px solid transparent;
                    transition: .15s border;

                    &:hover,
                    &:focus-within {
                        border-color: var(--text-dark);
                    }

                    a {
                        padding-block: .75rem;
                        font-size: .875rem;
                        line-height: 1.42;
                    }
                }
            }

            .navbar-cta {
                display: flex;
                justify-content: space-between;
                align-items: center;
                gap: 1.25rem;
                padding: .75rem 1.25rem;
                font-size: .875rem;
                line-height: 1.42;
                border-radius: 0.375rem;
                background-color: var(--bg-dark);
                color: var(--text-light);
                transition: .15s opacity;

                &:hover,
                &:focus-visible {
                    opacity: .85;
                }
            }
        }

        .navbar-toggle {
            display: none;
            background: none;
            border: 0;
            padding: 0;
            color: inherit;
            cursor: pointer;

            .icon-close {
                display: none;
            }
        }

        @supports selector(:popover-open) {
            @media (max-width: 768px) {
                .navbar-toggle {
                    display: flex;
                }

                .navbar-menu:not(:popover-open) {
                    display: none;
                }

                .navbar-menu:popover-open {
                    display: block;
                    width: auto;
                    position: fixed;
                    inset: 4.8125rem 0 auto 0;
                    margin: 0;
                    padding: 1.5rem 5%;
                    border: 0;
                    border-bottom: 1px solid var(--border-light);
                    background: var(--bg-light);
                    color: var(--text-dark);
                    box-shadow: var(--box-shadow);

                    .navbar-links {
                        flex-direction: column;
                        align-items: flex-start;
                        gap: 1.25rem;
                        padding: 0;

                        li a {
                            padding: 0;
                            display: block;
                        }
                    }

                    .navbar-cta {
                        margin-top: 1.5rem;
                    }
                }

                &:has(.navbar-menu:popover-open) {
                    .icon-open {
                        display: none;
                    }

                    .icon-close {
                        display: block;
                    }
                }
            }
        }
    }
}

JS:

document.addEventListener("DOMContentLoaded", () => {
    const menu = document.getElementById('navbar-menu');

    window.matchMedia('(min-width: 769px)').addEventListener('change', (e) => {
        if (e.matches && menu.matches(':popover-open')) menu.hidePopover();
    });

    menu.querySelectorAll('a').forEach((a) => {
        a.addEventListener('click', () => menu.hidePopover());
    });
});

If you have any doubts or stuck somewhere, you can reach out through Coding Yaar's Discord server.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Most Voted
Newest Oldest
0
Would love your thoughts, please comment.x
()
x