In this tutorial I'll show you how to create a button that lets users show/hide the Divi navigation bar with a click. This feature is great for making your website more user-friendly, especially for visitors who want to focus on content without distractions.
Table of Contents
- Demo
- Why Would You Need To Toggle The Navigation Bar?
- How It Works
- Header Toggle Button Implementation
- Step 1. Create The Header Toggle Button
- Step 2. Position And Style The Header Toggle Button
- Step 3. Add Icons To The Header Toggle Button
- Step 4. Remove Default Spacing From The Top Of The Page
- Step 5. Show/Hide The Primary And Secondary Headers Smoothly
- Step 6. Make Divi Header Fixed On Mobile
- Step 7. Apply Toggle Button Adjustments For Mobile
- Step 8. Combine All CSS Snippets
- Conclusion
Demo
Why Would You Need To Toggle The Navigation Bar?
Adding a toggle button for the navigation bar can offer several practical benefits, I'd like to highlight the followings:
- Enhanced Focus on Content.
On content-heavy pages, such as blogs, portfolios, or articles, users might prefer a distraction-free environment. Hiding the navigation bar can allow visitors to focus solely on the content without unnecessary elements cluttering the screen. - Improved Mobile Experience
On mobile devices, screen real estate is limited. A toggle button can help users hide the navigation bar to maximize their viewable content area. - Sticky Navigation Alternative.
Instead of having a sticky navigation bar that takes up space as users scroll down, a toggle button offers an alternative. Users can access the navigation only when needed without it being visible constantly. - Customization and User Control.
Giving users control over their browsing experience enhances usability. A toggle button lets visitors customize the layout to suit their preferences, making the website more user-friendly and engaging.
How It Works
The headaer toggle button will always stay visible at the top of the page, right where the header bar is. This way, users will be able to easily click it to hide the header when they don't need it and bring it back when they do.
The header bar is hidden on initial page load and only the toggle button is visible. It has the "MENU" label and the hamburger menu icon for the closed state of the header. This lets the site visitor know that clicking the MENU button will show the menu of this website.
When users click the MENU button, the header bar slides in and the hamburger menu icon changes to X icon. This change signals to users that a second click will hide the header bar.
Note that both the MENU label and the toggle button icons can be changed if you need to. You can do that in the JS and CSS snippets provided below.
Header Toggle Button Implementation
We'll implement this header toggle button for the Default, Centered and Centered Inline Logo header styles, both for desktop and mobile menus.
To implement this functionality we will use CSS and jQuery.
The toggle will be added to the page container using a simple jQuery script. And then we'll position and style it using custom CSS.
So, let's do that following simple steps below.
Step 1. Create The Header Toggle Button
First let's add the following jQuery code in Divi Theme Options -> Integration.
<script type="text/javascript">
(function($){
/**
* Add the toggle button to show/hide the
* navigation bar on click.
*
* @source https://divicio.us
*/
function dvcs_show_hide_horizontal_menu() {
/* add the header toggle to page container */
$('#page-container').prepend('<span class="dvcs_nav_toggle">MENU</span>');
/* toggle class when clicking the menu icon */
$("#page-container .dvcs_nav_toggle").on('click', function(event) {
$(this).parents('body').toggleClass('dvcs_nav_open');
/* close the mobile menu dropdown */
$('.mobile_nav.opened .mobile_menu_bar_toggle').trigger('click');
});
}
$(document).ready(function() {
dvcs_show_hide_horizontal_menu();
});
})(jQuery);
</script>
It adds the header toggle button with the 'MENU' label to the page container, and toggles the dvcs_nav_open CSS class on the body element on click. This CSS class is used to actually show and hide the navigation bar using CSS.
Also this script triggers the click event on the mobile menu hamburger icon if the dropdown menu is open, to close it when hiding the header bar. This is needed so that the dropdown menu does not remain opened when the header bar has already been moved out of the viewport.
Step 2. Position And Style The Header Toggle Button
Now that we’ve added the menu toggle to the page, we need to position it properly and apply styling.
The following CSS will position the toggle button at the top-right corner of the viewport and style it to look like a button.
To ensure it remains visible as visitors scroll the page we’ll use fixed positioning. This allows users to show or hide the navigation bar whenever they need to.
/* header toggle */
.dvcs_nav_toggle {
display: block;
position: fixed;
top: 0px;
left: auto;
padding: 2px 8px 0px;
right: 5%;
font-size: 12px;
font-weight: 700;
background: #617be5;
color: #ffffff;
border-radius: 0 0 5px 5px;
box-shadow: 0 2px 15px 1px rgba(0, 0, 0, 0.1);
z-index: 999999;
}
Please note that the button styling is for demonstration purposes only. Be sure to customize it to match the overall design of your website.
Step 3. Add Icons To The Header Toggle Button
Next, we’ll use this CSS to add icons to the toggle button.
For the closed state of the navigation bar we use the hamburger menu icon. Whereas for the open state we'll switch to the X icon.
/* header toggle icon */
.dvcs_nav_toggle:after {
display: block;
position: relative;
text-shadow: 0 0;
font-family: ETmodules !important;
font-weight: 400;
font-style: normal;
font-variant: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
line-height: 1;
text-transform: none;
font-size: 40px;
content: "\61";
cursor: pointer;
top: -7px;
margin-bottom: -10px;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
}
/* header toggle icon for visible header bar */
.dvcs_nav_open .dvcs_nav_toggle:after {
content: "\4d";
}
Step 4. Remove Default Spacing From The Top Of The Page
By default, Divi adds top padding to the page container to prevent the fixed header from overlapping the content.
However, since we're hiding the fixed header, that space is no longer needed.
We can remove it using the following CSS:
/* remove the page container top padding */
#page-container {
padding-top: 0px !important;
}
Step 5. Show/Hide The Primary And Secondary Headers Smoothly
This CSS snippet is the one that is actually showing and hiding the navigation bar as the dvcs_nav_open CSS class is being toggled on the body element.
If the dvcs_nav_open CSS calss is added then the we show the navigation bar. Otherwise, we hide it.
/* hide main and secondary headers */
#page-container #top-header,
#page-container #main-header {
-webkit-transform: translateY(-100%);
-ms-transform: translateY(-100%);
transform: translateY(-100%);
}
/* hidden main header bar top offset */
#page-container #main-header {
top: 0 !important;
}
/* show both main and secondary headers */
.dvcs_nav_open #page-container #main-header,
.dvcs_nav_open #page-container #top-header {
-webkit-transform: translateY(0);
-ms-transform: translateY(0);
transform: translateY(0);
}
/* open main header top offset */
.dvcs_nav_open.et_secondary_nav_enabled #page-container #main-header {
top: 31px !important;
}
/* transitions */
#page-container #top-header,
#page-container #main-header,
.dvcs_nav_toggle {
-webkit-transition: transform 0.5s ease;
-o-transition: transform 0.5s ease;
transition: transform 0.5s ease;
}
Additionally, we've also applied transitions so that the navigation bar shows/hides smoothly.
Step 6. Make Divi Header Fixed On Mobile
For the toggle button to function correctly we need to enable the fixed header. You can do this by turning on the Fixed Navigation Bar option in Divi Theme Options -> General.
However, this option only applies to the desktop header by default. So, let's use the following simple CSS to make Divi header fixed on mobile too:
@media all and (max-width: 980px) {
/* make mobile header fixed */
#main-header,
#top-header {
position: fixed !important;
}
}
If you'd like to further customize Divi mobile menu bar, you might want to check out this tutorial.
Step 7. Apply Toggle Button Adjustments For Mobile
Finally, we need to adjust the toggle button styling for mobile, including its positioning, spacing, and font size.
We’ll also use arrow icons that point up or down, depending on whether the navigation bar is open or closed. However, this is entirely optional—you can choose to stick with the hamburger menu and X icons on mobile too if you prefer.
/* adjustments for mobile header toggle */
@media all and (max-width: 980px) {
/* mobile header toggle */
.dvcs_nav_toggle {
padding: 3px 8px 0px;
right: 8%;
}
/* mobile header toggle icon */
.dvcs_nav_toggle:after {
font-size: 28px;
content: "\33";
top: -9px;
-webkit-transform: translateX(-40%);
-ms-transform: translateX(-40%);
transform: translateX(-40%);
}
/* mobile header toggle icon the visible header bar */
.dvcs_nav_open .dvcs_nav_toggle:after {
content: "\32";
}
}
Step 8. Combine All CSS Snippets
Now, let's combine all of the CSS snippets from previous steps and add into the Divi Theme Options -> General -> Custom CSS field (or into the child theme style.css file).
/**
* Show/Hide Divi Navigation Bar on Click
* @source https://divicio.us
*/
/* header toggle */
.dvcs_nav_toggle {
display: block;
position: fixed;
top: 0px;
left: auto;
padding: 2px 8px 0px;
right: 5%;
font-size: 12px;
font-weight: 700;
background: #617be5;
color: #ffffff;
border-radius: 0 0 5px 5px;
box-shadow: 0 2px 15px 1px rgba(0, 0, 0, 0.1);
z-index: 999999;
}
/* header toggle icon */
.dvcs_nav_toggle:after {
display: block;
position: relative;
text-shadow: 0 0;
font-family: ETmodules !important;
font-weight: 400;
font-style: normal;
font-variant: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
line-height: 1;
text-transform: none;
font-size: 40px;
content: "\61";
cursor: pointer;
top: -7px;
margin-bottom: -10px;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
}
/* header toggle icon for visible header bar */
.dvcs_nav_open .dvcs_nav_toggle:after {
content: "\4d";
}
/* remove the page container top padding */
#page-container {
padding-top: 0px !important;
}
/* hide main and secondary headers */
#page-container #top-header,
#page-container #main-header {
-webkit-transform: translateY(-100%);
-ms-transform: translateY(-100%);
transform: translateY(-100%);
}
/* hidden main header bar top offset */
#page-container #main-header {
top: 0 !important;
}
/* show both main and secondary headers */
.dvcs_nav_open #page-container #main-header,
.dvcs_nav_open #page-container #top-header {
-webkit-transform: translateY(0);
-ms-transform: translateY(0);
transform: translateY(0);
}
/* open main header top offset */
.dvcs_nav_open.et_secondary_nav_enabled #page-container #main-header {
top: 31px !important;
}
/* transitions */
#page-container #top-header,
#page-container #main-header,
.dvcs_nav_toggle {
-webkit-transition: transform 0.5s ease;
-o-transition: transform 0.5s ease;
transition: transform 0.5s ease;
}
/* adjustments for mobile header toggle */
@media all and (max-width: 980px) {
/* make mobile header fixed */
#main-header,
#top-header {
position: fixed !important;
}
/* mobile header toggle */
.dvcs_nav_toggle {
padding: 3px 8px 0px;
right: 8%;
}
/* mobile header toggle icon */
.dvcs_nav_toggle:after {
font-size: 28px;
content: "\33";
top: -9px;
-webkit-transform: translateX(-40%);
-ms-transform: translateX(-40%);
transform: translateX(-40%);
}
/* mobile header toggle icon the visible header bar */
.dvcs_nav_open .dvcs_nav_toggle:after {
content: "\32";
}
}
/* END: Show/Hide Divi Navigation Bar on Click */
Save everything, and that's it—you've just created a toggle button to show and hide the Divi navigation bar with a click. Enjoy!
Conclusion
In this tutorial, we've walked through the process of creating a toggle button that allows users to show/hide the Divi navigation bar with a click.
This small but powerful feature enhances the user experience by offering greater control and flexibility, making your website more dynamic and user-friendly.
Feel free to experiment with further customizations to match your website’s style and needs. Your users' experience is at the heart of every design choice, and giving them control over their browsing environment is a great way to keep them engaged and satisfied.
I'd love to hear your thoughts on this tutorial in the comments section below. Feel free to share your feedback, ask questions, or simply let me know how this feature is working for you.
If you enjoyed this tutorial and want to stay updated with more Divi tips, tricks, and tutorials like this, be sure to subscribe to our newsletter. By subscribing, you'll receive the latest updates right in your inbox, helping you stay ahead in optimizing your Divi-powered website.
Continue Reading: Show/Hide Divi Header Depending on Page Scroll Direction






Really cool feature :-) I like it…
Any idea how to do the same with a vertical menu (slide in) ?
All my best…
Hi Philippe.
Check out this tutorial: Show and Hide Divi Vertical Navigation on Click
Hope this helps.
very helpful thanks a million…
awesome! will use in my nxt project