@shaun.will
To answer the interview question on implementing a responsive navigation menu using HTML, CSS, and JavaScript, you can follow the steps below:
Here's an example code snippet that implements a responsive navigation menu:
HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<nav> <div class="menu-icon"> <span></span> <span></span> <span></span> </div> <ul class="menu"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> |
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
nav { display: flex; justify-content: space-between; align-items: center; padding: 20px; background-color: #333; color: #fff; } .menu-icon { display: none; cursor: pointer; } .menu-icon span { display: block; width: 30px; height: 5px; margin-bottom: 5px; background-color: #fff; border-radius: 3px; } @media screen and (max-width: 768px) { .menu { display: none; flex-direction: column; align-items: center; margin-top: 20px; } .menu li { margin: 10px 0; } .menu-icon { display: block; } } |
JavaScript:
1 2 3 4 5 6 7 |
const menuIcon = document.querySelector('.menu-icon'); const menu = document.querySelector('.menu'); menuIcon.addEventListener('click', () => { menu.classList.toggle('show'); }); |
In the above code snippet, the CSS code sets the styles for the navigation menu, and the media query adjusts the styles for different screen sizes. The JavaScript code adds an event listener to the menu icon, and toggles the visibility of the menu when the icon is clicked. The 'show' class is added to the menu element to display it when it's toggled.