Creating toggles

Collapsible toggles and accordions can be used to make your article content easier to navigate and read. They help minimize scrolling and, when used properly, allow visitors to find content that’s relevant to them quickly and easily.

It’s important to consider what the best approach for structuring your article content is. For example, if the page is long but visitors need most or all of the content on the page a navigation element like a Table of Contents or sidebar menu may be more appropriate.

Adding toggles to article content

The first step is to create a numbered or bulleted list in your article where each list item contains the content that will be contained within its own collapsible toggle section. If you have a lot of content to display in each toggle, or content that contains headings and other custom elements, we recommend using the Source Code view within the article editor.

The list itself will be a <ol> or <ul> element, with child list items represented as <li> elements.

Once you’ve added all of your content to the list add a class name of js-toggles to the list and a data-title attribute to each list item. For example:

<ul class="js-toggles">
  <li title="The first toggle title">...</li>
  <li title="The second toggle title">...</li>
  <li title="The third toggle title">...</li>
</ul>

Where:

  • The .js-toggles class name specifies that we’d like to convert this list into dynamic toggles.
  • The title attribute specifies the title for each toggle.
  • The content of each list item is converted into its own collapsible toggle section.

An example of the default toggles style as well as an alternative method for specifying toggle titles can be found on the Toggles extension page.

This approach ensures a separation between your content and how it’s displayed to visitors. If you ever decide to switch off toggles for a given list, change themes or export your content you’ll see a simple, uncontaminated list.

Enabling accordion functionality

Enabling the accordion option in the Toggles extension ensures that only one toggle within a group is open at a time. If one is already open, expanding another will cause it to close.

The easiest way to enable this option is using the data-accordion="true" attribute:

Toggles behaving as an accordion
View code
8d88c075691a7f41a97cb6d7ff5eeb312846428dbea546ec4aad3ae0773710658c26b386229896e241a1ea93ee5b3a2c9c384c1c98c61a9fce79bbb47d13c02edc2c351d75a382f1f68e3e99d5acb9a786f52e1c3c7f6059999a5f9c9fe660e934b46c6f44ae96401ebe08a5c3d30f3740f0b8e960fcdb9421ec05d6483bcba642367fb355b4b665a2eaabdc591b8f46609cc03570c1185e4b272491abfaa04f2ebb0a3ca5a31dfdc087ce1c3dde5d234dc29eb54af6dc1a9c2a4e8111a7f260f9de80101a1181a4c7f90e34dcac2d4bb5ae804849afde7a2a4c8e28082f12b102d7205959f0c878131e70ca9562532ec3a25c09020b3b99fa917ef081b6d39192f962ea1159e72b33ebf6f9f98e4048b9426ad433af9acecd4f20769dd028cd2b5463e2e71a27a678e664e27096235a39156d8477f371c929e0561a20f880f7fec81fb5ceb8846aeeebd0aee73c63bbfb9ae769c63a3ec2d8a1736670e9bf7c050b6275deaf41d22ed098f11719a2de14da372ca8140d7f7d86dbbcf486f858e31c6cd071ac61d2a7d46fc24b80d6de9022f126c6d305b03697479536b470239ffefee40f2f8d5e7133462f8511a11d4213dccbeaa06f4ded1d7544e98220d65d91f6f33e3721414f28e7ae8a5be71ba6ca2771570d1ef73da7d76699a30f33d7438557b52937daf3f562204c1c4f41e8945e6fd1b6386b4dfe6143b969ad939f42379f33894df4ffa25677d0acf991a1dd1a88e0bb6924954373b67eb998645eeb7ef9314a576d63c850a50dc5fbe7d8c21c195946f328d9e4fd8b9ceb816a89ca20a58607307a7f1b8a5c4d2ebc5cf68ae082bdb405ce62b6fc020f1ef1ed80e99dcf4ad7420db420d622f7e16f325128f75f6f7bc66115e5c33eb3b4b12767e48e3a8bc559d5350215d051a7bf0f11b54bcd5f49d86ddbff80a2eabe4eaead205081b4a0658f130c5dbe65ac2ac0fe22163d82ede6e11f3d795c0c22df57f5222c0bed3f736f2fb87fdefef014505ef45dac6bc366284824171c4907318dc017772ebdcde4940a3b8c6d6e0c73554029b3210fd992b93f77797c62bcab7e775920b10782500bdffa8a12d6cf820656bd9a34ef20c1d771e2c1ee2749f47c53ab4c6aa38bfee0ec92aa7cadb6dc4b745b10478cb9043a6b28d81d404afc81dcb7663410878cee430be1c9b7191ec7dc6aed97a4a116da65158543c04123542fd7a2a33c5746a7e0e9994d13b83501281e9a02a2599859c289598c9a313e57c5f519ce80e0dfcabe20681f8f475f218a48611d65ff00c40e0b5e4475fe061eb59cef9293a24884b7f9375bf31c2c14dec60e193cfc6aedb47db10d3d71b7c8f328e516560310805c47d167f7c145b79c1f8e1f37580b00c8fac32aa31ade07bd9938947ee44d933ef3d6ff14d17ae9a915d4ba340ac08931a3ddf99aa08ded906d87fafdaa494d8dad2ceeb3e797ba42ea87a1b3583fb0f00a45a9498c99f4e2e02d4a706cc24fc66994019c98c3c0a81e879ea07aad008787e1f8ab7116f287cfee92f2256d103251a8edca7f77db61b1ca555212f728cb3d86e9adbe52cf03361fe3d89f1d2fdb60f08c8a68cd9bb152f0e1dfa3c1a98482e6375546b7f923125ede471d99ee5fb41b99a3ab4668806bc39bad7df205ac49c9e8d49b6e9753fa715b54eeff22b899559bea2dc53536f76e1027ba73e3824ff5c1232a5af1e76422e6b36f142109061e71fdc3f6945ca6881d2a77245424b2441cee96cbe08a7e230f513b7f7d612fbcb9639d65afa445f050a508589109df3c0fa549a148cbdccd17867773f93baae4e88d031744fc8e733da1b0114545d9dbc66b65580372c7c9024cee24867c86166c0061a11f9b2988b95b2cbfef096d14328137a124859a0c7a369a0ec8bf119f47798645bf40fc1c1653110c103ee1c9b2115c2dc6d7281fbe4229fe5b267f2998334be9efe9a77afa1527659aadd951fcc2c861502e815471420c17a58ed42891475354d896c9f28963058f1fbfc827b845efe47eda03d151132ea69fb8bffa0bc6b899c35001e256ccd5c9df10aff1b377dfdbb87a4b8109eede00f3fd5c765b16e682fb5ab8159fce13020d3ee9a77c8c7e66b2e3d47ebfd088eb773a1b10ff46729c14b33345e7d2e19eb55a253f8976479d62031afd980edb7606a68f65ceb0e1d52b0d89a34d290100ab3836a4e377540ce33cab78c9452034c87b640b185def24d948ff02feba41d00c540ca7f0d40a9d5be82a14fde77945265c62142195659c29eecf49843d1e955b09c333598b7f52e8b7946415cb77f62af6ccc1b00916f5f4a586e5a9a8637f2f99f70c00fe8f31c3c9189d3e76649b06ea5c2168c4d9170499769f0620ed421ef71417208a0ae1604450fea8833a6d67e1785b86159984cd9b7e56129e8cbda1893cdaf9d0a6d19a4b708f7118162b8ff2bd70184c5071d7b0305d3d660350a01234f4911af82377dac5c757e8d909ee17dc3d2b4757446aca4c14cb312c495c01bb24b96632cd2e766abdb4ff8c49c409e6afde01628de78fd2094c81d07e9cf8a6a6a2dc23620cfa92a50e9da03cf47e3414184e8f401c42cbbc8483d0d02e54f84a362426155718659cd3b0ca5e33864079b7e110570cd9c3b18194cad4707d5551a992f08a8b964785c04e757039a0326c3aaa6513d6f55903fd4014df76e8b429086abbad68e2fcb1fcd575730fc9b43dd58a86da5178295ca8c65c863be0c72c28689d2b075be4fcffa5dca36e2dc998d5bb9db31cc285f5be992452d286579de095b6e1b707cb492174b0f21742abd53887ffdc9c5ef62011808ec22e6fc789f4a39c4a05af728f18519f40af9c15ac62daea42f5e2f7b902f73446c2a77b295884a380dfffc7911043bf470002bf3f5c1a02cc9f57f8089661c561ae5e5fadef7279d0e63aa3de1af08e5064d7c018df4eda29bf6f9afc6416c2d7427247bc7b681450ff5c044cb04ccff80dfdf7905485066fc0fea01cd41b59a8c72a262463d7a1d381fe4431758f1db6d88257f7b2476a914fa6d8962565947e9feeaf91716c06b5c36c4e3fe00e674040b0eaf05152809bf206a250a9ade6846d2fadaa1ef3aaadb40b8c32267c573374ebe3816150462883f501062f7c3d847ea53fe521f626d4a62c6f014220fe0f98aff5b6ee13e2fe0920b02ac0a3b3ef67c2b6c8991a2076b9ec9dab4d694561f1e53a6999687e389dfdbb05074da4a1171e142fd4c29f3b6d667be676021d174733abcd4cadc32dcda808c17c31a4599b2aae1e5c4ff5bfa175f3a61b380671893a614df5f9737f6403b75ba20da6a0dbea80e2bc5928e29eb6d1143f8524f0718f5eca385deca30f37467a11972f3d29058dfd1f81568ab7145e472db8d385245c201cb21589c1c795bdcb9cde4b415c265fb1e1bc7cce9b2befcbd7af4d861d8b036940b738265f2d3e71b36c9dbd87abde178098aa1e46b4f526827d91c0f40af6f02a6744473e04252c3bd8e60cd0270f9eedca1c1588e781be8e3d923f6712b834565d8903ff0cca9b7e1de73aa6b50fb5f1c11e54365761d121bf4ea5c65fa62dc4b7624aad78c0702fb334dafeeb569cdabdfcd23f86bd74018cc54052ed7ac1d451737f90d4ed69eaaed1f8ff8049b0bd4b2c9e99e0116fac0681673c6c471f230c7547cf6d1bc8753897b89af84986dee916a0656dcd5e22902b2cef3b85e2b8fcc4088cb8555a951f2d6be1aee603477dfe6a56671e274aea34662d24444c70310b7cc1c62a8eaa2365bdbd757b53d5643b4aaf14779441907f4c27b47f3890d39a01d4addda4f2a767bb80e5f78cb179d9ca6d719ca3d3cbce0a510ae9612cad5f1e81896c5120d5defaacac1c0ec9a587d5cb6064f8993e22bdf9d84e180b9d5108e75c4c1c41af6b50a8aca639be9df3b9027667cd2d69c1e6870ce9ee5c28cc1e6516e4a0ee60f4d9def7ecee34a2f94cda17a3cb3e919b60c2dd1590aaf3508584114a6a2c6e318447ea2c6728e579d77c430aafaf7891aec45d92fab90fc36a288632efca0fb193afa7c16fbaf0a402cf6476fb5133ae7fd69f2db6716c12759a538ef60d3186e46f1b4d5578601935d448366c1453fc326946c208d3fba12c04d85176c5df7cf012a374eed56b621533b0531a96e21a9949b266096ef24a5bb1b8f2b717abd853966f8da8908368ae5c34961f28ea0fc66b8c08b26fe02cd8b79a9bc25346ce8e230d27cfb60323695c34cb92e124c24bdfde056e1c2836ca06306bb5ed72edf7d58f2d943c1ffd1706be4ce63506b94329562a0f8bda8438da3b1c633816ab32bfc58e1164870f3ee18e0667cba594839c339c5b6dca0e364dc019db8588fe0f12b9a566ae68289908589ff44d367ab8a8e26af50b88fd69b20d3ebd67d6341b5a1f7fca1aa57f2d2d9d446d91d291944fef5abd9dac87b3df82d89f7298242d2e369153611fbac18e1beef17420b7643034af2802a2295065f49f7786c944f04a7cbd44eb759b6ec18a98e48821b9b2253b370caacb6d651a7e930d0805ed685857976af053a73f7818c781fee349ff2f737f97c5156f3b2813c4daee5e28564596dd694acc6d320d

Styling toggles

If you’d like to give your toggles their own unique look-and-feel there are a couple of options. You can write your own custom CSS, for example:

/* Change the color of a toggle title to red */
.content .toggle-title {
  color: red;
}

/* Change the color of text in a collapsible toggle section to blue */
.content .toggle-title ~ .collapse {
  color: blue;
}

Using a custom micro-template

Or for the more adventurous among you, you can create your own custom micro-template.

A custom template allows to to change not just the appearance of the toggles, but their underlying HTML structure and behavior. Below is an example of a custom toggles template that you could use to change the size and appearance of the toggles:

Large, custom toggles with icons

Additional features like custom icons can be included in a custom micro-template.

The Toggles extension looks for a micro-template with an id of toggles by default, so adding one to Article page template (article_page.hbs) or the globally available Footer template (footer.hbs) will ensure that it’s used for all toggles within your article content.

<ul class="js-toggles">
  <li title="The first toggle title">...</li>
  <li title="The second toggle title">...</li>
  <li title="The third toggle title">...</li>
</ul>

<template id="tmpl-toggles">
  ...
</template>

Questions or feedback about this article? Let us know