Toggle navigation
☰
Home
HTML
CSS
Scripting
Database
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Mega Menu</title> <style> /* --- Global Styles & Resets --- */ *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; background-color: #f4f4f4; color: #333; } .container { max-width: 1200px; margin: 0 auto; padding: 0 1rem; } img { max-width: 100%; display: block; } a { text-decoration: none; color: #007bff; } ul { list-style: none; } /* --- Header & Navigation Bar --- */ .site-header { background-color: #ffffff; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); position: relative; /* Needed for mega menu positioning */ z-index: 1000; } .navbar { display: flex; justify-content: space-between; align-items: center; height: 70px; } .nav-logo { font-size: 1.5rem; font-weight: bold; color: #333; } .nav-menu { display: flex; gap: 1rem; } .nav-menu a { display: block; padding: 1rem; color: #555; font-weight: 500; transition: color 0.3s ease, background-color 0.3s ease; } .nav-menu a:hover, .nav-menu a:focus { color: #0056b3; background-color: #f0f0f0; } /* --- Mega Menu (Desktop) --- */ .menu-item-has-children { position: relative; /* Parent for absolute positioning */ } .mega-menu { /* Hiding the menu by default */ visibility: hidden; opacity: 0; transform: translateY(10px); transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s; /* Positioning and Layout */ position: absolute; top: 100%; left: 50%; /* Center relative to the parent li */ transform: translateX(-50%); width: 800px; max-width: 100vw; background-color: #fff; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15); border-radius: 8px; padding: 2rem; display: grid; grid-template-columns: repeat(3, 1fr); gap: 2rem; } /* Show the menu on hover or when a child element has focus */ .menu-item-has-children:hover > .mega-menu, .menu-item-has-children:focus-within > .mega-menu { visibility: visible; opacity: 1; transform: translateY(0) translateX(-50%); } /* Note on aria-expanded: We can't change the attribute with CSS. JavaScript would be needed to toggle aria-expanded="true" on hover/focus. However, :focus-within provides the required keyboard functionality. */ .mega-menu-column { display: flex; flex-direction: column; } .mega-menu-column h3 { font-size: 1.1rem; color: #333; margin-bottom: 1rem; border-bottom: 2px solid #007bff; padding-bottom: 0.5rem; } .mega-menu-column ul li a { padding: 0.5rem 0; /* Simpler padding for mega menu links */ } .mega-menu-column ul li a:hover, .mega-menu-column ul li a:focus { background-color: transparent; /* Override main nav hover */ color: #0056b3; text-decoration: underline; } /* Featured Product Column Styles */ .featured-product { background-color: #f9f9f9; padding: 1.5rem; border-radius: 6px; text-align: center; } .featured-product img { margin-bottom: 1rem; border-radius: 4px; } .featured-product h4 { margin-bottom: 0.5rem; font-size: 1.1rem; } .featured-product p { font-size: 0.9rem; color: #666; margin-bottom: 1.5rem; } .btn-featured { background-color: #007bff; color: #fff !important; /* Override link color */ padding: 0.75rem 1.5rem !important; /* Override link padding */ border-radius: 5px; transition: background-color 0.3s ease; } .btn-featured:hover, .btn-featured:focus { background-color: #0056b3 !important; text-decoration: none !important; } /* --- Mobile Menu Styles (Hamburger) --- */ .nav-toggle, .nav-toggle-label { display: none; /* Hide on desktop */ } /* Main Content styling for demonstration */ main.container { padding-top: 2rem; padding-bottom: 2rem; } /* --- Responsive Styles (Tablet & Mobile) --- */ @media (max-width: 992px) { .mega-menu { width: 600px; grid-template-columns: repeat(2, 1fr); } .featured-product { grid-column: span 2; /* Make featured take full width */ } } @media (max-width: 768px) { /* Show hamburger toggle */ .nav-toggle-label { display: flex; flex-direction: column; justify-content: space-around; width: 30px; height: 25px; cursor: pointer; z-index: 1001; } .nav-toggle-label span { display: block; width: 100%; height: 3px; background-color: #333; border-radius: 3px; transition: transform 0.3s ease, opacity 0.3s ease; } /* Hide checkbox */ .nav-toggle { display: none; } /* Hide the desktop menu and prepare for mobile layout */ .nav-menu { position: fixed; top: 0; right: 0; width: 80%; max-width: 320px; height: 100vh; background-color: #fff; flex-direction: column; align-items: flex-start; padding: 4rem 2rem 2rem; gap: 0; transform: translateX(100%); transition: transform 0.3s ease-in-out; box-shadow: -5px 0 15px rgba(0,0,0,0.1); } /* Show the mobile menu when checkbox is checked */ .nav-toggle:checked ~ .nav-menu { transform: translateX(0); } /* Animate hamburger icon to an "X" */ .nav-toggle:checked ~ .nav-toggle-label span:nth-child(1) { transform: translateY(9px) rotate(45deg); } .nav-toggle:checked ~ .nav-toggle-label span:nth-child(2) { opacity: 0; } .nav-toggle:checked ~ .nav-toggle-label span:nth-child(3) { transform: translateY(-9px) rotate(-45deg); } /* Reset mega menu to a simple dropdown on mobile */ .mega-menu { position: static; width: 100%; transform: none !important; /* Override desktop transform */ opacity: 1; visibility: visible; background-color: transparent; box-shadow: none; padding: 0 0 0 1rem; display: none; /* Hide sub-menu by default on mobile */ grid-template-columns: 1fr; /* Single column layout */ } /* Show sub-menu on focus (tap on mobile) */ .menu-item-has-children:focus-within > .mega-menu { display: grid; /* Show as a grid to respect gap */ } .mega-menu-column { padding: 0; } .featured-product { display: none; /* Hide featured product on mobile for simplicity */ } } </style> </head> <body> <header class="site-header"> <div class="container"> <nav class="navbar"> <!-- Logo --> <a href="#" class="nav-logo">CompanyLogo</a> <!-- Mobile Menu Toggle (Checkbox Hack) --> <input type="checkbox" id="nav-toggle" class="nav-toggle"> <label for="nav-toggle" class="nav-toggle-label" aria-label="Open menu"> <span></span> <span></span> <span></span> </label> <!-- Navigation Menu --> <ul class="nav-menu"> <li><a href="#">Home</a></li> <!-- Item with Mega Menu --> <li class="menu-item-has-children"> <a href="#" aria-haspopup="true" aria-expanded="false">Products</a> <!-- Mega Menu Panel --> <div class="mega-menu"> <div class="mega-menu-column"> <h3>For Individuals</h3> <ul> <li><a href="#">Personal Plan</a></li> <li><a href="#">Creator Suite</a></li> <li><a href="#">Individual Pro</a></li> </ul> </div> <div class="mega-menu-column"> <h3>For Teams</h3> <ul> <li><a href="#">Team Starter</a></li> <li><a href="#">Business Class</a></li> <li><a href="#">Enterprise Solution</a></li> </ul> </div> <div class="mega-menu-column featured-product"> <img src="https://via.placeholder.com/250x150.png?text=Featured+Product" alt="A placeholder image for a featured product."> <h4>Spotlight: Product X</h4> <p>A groundbreaking new tool to revolutionize your team's productivity.</p> <a href="#" class="btn-featured">Learn More</a> </div> </div> </li> <li><a href="#">Solutions</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </div> </header> <main class="container"> <h1>Page Content</h1> <p>This is the main content area of the page, demonstrating that the mega menu appears over the content without pushing it down.</p> <p>Try resizing your browser window to see the mobile "hamburger" menu appear.</p> <p>Use the Tab key to navigate the menu for an accessible experience.</p> </main> </body> </html>