Make Divi Accordion Module Tabs Closable

Written by Dan Mossop

Divi includes an accordion module which lets you place content in expandable boxes, useful for things like FAQs sections. Normally Divi will always keep one accordion tab (or toggle) open, and the only way to close a tab is to open another. If you'd like your users to be able to close accordion tabs without needing to open another (i.e. so that all tabs are closed), here's how to do it.

Note: see this post if you'd like to make the Divi accordion tabs start closed by default.

Example of a Closable Accordion

The following example shows the feature in action. Click to open the accordion tabs and you'll then be able to close them again by clicking either on the toggle "close" icon, or on the toggle title itself.

How hot is the sun?
Really, really hot.
Is the moon really made of cheese?
Nope.
Do you want closable Divi accordion toggles?
Go on then.

Making Accordions Closeable using Divi Booster

Divi Booster adds hundreds of new options to Divi, including the option to make accordions closeable. This can be applied to individual modules, or to all modules at once.

Making Individual Accordions Closeable using Divi Booster 

Divi Booster adds an option to make individual accordions closeable into the Accordion Module settings. You'll find it at:

  • Divi Booster 2.9.5 upwards: "Accordion Module Settings > Design > Toggle > Closeable"
  • Divi Booster 2.8.4 – 2.9.4: "Accordion Module Settings > Design > Title Text > Closeable"

Making All Accordion Modules Closeable using Divi Booster

It is also possible to make all accordions on the site closeable using the option found under "Modules > Accordion" on the Divi Booster settings page. This option has been available since Divi Booster v1.9.9.

Make Accordion Modules Closeable using jQuery / CSS

If you don't have Divi Booster and are comfortable with adding jQuery / CSS code to Divi, you can make the accordions closable using the following method. 

Step 1: Add the following jQuery code to your site, e.g. by pasting it into the "Divi > Theme Options > Integration > Add this code to the head of you blog" box.

<script>
jQuery(function($){
  $('.et_pb_toggle_title').click(function(){
    var $toggle = $(this).closest('.et_pb_toggle');
    if (!$toggle.hasClass('et_pb_accordion_toggling')) {
      var $accordion = $toggle.closest('.et_pb_accordion');
      if ($toggle.hasClass('et_pb_toggle_open')) {
        $accordion.addClass('et_pb_accordion_toggling');
        $toggle.find('.et_pb_toggle_content').slideToggle(700, function() { 
          $toggle.removeClass('et_pb_toggle_open').addClass('et_pb_toggle_close'); 
					
        });
      }
      setTimeout(function(){ 
        $accordion.removeClass('et_pb_accordion_toggling'); 
      }, 750);
    }
  });
});
</script>

Step 2 (Optional): To add a "close" icon to the open toggles, add the following CSS code to your site, e.g. by pasting it into the "Divi > Theme Options > General > Custom CSS" box.

.et_pb_accordion .et_pb_toggle_open .et_pb_toggle_title:before {
    display: block!important;
    content: "\e04f" !important;
}

Styling the Close Icon

With any of the above methods of making accordions closeable, the close icon inherits the styles of the built in "open" icon. That means you can style both the open and close icons using the existing options in the accordion module settings, e.g.: 

If you wish to apply certain styles to the close icon only (i.e. not to the open icon), then you can do so using CSS such as this:

/* Normal state */
.et_pb_accordion .et_pb_toggle_open .et_pb_toggle_title:before {
	color: grey !important;
}

/* Hover state */
.et_pb_accordion .et_pb_toggle_open .et_pb_toggle_title:hover:before {
	color: blue !important;
}

The example above will make the icon grey normally, changing to blue when hovered. If you aren't sure how to get the effect you want, just give me a shout in the comments.

Adding a Second Close Icon at the Bottom of Toggles

If your accordion toggles contain a lot of text, you may want to add a second close icon at the bottom to save your users having to scroll all the way back to the top of the text to close the toggle. Here's the code to do this. It should be used in addition to either the Divi Booster / manual methods given above. The code can be placed either in a code module on the page in question, or in the "Divi > Theme Options > Integrations > Add Code to the Head of your Blog" box.

<script>
jQuery(function($){
	$('.et_pb_toggle_title').each(function(){
		var $title = $(this);
		var $closebar = $title.clone(true).addClass('db_pb_toggle_close2').html(' ');
		$title.closest('.et_pb_toggle').append($closebar);
	});
		
  	$('.db_pb_toggle_close2').click(function(){ // Listen for clicks on the close bar
    		var $toggle = $(this).closest('.et_pb_toggle');
    		var mainAreaOffset = $('#et-main-area').length ? $('#et-main-area').offset().top : 0;
    		$('html, body').animate({scrollTop: $toggle.offset().top - mainAreaOffset - 16}, 700); // Scroll considering the main area's top offset
  	});
});
</script>
<style>
	.et_pb_toggle_close .db_pb_toggle_close2 {
		display: none; 
	}
	.db_pb_toggle_close2 { 
		margin-top: 10px; 
		visibility: hidden;
	}
	.db_pb_toggle_close2:before {
		visibility: visible;
	}
</style>

Easily Close Tabs with Divi Booster!

Enhance your Divi accordions by making their tabs closable using Divi Booster. This user-friendly plugin allows you to add new features like closable accordions effortlessly, perfect for improving FAQ sections and content organization.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

131 Comments

  1. Thanks a million Dan for the JQ code! It does the job I want! Excellent!

    Reply
  2. Hi Dan,

    Loving Divi Booster!
    I'm having some issue with the accordions closing after they've been opened on some browsers and mobile. I'm using the Divi Booster option "make accordions closable". Any way you could help?

    Here the page:
    https://studio.impactbrand.com

    Reply
    • Hi Alan, it looks like you're using the toggle module rather than the accordion module. The Divi Booster option should only affect the accordion module, but there was a bug in earlier versions which caused it to affect the toggle module (leading to the type of effect you're describing). If you haven't, try updating to the latest version of Divi Booster – I think that should fix the issue for you. Let me know if not. Cheers!

      Reply
  3. Hi Dan, can you take a look at this, there is a small plus sign above the toggles on the page. I want to take it out but i don't know how.

    http://rmiinstitute.com/staging/

    Reply
    • Hi Vic, I'm not quite sure how they got there, but this CSS should get rid of them:

      .et_pb_text .et_pb_toggle_title:before { content: ''; }
      
      Reply
  4. Hi again :-)
    Now I changed it. I deleted the code in the footer.php and put your 2 codes in where you described and now the close icon works :-) But now the accordions are not closed when entering on the site – the two first are open.
    Hhhmmm ;-) what am I doing wrong?
    Looking forward to hearing from you.

    Reply
    • Hi Elisabeth,

      I'm glad you got the code working :)

      I wrote a separate post which describes how to make the accordions start off closed.

      You should just be able to add the code in that post alongside the code you have already added.

      Hope that helps!

      Reply
  5. Hi :-)
    Before I found you page I found this code to put in the footer.php

    jQuery(function($){
    $('.et_pb_accordion .et_pb_toggle_open').addClass('et_pb_toggle_close').removeClass('et_pb_toggle_open');
    $('.et_pb_accordion .et_pb_toggle').click(function() {
    $this = $(this);
    setTimeout(function(){
    $this.closest('.et_pb_accordion').removeClass('et_pb_accordion_toggling');
    },700);
    });
    });

    It worked to have all accordions closed.

    Then I put you code:
    In the CSS where you described it. I got the icon to close the box – but the box is just not closing.

    Is it because I can not mix the two codes?
    Have a nice day.
    Elisabeth :-)

    Reply
  6. Hi, I'm using Divi Booster but nothing happens when I select "Make accordions closable"

    Reply
    • Hi Pascal, is there any chance you're able to send me a link to the page you're working on so that I can take a look?

      Reply
  7. Thank you so much. This is exactly what my client asked for and it works perfectly.

    Shaun

    Reply
  8. Hi! I'm having an issue — the open/close toggling is working perfect on IE and FF, but I can't see the (-) close toggle button on chrome!

    Reply
    • Hi Trisha, are you able to send me the link to a page it's happening on? I took a quick look on the site linked in your comment but it seemed to be working (in Chrome) on the ones I saw.

      Reply
      • Dan —
        I actually solved it, I didn't have a media query closed in my custom css. Also, if you could please remove the URL from my name — thanks!

        Reply
        • Okay, great! I'm glad you solved it, Trisha. I've removed the URL. Cheers!

          Reply
  9. It adds the minus sign nicely, but clicking the minus sign doesn't actually close the accordion. I added the What am I doing wrong? I inserted the code into the footer as you explain in this post: https://divibooster.com/make-divi-accordions-closed-by-default/ and then I added the second part of the code above to the custom css in the theme options. What did I miss?

    Reply
    • Hi Simone, the code you've added into your footer seems to be different to what I have in the post above, and doesn't seem to work correctly. I'd suggest replacing the jQuery code you currently have in your footer with that given above and see if it helps. Also, I'd place the code before the closing </html> tag.

      Reply
  10. Hi, Dan – trying again as my first comment never got approved a couple weeks ago.

    When I turn on the accordion settings in Divi Booster I get a gray "-" sign when I click on the accordion button. When I deselect the settings the gray minus sign goes away. Also, with or without the settings on or off, the accordion doesn't close the previous when I open a new one. Any insight is greatly appreciated. Loooove Divi Booster!

    Please have a look here: http://stainfu.com/f-a-q/

    ~Scott

    Reply
    • Hi Scott, sorry I'm just getting to this. I've released an update (2.4.0) which should sort out the "-" sign. It should now correctly inherit the styles you configure for the "+" icon, rather than showing the default size and gray color. I've also updated the post above.

      I think the reason the previous toggle isn't closing when you open a new one is that you are using the "toggle" module rather than the "accordion" module. The former gives you toggles which are essentially independent of one another, while that latter gives toggles that open and close based on the state of the others. Switching over to an accordion module should give you the behavior you are looking for.

      Reply
  11. Hi,Dan –

    I have set the accordion buttons in Divi Booster to:

    Make accordions start fully closed by default
    -and-
    Make accordions closable

    It doesn't seem to close the previous toggle and places a gray overlay – icon on top of the black + icon. Please have a look:

    http://stainfu.com/f-a-q/

    I have tried turning each of the 2 settings on and off to see if it makes a difference and apart from the white – overlay icon going away, it functions the same. Please advise.

    Reply
    • Hi Scott, I've released an update (2.4.0) which should sort out the "-" sign. It should now correctly inherit the styles you configure for the "+" icon, rather than showing the default size and gray color. I've also updated the post above.

      I think the reason the previous toggle isn't closing when you open a new one is that you are using the "toggle" module rather than the "accordion" module. The former gives you toggles which are essentially independent of one another, while that latter gives toggles that open and close based on the state of the others. Switching over to an accordion module should give you the behavior you are looking for.

      Reply
      • Thanks, Dan! You are the man!

        Reply
  12. Or you can just use the toggle module instead of the accordion module. The toggle module allows all toggles to be closed by default.

    Reply
    • Yeah, that's correct, though you do lose the accordion effect where opening one toggle closes the others so it depends on the effect you're after. Cheers!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.

Latest From Divi Booster

Enable Automatic Looping for YouTube Videos in the Divi Video Module

Enabling automatic looping for YouTube videos in your Divi video module ensures that content plays seamlessly and continuously without interruption. This feature can enhance user engagement, highlight key information, or create dynamic visual effects on your website....

Hide Video Controls in Divi Video Module

Disabling YouTube video controls in the Divi Video Module helps create a seamless, distraction-free viewing experience for your site visitors. This approach is useful when you want full control over how your video content appears and is interacted with on the page, or...

Mute YouTube Videos by Default in the Divi Video Module

In the Divi Video Module, starting your YouTube videos muted by default can create a more user-friendly browsing experience on your webpage. It is particularly beneficial for reducing distractions, enhancing visitor engagement, and providing a seamless content preview...

Autoplay Videos in Divi Video Module

Enabling videos in your Divi video module to start playing automatically can enhance both the site's design and user engagement. To ensure full compatibility with browser requirements, such as Chrome's autoplay policy, it's often necessary to start your videos muted...

How to Add the Post Status in Divi's Dynamic Content

Divi's Dynamic Content feature is great for enhancing your post/page templates with post-specific information. While Divi includes an option to show information such as the Post Created Date using Dynamic Content, there is no equivalent option for the Post Status....

Random Divi Posts