Collapsing is a feature that allows you to collapse (or hide) any HTML element.
There are times when a page can contain many related elements which makes it very long; using a collapsing feature enables editors to shorten the page, and allow readers to expand areas of interest to them. Most frequently this feature is used on long tables of data and large navigation templates.
To make an element collapsible, simply add the class "mw-collapsible" to it.
Examples
Collapsing a table
By adding "mw-collapsible" as the class, a table can be collapsed to save page space.
{| class="article-table mw-collapsible"
! Number !! Letter
|-
| 1 || A
|-
| 2 || B
|-
| 3 || C
|}
| Number | Letter |
|---|---|
| 1 | A |
| 2 | B |
| 3 | C |
Collapsing text
It is possible to collapse text in an article, effectively shortening the page, and allowing the reader to decide if they want to read the rest of the information. For example:
<div class="mw-collapsible">
'''This text is collapsible.'''
</div>
Advanced usage
Initial state
To make a collapsible element start closed, add mw-collapsed alongside mw-collapsible.
{| class="mw-collapsible mw-collapsed article-table"
! The header !! remains visible
|-
| This content || is hidden
|-
| until 'Expand' || is clicked
|}
| The header | remains visible |
|---|---|
| This content | is hidden |
| until 'Expand' | is clicked |
Selecting collapsible content
By default, the entire element is collapsible. To hide only part of the content, wrap it with mw-collapsible-content.
<div class="mw-collapsible mw-collapsed" style="width:100%">
'''This text is not collapsible; but the next is collapsible and hidden by default:'''
<p class="mw-collapsible-content">This text should be hidden by default.</p>
'''This text should be visible as well.'''
</div>
This text is not collapsible; but the next is collapsible and hidden by default:
This text should be hidden by default.
This text should be visible as well.
Custom toggle labels
Don’t like the default "Expand" / "Collapse" labels for toggles? No problem! You can easily replace them using:
data-expandtext— text shown when the content is collapseddata-collapsetext— text shown when the content is expanded
{| class="article-table mw-collapsible mw-collapsed" data-expandtext="Show spoilers" data-collapsetext="Hide spoilers"
! My || Header
|-
| A || B
|-
| C || D
|}
| My | Header |
|---|---|
| A | B |
| C | D |
You can also use symbols instead of text to make the toggle smaller:
{| class="article-table mw-collapsible mw-collapsed" data-expandtext="▼" data-collapsetext="▲"
! My || Header
|-
| A || B
|-
| C || D
|}
| My | Header |
|---|---|
| A | B |
| C | D |
Changing toggle placement
You may notice that the "Show spoilers" link in the second column significantly increases the width of the table. If this happens, you can move the toggle elsewhere. We can also fix that by moving the toggle to somewhere else on the page.
To control where the toggle appears, you can use a placeholder element:
- Add an element with the class
mw-collapsible-toggle-placeholder - The placeholder will be replaced by the actual toggle link
- Make sure it is placed outside the
mw-collapsible-contentarea so the toggle remains accessible when the content is collapsed
{| class="article-table mw-collapsible mw-collapsed" data-expandtext="Show spoilers" data-collapsetext="Hide spoilers"
|+ <div class="mw-collapsible-toggle-placeholder"></div>
! My !! Header
|-
| A || B
|-
| C || D
|}
| My | Header |
|---|---|
| A | B |
| C | D |
Removing toggle brackets
By default, the label is displayed inside brackets. You can remove them by adding the following CSS to your wiki:
.mw-collapsible-toggle-default::before,
.mw-collapsible-toggle-default::after {
display: none;
}
Custom toggles
The data-expandtext and data-collapsetext options are quick to use, but limited:
- the label is always wrapped in brackets (unless removed with CSS),
- you cannot use templates or any styling inside these attributes.
If you need more flexibility, you can create a custom toggle instead. To do this:
- Give the collapsible element unique
idin the formatmw-customcollapsible-YourID. Using our table above, we can add the ID "mw-customcollapsible-myTable". - Add a button/link/text element with the class "
mw-customtoggle-myTable" (note this is a class and not an ID like above).
<span class="mw-customtoggle-myTable wds-button wds-is-secondary">
Show/Hide table
</span>
{| class="article-table mw-collapsible" id="mw-customcollapsible-myTable"
! My || Header
|-
| A || B
|-
| C || D
|}
| My | Header |
|---|---|
| A | B |
| C | D |
Changing toggle text based on state
By default, MediaWiki does not automatically change custom toggle text when a collapsible element is expanded or collapsed. You can achieve this effect using CSS and separate elements.
<span class="mw-customtoggle-myTable2 wds-button wds-is-secondary">
<span class="expand">Show table</span>
<span class="collapse">Hide table</span>
</span>
{| class="article-table mw-collapsible" id="mw-customcollapsible-myTable2"
! My || Header
|-
| A || B
|-
| C || D
|}
body:has(.mw-collapsed#mw-customcollapsible-myTable2) .mw-customtoggle-myTable2 > .expand,
.mw-customtoggle-myTable2 > .collapse {
display: block;
}
body:has(.mw-collapsed#mw-customcollapsible-myTable2) .mw-customtoggle-myTable2 > .collapse,
.mw-customtoggle-myTable2 > .expand {
display: none;
}
Using collapsible to change content
<div class="mw-customtoggle-myFirstText mw-customtoggle-myOtherText" style="color:#c00">Click here to toggle the element</div>
<div class="mw-collapsible" id="mw-customcollapsible-myFirstText">
<div class="mw-collapsible-content">
{| class="wikitable" style="width:300px"
! Column 1 !! Column 2 !! Column 3
|-
| 1 || 2 || 3
|-
| 4 || 5 || 6
|-
| 7 || 8 || 9
|}
</div>
</div>
<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myOtherText">
<div class="mw-collapsible-content">
{| class="wikitable" style="width:300px"
|-
!Row 1 || 1 || 2 || 3
|-
!Row 2 || 4 || 5 || 6
|-
!Row 3 || 7 || 8 || 9
|}
</div>
</div>
<div class="mw-customtoggle-myFirstText mw-customtoggle-myOtherText" style="color:#0a0">Clicking will toggle it also!</div>
| Column 1 | Column 2 | Column 3 |
|---|---|---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| Row 1 | 1 | 2 | 3 |
|---|---|---|---|
| Row 2 | 4 | 5 | 6 |
| Row 3 | 7 | 8 | 9 |
See also
Further help and feedback
- Browse and search other help pages at Help:Contents
- Check Fandom Community Central for sources of further help and support
- Check Contacting Fandom for how to report any errors or unclear steps in this article