Learn how to create Smooth Slide-in Sidebar Section with custom CSS and jQuery!
A sidebar section is a great and useful tool but it can also distract the user from the main content of your page. Especially when it takes a lot of the website’s space.
That’s why in this tutorial, I will show you how to use Divi Builder to create a slide-in sidebar that will show up only after you click on the button.
We are going to use the custom CSS and some jQuery code to achieve that. Watch my step-by-step walkthrough video and copy the code we used below!
If you’re our subscriber feel free to download the JSON file!
Free Resources for Divi Lovers
Join our newsletter to get the good stuff!
We’ll provide you with updates on new tutorials, webdesign assets and special offers. Get top quality Divi goodies straight into your inbox! Read our Privacy Policy to learn how we manage and protect your submitted information.
Sign up to download
Get access to this layout and all the free resources made exclusively for our subscribers!
Already subscribed?
Fill the signup form and if you’re on the list it’ll get you straight to the download page!
Can’t see the form? Click here
Step 1. Add a section in the Divi Builder
In the Advanced tab of the section setting, we need to change the position of the sidebar section to “Fixed”. We also need to include a custom CSS class. The CSS class used in the demo example is called dl-slide-sidebar.
The max-width of the section is set to 300px and if there is a lot of content, make sure to change the horizontal overflow value to “scroll” (in the Advanced tab of the section settings).
Step 2. Add the Close Button inside the Sidebar
To create an X close button we used the Blurb module added in the sidebar section. The module does not have a title, nor any content. We enabled the icon and changed the width, height, and the icon size to 50px. To make sure the sidebar section slides out upon clicking on the Blurb – we’ve added a custom CSS class of dl-close so that we can target this element in our JavaScript code.
Step 3. Add the Button trigger
We are using a standard Divi Button with a custom CSS class of dl-btn-toggle. The button text will be changed dynamically, but on the page load it is set to “Open”.
Add the CSS Styles!
Here is the CSS code we used to hide the sidebar only if not in the Visual Builder and to show it when it gets a new CSS class of is-opened. The “translateX” value needs to match the section width. The last bit adds a hand cursor to the Blurb module:
body:not(.et-fb) .dl-slide-sidebar {
transform:translateX(300px);
}
body:not(.et-fb) .dl-slide-sidebar.is-opened {
transform:translateX(0);
}
.dl-slide-sidebar.has-transition {
transition:.7s;
}
.dl-close {
cursor:pointer;
}
Finally, the jQuery magic!
This is the final jQuery code. To understand what it does – please watch the video 😉
jQuery(document).ready(function($) {
var dl_ButtonToggle = $('.dl-btn-toggle');
var dl_SlideSidebar = $('.dl-slide-sidebar');
function dl_SidebarCheck() {
if (dl_ButtonToggle.hasClass('is-opened')) {
dl_ButtonToggle.text('Close');
}
else {
dl_ButtonToggle.text('Open');
}
}
dl_ButtonToggle.click(function(e) {
e.preventDefault();
dl_ButtonToggle.toggleClass('is-opened');
dl_SlideSidebar.toggleClass('is-opened');
dl_SlideSidebar.addClass('has-transition');
dl_SidebarCheck();
})
$('.dl-close').click(function() {
$('.is-opened').removeClass('is-opened');
dl_SidebarCheck();
})
});
What do you think?
Feel free to use it on your website and do not hesitate to show us the final result! We are eager to see the effects of your work!
We always appreciate your feedback, so please share your opinion with us in the comments!
I’m happy that I found this, though I am curious if there’s a way to have the content width adjust based on the sidebar being opened or closed.
I was able to make some changes so that it shows open by default in a sidebar taking 15% of the page width, with the main content set to 85%
I am trying to find a way to make it so that when the section closes the main content fills up the whitespace.
Aniu a czy da się wykonać przycisk aby zamiast przycisku z tekstem był moduł “Icon” np. ikonka hamburgera, a po kliknięciu zamienia się na ikonkę krzyżyka?
Could you tell me how to make the menu close when you click outside of it?
How would I alter the CSS to also hide it in the visual builder (since it overlays the content – and is a distraction) I would just use the widreframe mode to edit it.
You can remove the
body:not(.et-fb)
parent selector from the CSS (the sidebar will be moved outside of the viewport, the same as on the front-end).I’m trying to trigger this side menu from a main wordpress menu item, I add the css class “dl-btn-toggle” to the wordpress menu item css and it works on desktop but not on mobile…would the jquery code need to change to allow a touch/tap of a finger on to trigger it?
Hi John,
Yes, the jQuery code would need to be changed. I used the dl-menu-toggle CSS class for the menu item and this bit of code:
You can see this working here.
Ania, I’m just stuck. I was hoping to use an icon that looks like a hamburger menu to create a menu that’s not really a menu. I really wanted a slide-in menu – but I also wanted OTHER items on the header. And Divi didn’t really seem to be able to do that. For example, a logo on the left, a schedule button, 2 social shares, and then a hamburger menu that would push everything over to open a slide in.
If I could make it do it on hover, that would be cool, but clicking will work.
Unfortunately, I’m just getting lost. I used an icon module instead of a button. But I did everything else the way you did. It hides it – but when I click the icon, nothing happens. I changed all of the words that said button or btn in the code to icon, but it isn’t working.
Hey Ania! What an amazing tutorial! One of the best I’ve seen : ) Everything seems to be working on my end.
But I would like to have multiple buttons on my webpage. Each button would open a *different* sidebar.
I have therefore given a a CSS class name to each button and sidebar. Like this:
dl-slide-sidebar-2
dl-btn-toggle-2
dl-slide-sidebar-3
dl-btn-toggle-3
etc.
How can I change both the CSS *and* Jquery codes to make it work for all my buttons and sidebars? Without having to repeat the whole code multiple times.
(I’m new to all of this)
Thank you for your help Ania! And congrats again on this wonderful tutorial : )
Thanks for this tutorial! Excellent! My need is related to this slider and I have gotten close based on your ideas. Perhaps you can give guidance to my slight differences: slider has images next to text; want to show slider on page load; when scrolling down, want to shrink slider to just show images until hover (when we expand again)…. This page is a model: https://www.allegramarketingprint.com/locations/franklin-ma-downtown
Works great, thank you! I have built a menu with it. Maybe you can give me a hint:
How could i achieve to close that opened section by clicking somewhere outside?
Hi it looks great but using the theme builder for the header and footer the sidebar is cut off so you can’t close it at all. Is there a way to make it slide over the header and footer?
Thanks
Hi Terry!
Please try adding this bit of CSS to make sure the Body template is on top of the Header and Footer templates:
Hopefully, this helps!
Great tutorial! I would like to do exactly this, but I would like push the page to the main elements on the page to the left and resize the items (like you are making your browser window smaller). I hope I’m explaining that correctly.
For example, when you click on the Open button, the menu would come out and shift all of the page elements to the left.
Hi Ania
Love your work, thanks for this.
Do you think it would be possible to slide it out after a certain time using a conditional?
Say 5 seconds after the page has loaded it slides out, it’s much less intrusive than a popup.
For mobile it would probably need a different way.
Thanks
Thank you, Stoan 🙂
You might want to look at this tutorial and combain the two methods together..
Hi Ania. Just downloaded and implemented the code from your freebie for which many thanks. It works so I can check where I went wrong.
This is exactly what I want and your explanations are great however while having checked multiple times that I have followed you and have copied the code directly from this site, clicking the button gives
“Uncaught ReferenceError: dl_Buttontoggle is not defined
at dl_SidebarCheck ((index):111)”
Any assistance greatly appreciated thank you.
Ania please please please share how can we turn display: none before click and return to regular default display value using your code. Could you please share a snippet i can add to the above code.
Thanks a bunch in advance. hope you actually answer. Awaiting your reply.
what i mean to say is display: none; before any click or trigger and then return to default display value after click.
Then we also need it to revert to display: none; when the close icon is clicked.
Awaiting your reply.
Your tuts is very good but then i find this to be the only caveat in your code.
I’m sorry, but I do not understand what it is you are trying to achieve. If you click the close icon, it closes the panel – not sure what do you mean by “need it to revert display:none”.
How can we have the sidebar close if anywhere outside of it is clicked?
Hi! I would like to make Slide-in Sidebar Section fixed on the uper part of my page. I did it but I have issue when it opens because it covers my menu. Could you help me? What extra code I should use?
Hello i arrive to the jquerry, then i dont know how to continue.
Can you send me the download link?
Thanks
Hi,
How can I display hamburger icon instead of ‘Open’ and ‘Close’
Please help.
Thanks
Ania, I love your tutorial style, its simple easy to follow and as always very useful!
Thank you so much for making the effort to give us these free snippets!
Perfecto! i learn jQuery but since then i don’t know how to use in Divi. LOL
So i easily understand your step by step guide throughout this videos.