Skip to content
TheChatArchiveThe Chat Archive
Grok 4.1 Fast· xaiHugging Face Frontier Reasoning Traces

Design a responsive navigation bar that transforms into a hamburger menu on smaller…

  1. User

    Design a responsive navigation bar that transforms into a hamburger menu on smaller screens using HTML, CSS, and JS.

  2. Assistant
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Responsive Navigation Bar</title>
        <style>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
            }
    
            body {
                font-family: 'Arial', sans-serif;
                line-height: 1.6;
                background-color: #f4f4f4;
            }
    
            /* Navigation Styles */
            .navbar {
                background-color: #333;
                color: white;
                padding: 1rem 2rem;
                box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
                position: sticky;
                top: 0;
                z-index: 1000;
            }
    
            .nav-container {
                max-width: 1200px;
                margin: 0 auto;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
    
            .nav-logo {
                font-size: 1.5rem;
                font-weight: bold;
                text-decoration: none;
                color: white;
            }
    
            .nav-menu {
                display: flex;
                list-style: none;
                gap: 2rem;
            }
    
            .nav-menu a {
                color: white;
                text-decoration: none;
                font-weight: 500;
                transition: color 0.3s ease;
                position: relative;
            }
    
            .nav-menu a:hover {
                color: #ffd700;
            }
    
            .nav-menu a::after {
                content: '';
                position: absolute;
                bottom: -5px;
                left: 0;
                width: 0;
                height: 2px;
                background-color: #ffd700;
                transition: width 0.3s ease;
            }
    
            .nav-menu a:hover::after {
                width: 100%;
            }
    
            .hamburger {
                display: none;
                flex-direction: column;
                cursor: pointer;
                padding: 0.5rem;
                z-index: 1001;
            }
    
            .hamburger span {
                width: 25px;
                height: 3px;
                background-color: white;
                margin: 3px 0;
                transition: 0.3s;
                transform-origin: center;
            }
    
            /* Mobile Menu Styles */
            @media screen and (max-width: 768px) {
                .hamburger {
                    display: flex;
                }
    
                .nav-menu {
                    position: fixed;
                    top: 70px; /* Adjust based on navbar height */
                    left: -100%;
                    width: 100%;
                    height: calc(100vh - 70px);
                    background-color: #333;
                    flex-direction: column;
                    justify-content: flex-start;
                    align-items: center;
                    padding-top: 2rem;
                    transition: left 0.3s ease;
                    gap: 1rem;
                }
    
                .nav-menu.active {
                    left: 0;
                }
    
                .nav-menu a {
                    font-size: 1.2rem;
                    padding: 1rem;
                    width: 100%;
                    text-align: center;
                }
    
                /* Hamburger Animation to X */
                .hamburger.active span:nth-child(1) {
                    transform: rotate(-45deg) translate(-5px, 6px);
                }
    
                .hamburger.active span:nth-child(2) {
                    opacity: 0;
                }
    
                .hamburger.active span:nth-child(3) {
                    transform: rotate(45deg) translate(-5px, -6px);
                }
            }
    
            /* Additional responsiveness for very small screens */
            @media screen and (max-width: 480px) {
                .navbar {
                    padding: 1rem;
                }
    
                .nav-container {
                    padding: 0 1rem;
                }
    
                .nav-logo {
                    font-size: 1.2rem;
                }
            }
    
            /* Demo content */
            .content {
                max-width: 1200px;
                margin: 2rem auto;
                padding: 2rem;
                background: white;
                border-radius: 8px;
                box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            }
        </style>
    </head>
    <body>
        <nav class="navbar">
            <div class="nav-container">
                <a href="#" class="nav-logo">Logo</a>
                <ul class="nav-menu" id="navMenu">
                    <li><a href="#home">Home</a></li>
                    <li><a href="#about">About</a></li>
                    <li><a href="#services">Services</a></li>
                    <li><a href="#portfolio">Portfolio</a></li>
                    <li><a href="#contact">Contact</a></li>
                </ul>
                <div class="hamburger" id="hamburger">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            </div>
        </nav>
    
        <div class="content">
            <h1>Responsive Navigation Demo</h1>
            <p>Resize your browser window to see the navigation transform into a hamburger menu on screens smaller than 768px. Click the hamburger to toggle the menu.</p>
            <p>This uses pure HTML, CSS, and vanilla JavaScript. No frameworks required!</p>
        </div>
    
        <script>
            // JavaScript for hamburger menu toggle
            const hamburger = document.getElementById('hamburger');
            const navMenu = document.getElementById('navMenu');
    
            hamburger.addEventListener('click', () => {
                hamburger.classList.toggle('active');
                navMenu.classList.toggle('active');
            });
    
            // Close mobile menu when clicking on a link (improves UX)
            const navLinks = document.querySelectorAll('.nav-menu a');
            navLinks.forEach(link => {
                link.addEventListener('click', () => {
                    hamburger.classList.remove('active');
                    navMenu.classList.remove('active');
                });
            });
    
            // Close menu on window resize to desktop (optional)
            window.addEventListener('resize', () => {
                if (window.innerWidth > 768) {
                    hamburger.classList.remove('active');
                    navMenu.classList.remove('active');
                }
            });
        </script>
    </body>
    </html>
    

    This is a complete, self-contained responsive navigation bar:

    • Desktop (≥769px): Horizontal menu with hover effects and underline animations.
    • Mobile (≤768px): Hamburger icon (animates to "X" when open). Full-screen vertical menu slides in from the left.
    • Features:
      • Smooth CSS transitions.
      • Sticky navbar.
      • Closes menu on link click.
      • Auto-closes on desktop resize.
      • Mobile-optimized for very small screens (≤480px).
      • Accessible (focusable links, semantic HTML).
    • Customization: Easy to tweak colors, add more links, or integrate with your site. Just copy-paste into an HTML file and open in a browser!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.