Slide-in Sidebar Section in the Divi theme

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! 

Have you seen our

Divi child themes?

Divi Toolbox plugin?

We offer a great selection of high-quality Divi products to speed up your work. Feel free to use a coupon code IREADBLOG at the checkout to get a 10% discount!

28 Comments

  1. Kyle

    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.

    Reply
  2. Paweł

    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?

    Reply
  3. Rick Elrod

    Could you tell me how to make the menu close when you click outside of it?

    Reply
  4. John

    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.

    Reply
    • Ania Romańska

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

      Reply
  5. John

    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?

    Reply
    • Ania Romańska

      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:

      jQuery(document).ready(function($) {
      		var dl_MenuToggle = $('.dl-menu-toggle a');
        	var dl_SlideSidebar = $('.dl-slide-sidebar');
        	
        	function dl_SidebarCheck() {
            if (dl_MenuToggle.hasClass('is-opened')) {
              dl_MenuToggle.text('Close');
            }
            else {
              dl_MenuToggle.text('Open');
            }
          }
        
        
        	dl_MenuToggle.off('touchend');
          $(document).on('touchend click', '.dl-menu-toggle a' , function(e){
            e.preventDefault();
            dl_MenuToggle.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();
          })
      });

      You can see this working here.

      Reply
  6. Kristine Schroeder

    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.

    Reply
  7. Charles

    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 : )

    Reply
  8. Richard Frongillo

    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

    Reply
  9. David

    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?

    Reply
  10. Terry

    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

    Reply
    • Ania Romańska

      Hi Terry!
      Please try adding this bit of CSS to make sure the Body template is on top of the Header and Footer templates:

      .et-l--post .et_builder_inner_content {
          z-index: 2;
      }

      Hopefully, this helps!

      Reply
  11. Joel

    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.

    Reply
    • Joel

      For example, when you click on the Open button, the menu would come out and shift all of the page elements to the left.

      Reply
  12. Stoan

    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

    Reply
    • Ania Romańska

      Thank you, Stoan 🙂
      You might want to look at this tutorial and combain the two methods together..

      Reply
  13. Murray Russell

    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.

    Reply
  14. Murray Russell

    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.

    Reply
  15. Abhinav

    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.

    Reply
    • Abhinav bhalla

      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.

      Reply
      • Ania Romańska

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

  16. Jacob Eggebeen

    How can we have the sidebar close if anywhere outside of it is clicked?

    Reply
  17. Ania

    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?

    Reply
  18. San

    Hello i arrive to the jquerry, then i dont know how to continue.
    Can you send me the download link?
    Thanks

    Reply
  19. Jyoti

    Hi,

    How can I display hamburger icon instead of ‘Open’ and ‘Close’

    Please help.

    Thanks

    Reply
  20. Hurri

    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!

    Reply
  21. Aiman

    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.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Enjoing our content?

You might find this interesting…

License Details

REGULAR LICENSE

Single Site

A regular license allows an item to be used in one project for either personal or commercial use by you or on behalf of a client. The item cannot be offered for resell either on its own or as a part of a project. Distribution of source files is not permitted.

EXTENDED LICENSE

Unlimited Sites

An extended license allows an item to be used in unlimited projects for either personal or commercial use. The item cannot be offered for resell either on its own or as a part of a project. Distribution of source files is not permitted.

SUPPORT & UPDATES

Each license is a one-time payment. There are no annual fees. You get lifetime access to product updates. Support is provided for 6 months from the date of purchase.

Server Requirements

SERVER SETTINGS:

  • PHP 7.2 or later
  • upload_max_filesize (256M)
  • max_input_time (300)
  • memory_limit (256M)
  • max_execution_time (300)
  • post_max_size (512M)

PHP.INI SETTINGS:

  • php-xml or/and php-dom
  • XMLReader
  • PHP CURL module

PHP MODULES:

  • allow_url_fopen

Would you like to...

Consult Ania and fellow students?

Join the conversation inside our private FB group.

Would you like to...

Get support privately?

Submit a support ticket via your account page.

Ask Ania?

Submit a question for the next live Q&A session.

Consult fellow students?

Join the conversation inside our private FB group.