Tip: Collapsible headers in articles or templates (accordions) | The place for Zendesk users to come together and share
Skip to main content
Trapta11
August 18, 2021

Tip: Collapsible headers in articles or templates (accordions)

  • August 18, 2021
  • 141 replies
  • 1006 views

The original tip was posted by Wes. I am simply updating the tip to make it work with the current theme version.

Let gets started without any further delay.

Demo: https://moderatortrapta.zendesk.com/hc/en-us

Step 1: Add the below code (HTML CODE SNIPPET) to the source code of your article.

<div class="accordion">
<p>Section 1</p>
</div>
<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

Click OK to save the progress.

NOTE: You can copy and paste the same snippet to add as many accordions as you want.

Step 2: Edit your theme to add the below code at the bottom of your style.css file

.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
margin-bottom: 10px;
}

.accordion p {
  display: inline;
}

.active, .accordion:hover {
background-color: #ccc;
}

.accordion:after {
content: '\002B';
color: #777;
font-weight: bold;
float: right;
margin-left: 5px;
}

.active:after {
content: "\2212";
}

.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}

Step 3: Edit your theme to add the below code at the bottom of your script.js file

document.addEventListener('DOMContentLoaded', function() {
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
});

Save and publish the changes.

YOU CAN USE THE ABOVE CODE TO ADD ACCORDION ANYWHERE THROUGHOUT THE THEME. YOU NEED TO PASTE THE HTML CODE SNIPPET (IN STEP 1) IN RESPECTIVE TEMPLATES WHERE IF YOU WANT TO ADD IT ANYWHERE OTHER THAN THE ARTICLE TEMPLATE. 

Let us know if you face any issues.

Thank you 544374443 for figuring out the issues in the tip and providing the fix for the same.

Thanks

141 replies

August 30, 2021

Hello, team.

What a great tip you've provided, I'm so impressed!
I tried it as soon as I could, but I can't view the sentences that are collapsed in sections.
I set it up as you wrote, but I wonder if there is something I am missing.

I hope you will be able to solve the problem.

Thank you.

Trapta11
Trapta11Author
August 30, 2021

 

Can you check if you are getting any errors in your console? If possible please share the URL of your HC so we can take a look at it.

Thanks

August 31, 2021

Hi Trapta,

For security reasons, it's not safe to post the URL, so can I send you an email directly?

By the way, the error message was not displayed in script.js, style.css, or HTML code.

Thanks

Trapta11
Trapta11Author
August 31, 2021

@Kobayashi, sure you can. Please find the email ID in my profile.

Thanks

August 31, 2021

@ifra, amazing tip! I've just implemented this on my help center for a step-by-step process and it's gorgeous. One question - do you happen to know how i could turn an accordion step into an anchored link that automatically opens to the right step? i.e. we want to send some of our users directly to the correct step in the process, and have it auto-open where we send them once they've clicked the link. do you know if this is possible with this accordion set-up? 

September 22, 2021

This is great, thanks @ifra!
How would we make one auto-close when the next is clicked? 

Tony18
November 23, 2021

Hi,

Do anyone have any example pages where I can test this functionality where it has been implemented? :)

Thanks in advance! 

Trapta11
Trapta11Author
November 23, 2021

Hi @Tony Jansson,

I have updated the post to add a demo link for the same as well as to add the bottom margin to accordions in case of more than one.

Thanks

Admin24
November 23, 2021

Awesome job @trapta11, just implemented it in a critical article that we have here, thank you so much for sharing.

Tony18
November 24, 2021

Thanks for uploading a demo @trapta11 :) 

Hope it is okey with a follow-up question:

As i understand, the demo shows articles that collapses with headers, Is this also something that is possible to apply to sections?

Example if i click on a section header that expands with article titles, and then again expand the article when clicking on the title? (like everything shows up on same page).