How to Create: Fullscreen Overlay Navigation
- Previous Page Responsive Sidebar
- Next Page Canvas Outside Menu
Learn how to create a fullscreen overlay navigation menu.
Click the button below to see how it works:
Create Fullscreen Overlay Navigation
Step 1 - Add HTML:
<!-- 覆盖层 --> <div id="myNav" class="overlay"> <!-- 可关闭叠加导航的按钮 --> <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a> <!-- Overlay content --> <div class="overlay-content"> <a href="#">About</a> <a href="#">Services</a> <a href="#">Clients</a> <a href="#">Contact</a> </div> </div> /* Use any element to open/display the overlay navigation menu */ <span onclick="openNav()">open</span>
Second step - Add CSS:
/* Overlay (background) */ .overlay { /* Height and width depend on how you want to display the overlay (see the JS below) */ height: 100%; width: 0; position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ left: 0; top: 0; background-color: rgb(0,0,0); /* Alternate black */ background-color: rgba(0,0,0, 0.9); /* Semi-transparent black */ overflow-x: hidden; /* Disable horizontal scrolling */ transition: 0.5s; /* 0.5 second transition effect, to slide in or slide down the overlay (height or width depends on the display method) */ } /* Position the content inside the overlay */ .overlay-content { position: relative; top: 25%; /* 25% from the top */ width: 100%; /* 100% width */ text-align: center; /* Center the text/link */ margin-top: 30px; /* Top outer margin of 30 pixels to avoid conflict with the close button on small screens */ } /* Navigation links within the overlay */ .overlay a { padding: 8px; text-decoration: none; font-size: 36px; color: #818181; display: block; /* Display as a block instead of inline */ transition: 0.3s; /* Transition effect (color) when the mouse hovers */ } /* Change the color when the mouse hovers over the navigation link */ .overlay a:hover, .overlay a:focus { color: #f1f1f1; } /* Place the close button (top right corner) */ .overlay .closebtn { position: absolute; top: 20px; right: 45px; font-size: 60px; } When the screen height is less than 450 pixels, change the link font size and reposition the close button to avoid them overlapping /* Change the link font size and reposition the close button when the screen height is less than 450 pixels, so that they do not overlap */ @media screen and (max-height: 450px) { .overlay a {font-size: 20px} .overlay .closebtn { font-size: 40px; top: 15px; right: 35px; } }
Step 3 - Add JavaScript:
In the following example, when triggered, the overlay navigation menu slides in from left to right (from 0 to 100% width):
Slide in from the side
/* Open when someone clicks the span element */ function openNav() { document.getElementById("myNav").style.width = "100%"; } /* Close when someone clicks the "x" symbol within the overlay */ function closeNav() { document.getElementById("myNav").style.width = "0%"; }
In the following example, the overlay navigation menu slides down from the top (from 0 to 100% height).
Note:In this example, note that the CSS is slightly different from the above example (the default height is now 0, the width is 100%, and overflow-y is hidden (disabled vertical scrolling, except for small screens)):
Slide down from the top
/* Open */ function openNav() { document.getElementById("myNav").style.height = "100%"; } /* Close */ function closeNav() { document.getElementById("myNav").style.height = "0%"; }
This example does not use animation when opening the navigation menu:
No animation to open the menu
/* Open */ function openNav() { document.getElementById("myNav").style.display = "block"; } /* Close */ function closeNav() { document.getElementById("myNav").style.display = "none"; }
Related Pages
Tutorial:CSS Navigation Bar
- Previous Page Responsive Sidebar
- Next Page Canvas Outside Menu