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!
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.