04. The Navigation

Build in the overlay navigation

Mentor Morrigan

Your Task

Make sure to link the HTML, CSS, and JavaScript correctly.
Check whether the navigation actually works.

HTML
<button id="openNav" class="open-btn">☰ Open Navigation</button>

<!-- Fullscreen Overlay-Navigation -->
<div id="myNav" class="overlay">
    <a href="javascript:void(0)" class="closebtn">×</a>
    <div class="overlay-content">
        <a href="#" class="closeNav">Home</a>
        <a href="#" class="closeNav">Services</a>
        <a href="#" class="closeNav">About</a>
        <a href="#" class="closeNav">Contact</a>
    </div>
</div>
CSS
/* The navigation covers the entire screen width, but initially starts with a width of 0 */
.overlay {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.9);
    overflow-x: hidden;
    transition: width 0.5s ease;
}

/* The overlay content remains centered */
.overlay-content {
    position: relative;
    top: 50%;
    width: 100%;
    text-align: center;
    transform: translateY(-50%);
}

.overlay-content a {
    padding: 15px;
    text-decoration: none;
    font-size: 30px;
    color: white;
    display: block;
    transition: color 0.3s;
}

.overlay-content a:hover {
    color: coral;
}

/* Close button */
.closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
    color: white;
    text-decoration: none;
}

/* The button to open */
.open-btn {
    font-size: 20px;
    padding: 10px 20px;
    background-color: coral;
    color: white;
    border: none;
    cursor: pointer;
}
JavaScript
// JavaScript
(function () {
    // Open the navigation
    document.getElementById('openNav').addEventListener('click', () => {
        document.getElementById('myNav').style.width = '100%';
    });

    // Close the navigation when clicking on a link or the close button
    const closeNavBtns = document.querySelectorAll('.closeNav, .closebtn');
    closeNavBtns.forEach(element => {
        element.addEventListener('click', () => {
            document.getElementById('myNav').style.width = '0%';
        });
    });
})();

Use the Material Design icon to open the overlay navigation.

Inside the overlay navigation, create the following anchors (and link names):

  1. hero-section: Home
  2. event: Event
  3. about: The Flame
  4. news: News from Aetheron
  5. enemies: Progress
  6. heroes: Our Heroes
  7. contact: Contact Us
  8. gallery: Gallery

Solve the task here in the console [–> Open in a new tab]

Scroll to Top