Headings that Expand and Collapse
Start with content that has a heading, e.g.
1
<h2>Section Title</h2>
2
3
<p>Section text here</p>
4
<p>More text here</p>
Copied!
To make the section collapsible, it needs to be wrapped in div tags like this:
1
<div>
2
<p>Section text here</p>
3
<p>More text here</p>
4
</div>
Copied!
After you've put the content in a div, give that div the class name collapse and a unique ID:
1
<div class="collapse" id="uniqueID">
2
<p>Section text here</p>
3
<p>More text here</p>
4
</div>
Copied!
Now turn your section title into a link with the following code:
1
<h2><a href="#uniqueName" class="collapsed" data-toggle="collapse">Section Title</a></h2>
Copied!
Note that href has to equal the unique ID of the div. This is so the link, when clicked, knows which div it's supposed to expand or collapse. So if you want multiple divs on the page that can be expanded or collapsed, each one must have a unique ID.
Note: If you want a section to be expanded by default when a page loads instead of hidden/collapsed, rewrite the link heading code as follows:
1
<h2><a href="#uniqueName" data-toggle="collapse">Section title here</a></h2>
Copied!
That is, remove the code class=collapsed (when you include class=collapsed, it makes the link show up with a "Plus" icon to indicate the content can be expanded. This "Plus" icon changes to a "Minus" when the content is expanded). You also need to change the section's div code from:
1
<div class="collapse">
Copied!
to:
1
<div class="collapse in">
Copied!

Markup for complete, working example:

1
<h2><a href="#sec0" data-toggle="collapse">This section expanded by default</a></h2>
2
<div class="collapse in" id="sec0">
3
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p>
4
</div>
5
6
<h2><a href="#sec1" class="collapsed" data-toggle="collapse">Section One</a></h2>
7
<div class="collapse" id="sec1">
8
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p>
9
</div>
10
11
<h2><a href="#sec2" class="collapsed" data-toggle="collapse">Section Two</a></h2>
12
<div class="collapse" id="sec2">
13
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p>
14
</div>
15
16
<h2><a href="#sec3" class="collapsed" data-toggle="collapse">Section Three</a></h2>
17
<div class="collapse" id="sec3">
18
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p>
19
</div>
Copied!
If you'd like to add a button that toggles multiple sections at the same time, you need to add one more class to each div. For this example, we'll call the class collapse-all though you can use whatever name you prefer:
1
<h2><a href="#section1" class="collapsed" data-toggle="collapse">Section 1 title here</a></h2>
2
<div class="collapse collapse-all" id="section1">
3
<p>Section 1 text here</p>
4
</div>
5
6
<h2><a href="#section2" class="collapsed" data-toggle="collapse">Section 2 title here</a></h2>
7
<div class="collapse collapse-all" id="section2">
8
<p>Section 2 text here</p>
9
</div>
10
11
<h2><a href="#section3" class="collapsed" data-toggle="collapse">Section 3 title here</a></h2>
12
<div class="collapse collapse-all" id="section3">
13
<p>Section 3 text here</p>
14
</div>
Copied!
The code for the button would look like this:
1
<button aria-controls="section1 section2 section3" aria-expanded="false" class="btn btn-primary" data-target=".collapse-all" data-toggle="collapse" type="button">Your button text here</button>
Copied!
For aria-controls, list all the div IDs you want the button to control. For data-target, use the class collapse-all (or whatever name you chose - it just needs to be the same for each div). So the complete code would look something like this:
1
<button aria-controls="section1 section2 section3" aria-expanded="false" class="btn btn-primary" data-target=".collapse-all" data-toggle="collapse" type="button">Your button text here</button>
2
3
<h2><a href="#section1" class="collapsed" data-toggle="collapse">Section 1 title here</a></h2>
4
<div class="collapse collapse-all" id="section1">
5
<p>Section 1 text here</p>
6
</div>
7
8
<h2><a href="#section2" class="collapsed" data-toggle="collapse">Section 2 title here</a></h2>
9
<div class="collapse collapse-all" id="section2">
10
<p>Section 2 text here</p>
11
</div>
12
13
<h2><a href="#section3" class="collapsed" data-toggle="collapse">Section 3 title here</a></h2>
14
<div class="collapse collapse-all" id="section3">
15
<p>Section 3 text here</p>
16
</div>
Copied!