Community Central
Community Central

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
|}

This results in:

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>

This results in:

This text is collapsible.

Advanced settings[]

Initial state[]

To set the initial state as collapsed add "mw-collapsed" along with "mw-collapsible", such as seen in the following table:

{| class="mw-collapsible mw-collapsed article-table"
! The header !! remains visible
|-
| This  content || is hidden
|-
| until 'Expand' || is clicked
|}

This results in:

The header remains visible
This content is hidden
until 'Expand' is clicked

Selecting collapsible content[]

You can also specify which part is the collapsible content by using "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:'''
<div class="mw-collapsible-content">This text should be hidden by default.</div>
'''This text should be visible as well.'''
</div>

This results in:

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 wording[]

Don't like 'Expand'/'Collapse' for the links? No problem! Change them using "data-expandtext" and "data-collapsetext".

{| class="article-table mw-collapsible mw-collapsed" data-expandtext="Show spoilers" data-collapsetext="Hide spoilers"
! My || Header
|-
| A || B
|-
| C || D
|}

This results in:

My Header
A B
C D

For a more compact toggle button, any text symbol may be used.

{| class="article-table mw-collapsible mw-collapsed" data-expandtext="&#9660;" data-collapsetext="&#9650;"
! My || Header
|-
| A || B
|-
| C || D
|}

This results in:

My Header
A B
C D

Toggle button[]

In the first example of the previous section you may notice that the "Show spoilers" link in the second column significantly increases the width of the table. We can also fix that by moving the toggle to somewhere else on the page.

To do that, we have to assign an ID to the element. This ID must be prefixed with "mw-customcollapsible-" and followed by a unique key phrase for that element. Using our table above, we can add the ID "mw-customcollapsible-myTable".

That takes care of the table itself. Next, we have to 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
|}

This results in:
Show/Hide table

My Header
A B
C D

Advanced examples[]

Default toggle link[]

Input:

{| class="infobox"
! Foo
| Bar
|-
! Lorem
| Ipsum
|-
! More info
|<!--
-->
{| class="wikitable mw-collapsible mw-collapsed" style="width: 100%;"
! Head
! Top
|-
| Cell
| content
|-
| This table is collapsible
| Because it has the "mw-collapsible" class
|-
| It was initially hidden, because it
| had the "mw-collapsed" class
|}<!--
-->
|-
|}

<div class="toccolours mw-collapsible" style="color:#000;width: 400px;">
This is text is collapsible.
</div>

Output:

Foo Bar
Lorem Ipsum
More info
Head Top
Cell content
This table is collapsible Because it has the "mw-collapsible" class
It was initially hidden, because it had the "mw-collapsed" class

This text is collapsible.


Custom label[]

Input:

{| class="wikitable mw-collapsible mw-collapsed" data-expandtext="+" data-collapsetext="-"
! Column 1 !! Column 2 !! Column 3
|-
| 1 || 2 || 3
|-
| 4 || 5 || 6
|-
| 7 || 8 || 9
|}

<div class="mw-collapsible mw-collapsed" data-expandtext="Open" data-collapsetext="Close" style="width:300px;">
This text is not hidden
<div class="mw-collapsible-content">
This text is all hidden

And is only shown

When it is uncollapsed.
</div></div>

Output:

Column 1 Column 2 Column 3
1 2 3
4 5 6
7 8 9

This text is not hidden

This text is all hidden

And is only shown

When it is uncollapsed.


Remote toggle[]

Input:

<div class="mw-customtoggle-myDivision mw-customtoggle-myDivision2" style="display:inline-block;background:rgba(128,128,128,0.5);color:white;padding:5px;border-radius:5px;outline:none;user-select:none">Toggle all Spoiler</div>

<div class="mw-customtoggle-myDivision" style="color:#070">Troll div in middle</div>

<div class="mw-customtoggle-myDivision" style="display:inline-block;background:rgba(128,128,128,0.5);color:white;padding:5px;border-radius:5px;outline:none;user-select:none">Spoiler 1</div>

<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision">
	<div class="mw-collapsible-content" style="background:rgba(255,255,255,.5);margin-top:10px;padding:10px;border-radius:5px">
		¡HI! I am a spoiler
	</div>
<div class="mw-customtoggle-myDivision2" style="display:inline-block;background:rgba(128,128,128,0.5);color:white;padding:5px;border-radius:5px;outline:none;user-select:none">Spoiler 2</div>

<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision2">
	<div class="mw-collapsible-content" style="background:rgba(255,255,255,.5);margin-top:10px;padding:10px;border-radius:5px">
		¡HI! I am a spoiler
	</div>
</div></div>

Output:

Toggle all Spoiler
Troll div in middle
Spoiler 1
¡HI! I am a spoiler
Spoiler 2
¡HI! I am a spoiler

Change text for other[]

Input:

<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"
! 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:15%;"
|-
!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>

Output:

Click here to toggle the element
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
Clicking will toggle it too!

Removing brackets from toggle[]

The following style rule may be added to the wiki's stylesheet to hide the brackets in the default or custom label toggle links:

.mw-collapsible-toggle-default::before,
.mw-collapsible-toggle-default::after {
    display: none;
}

See also[]

Further help and feedback[]