How to Create: Curtain Menu
- Previous Page Mobile Menu
- Next Page Collapsible Sidebar
Learn how to create a curtain navigation menu.
Click the button below to see how it works:
Create a Curtain Menu
Step 1 - Add HTML:
<!-- Overlay --> <div id="myNav" class="overlay"> <!-- Close overlay navigation button --> <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 following JS) */ height: 100%; width: 0; position: fixed; /* Keep in place */ z-index: 1; /* Place on top */ left: 0; top: 0; background-color: rgb(0,0,0); /* Alternative black color (backup color) */ background-color: rgba(0,0,0, 0.9); /* Black with transparency */ overflow-x: hidden; /* Disable horizontal scrolling */ transition: 0.5s; /* 0.5 second transition effect to slide in or out the overlay (height or width, depending on the display method) */ } /* Place the content inside the overlay */ .overlay-content { position: relative; top: 25%; /* 25% from the top */ width: 100%; /* 100% width */ text-align: center; /* Centered text/links */ margin-top: 30px; /* Top outer margin of 30px to avoid conflict with the close button on small screens */ } /* Cover layer navigation links */ .overlay a { padding: 8px; text-decoration: none; font-size: 36px; color: #818181; display: block; /* Display as a block rather than inline */ transition: 0.3s; /* Transition effect (color) when the mouse hovers */ } /* When the mouse hovers over the navigation link, change its color */ .overlay a:hover, .overlay a:focus { color: #f1f1f1; } /* Position the close button (top right corner) */ .overlay .closebtn { position: absolute; top: 20px; right: 45px; font-size: 60px; } /* Change the link font size and reposition the close button when the screen height is less than 450 pixels to avoid 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 will slide in from left to right (from 0 to 100% width):
Slide in from the side
/* Open when the user clicks the <span> element */ function openNav() { document.getElementById("myNav").style.width = "100%"; } /* Close when the user clicks the "x" symbol within the overlay */ function closeNav() { document.getElementById("myNav").style.width = "0%"; }
The following example will slide the overlay navigation menu down from the top (from 0 to 100% height).
Note:In this example, please note that the CSS is slightly different from the above examples (the default height is now 0, the width is 100%, and overflow-y
For hidden
(Disable 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 opens a navigation menu without animation effects:
Open menu without animation
/* 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 Mobile Menu
- Next Page Collapsible Sidebar