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
c9affcc378d2325352d83da164fbd91d3be9888793a3e7302fb104d0cbafed3cf467e40a8f19767fc50e22d461d4ae65851b3e0fce5823dab360eddfb182539b3fa5936d69122b460d71e9176a40a27698925ee83819ae2a9005611f8547e55b13fa2b03eea4d9c06e1787896d551d57e53e3eee9b29b4ed7ea9c01cd7aeb5ea691169a70280bdf3def0d247d397d39faa2c4312965534725c6cbbbe752be59f5e3989e530380d7ac5b2aa6fc39c700d1a6cd7266aec357759c06dece53d3a7c496e4945013e2343673ea949f1e99646b347fb9baa94061c631c6e3f86d096fe4747f0eeaca1ddbfea0b887faf60560eba8e3619b05731480f690f8c9c71d943f9b0e7cfb16243c789a7d1e9f44d99893345398e13d33ad2c760b7be2cf30c2561f055640b281806d54f61dc639d26e39c5b88a4dacb4ecfb7c91f35cf9ae07e9b06c5a091ca3717a31e1fb2a717b54fee4dd5eaa8580a5e1655578ed96b22b6b166fc8d34a21030e551fc0618ee071e75a1aed7d8a332dd013f0885f849da4198bce3fb902838abdf9f73e4202981dd37b1b215d4c0305899d6b205ba723379b584b0a27aa0d24322aeceb233be7043617aa7906dca8c500db08482cf1b180c46ba45792b0c83bc279db1644115b097ec80c102456bc493ca075597f272695143b1d3c8b6c3d4f293995af4644c0a955e3eba799f54eb565c69c14e4288350fc75e264c1e3340bd0cd853457c2ff30c5bfe4b81cb6928f08f2f67a9e4ae5b103e87cfe5a8e48017e892341bed33aa834847279c4661ff9702fb99ebd9b166b9850cb8ddd31dbd2c38fb6ab4aabb34b821acb6db22d5043572f1b62085094d0cded3067963258f5a2400ca814b43f8a02196976e4b97578bb895aa0d5077d71d14f82b30864d612fa01483e04ccd693f997f302ec4e2b9ba49b83000edb1207f8484c7500965c1a43b2fb594b14475b5a862bc0079b1d128fb38099b92632ee01b81ca21bda6268c3d4bd5325983aa3a655acdafaede377c2f1700d01b3017513ab3f1a2dd4cbe15e84c681dc42b4ba73ef669c37013b6e40ab57571f4b3ec195706b7285d0a658b8f03e6a96d319b712287bb708708a3bface67e0855d3567d2cb6a09467912a3006bc1a4efeaeac87774506756537345516a877aaada1ccd5281b215678cfa6e42545ef8672379001d32410aeb85b66899a3be6a51297f0bb28af72a7d7f1f4d9876ce3f5a21c3eee18aa26683dae9cf708b3add58cc64e17af8252a63f64c6c642fe5e3599617458412327509014c3e647e9f15ea6a494cffc1e33528181471fe559fab133c47cfc2a069cdddebeaa56cd156caf7cc348e11150fbc866d179582d1556e877aaf751efcc2947e46c8841c7e25f41dcf105626bc04f869c487b14207febb162f5b755e6161955a40f17dac5aed0d49684e7c4c215d06fcf4b48a54341188b0129b6247477ca6031e553d63162b74a802528d16dfe10dda0d696568d3e6baea0028c49d6609c095dcae9baa4f9d7914b0526cffefad74fe445b66e25ab1bbfdedfe8223923f009b60fc2eab2ef6ec08ffe7b897ac80c32b8d7d3ab2d98367a84bd2d2c56e7148916209816d391d8832c883ca9073f01939ef61f5d37031b050ac19e8b81dc1b7c64334da6364f5791cb5da03b493831700a0396714c57ff4a6a9b8fe8aee12e1baafb479790823fd12635ed5c9cf13f8b22b39b40b04e86ca7ae062bca1130951ec370e4dff8b7130e8807519d3be79f518a537433e64dca262637d9a03bc2afac037a0caf53a275938d32a14759e4a3b0c26911c556fe8b2f0b68a0532b92b9b7a715b3b6490d33a0b54a350fdba2051fd39436956a4a9ac6ce3ec700194a4b4f984399eb2255c81147884f8a7ca6374ba6e9bfc4e748b65839d5546dfeb3c6293c9b7a4a7d208f12d9c00d82a37f2283c3c853b7eedbe261371c265b37c0c55cb0cdfae321a27d959ad424399d0b715eeb79ceba400ad85aed4b9e28c799893047096b93628e1cba7e594d8a2cd8dbfaefb5a7615af725dd00d89dcfe509750d93390c29f553b7c45f0559e72f272e1f46d39af3988e2ae531f5fc21b7a69225c1bae2ca8e7c0e49a4fad24d4407b6d73ed9f65ebd6e444908c77cc11fffb0833d3f0fbb6dd3e40e48b16f49ec6c4692124eb9af34214a3082dff7122c53d09c2988655f6c92aa75eb9c3dca0a594c6e301044c338790cdcbc30c161a3a20f15db74c69db574cc6cfe2fa68a33ff5b1341780eaa6bce66898316bacdd4d841bed966ab82b1c4fd07c92a370508037bcbe7ea090680e8d3d2ef309066d5f73a06151c7165cd36429835ba7b8f301d83a1ce54984eaddf3495105b2fb21121154eb66f0df1fc9942d06c15188b522e5847c4ddb6ac72eb1179b42226e53820e252ec9b0ec8a1057a218ff1b079d3e1c7c09ca5ba1c834cca1df6f81a7407db0d7cb215f3aab8d38cfb4706168e80916969348467de575836f4b169500d267b37bc94f18cef3ef7f9bd0d66af441f79d529f4c73df7954ceceb497bc2333caa5d46b121b7c11f040785350bbb6417db2a834747bea106c7c85e1bbe0dd0d5a9c600da8ef4eaa57b594f096d15b9c25686fb4046d545008877eeea28591f7d1c950fa4212cc8ce7c66e45b29b192baf19bd44183fe5bd04cc86cc1f75a6ff0277637d4f5bb6558e7e9d6b6ed73b8591ddb539356df45990b25ca94d4ab3ce80d214cf9579a42ac04756980143c2de331c263021c49e85496379170b74596098fba9ca7f05d15097bdf0c267fb57cd828a08c1ec051d71d66074e2960605e374ce1d52e1b825a90a2e19d470c43949cd417b9f175695e670de1e64d7e13a009b9a8bdd75d388aa98cc5a63c8e1d7fe8a580a0c6a8c33edd66b5872d763e587454cd3304e760ca0ae050a4d47a94a44e6593f7f75c9ac1a9af436517a9bf9c07c05551e9337ef2a78b3af2e9474d3e94e254ff62c48038ef112b4106472921f146aada3587ee75b14d7ef47ca75bb16bf5ec1d222f8d14c3ce9f2c7674f667fb05a2a7e7be9e03573fc350ebb28b967155160612fdeed1c368bc90768a5034cecd1f07bc2911cda231691d1a0179d016fb27a1e36721359ce934af162648eb4dbe5628942834cec2ee5ac1c73929ee00c366450b3f910ee6dda81259205265b67e39ed2244df5a493ab8bb43243d353e4e8b7d472bfdf81d0ff4bef4609a894b832e217125701204fa58f2f74a6dab9ee50501b6eebddcdc2956bea512fc9edc4248e38bf7f9688f0ef4ed0c084a41620f1f2037cd62944c4101aab0b65a018bda91ac4f334399685fd493e2995a3a673eb34d299b7fe41283b6a7cf7217c881323657b3783c525a60a61b0c61658f59665b37f8dacc3026830d03fd31a21433d801d1e46d5d34a8d53935b2f09bdbaa7b95e8b5dff8d89d5998ed62a83dfeedbdd4e30a8b9a475e4eee20fcf3a3fc36a8fedc6da233272b5c6c932b2f7146536cae205f0fa6e334ffbe1f5d2289fbf6d089a4ab901925cb4106eb3c2e8f4a3da06a00a5fce76f4821cb2a153125c9017b98834c014c0b6b9c9ef9bf5faef92cdf91c3c28537c71234535410cb3e830a63421589fd1c27d69ad7c4fcf5165ef7ad24685da87dedc4dbbfee77b87eb949bee33a939fd38f56b6a17a741af2b2214e1d056ad591b89063cfb3739849a5bfb9553303e9e35688f4a15108ca37ba089b40ff5e63a834c7bb87a5ae9cdbe946ce84526b582452228c37b254302927c5641f1269de7456776f8d9c86b2b5438f3803408b9bf335bc2259c5640dfab6c4214d34575239778498e52b406f218ef86a5fd12af94963696a65354a7d87bc0e4df4c9891fcd325049670c29858ccfd3fb733750b087727f6c303cdd9e6f3060659e7900f8568ede2701951f3f5eb71a477d8abb7f928d5977acaa04d42be928cf96844e8ad6437a063f0144e6ece8fd6fbd2c1497a3d990a389783e45a0aacd642de3b953ae287aae255ecc3121b34fb17e8d9b1361686c8c2340b81fded03312820e7410871badd3d59e9428b39422a9e898c3915a83b8bcb4859b3e702b6125c5c99e8aed91ba1d5a6f286ee45cada777e617e4a81f7eb66b0527a5a3b8a42e9a5e4ef135b04b3dbd1a74c615b65eb01eec1adbaa6142ab280a0c1f2d47f66d7774f81dc7a4873cc36d186d2dba4d7bdadc6fbb9e0678676bc6846c11af004f8b41b229daa47d987e623876d6e664e00659828d3d86d362a8612fcf051c4f88ea92327905fe2733230a53a684c237ed30401d08c06c6af0156b299d568e51cd077b24ff4c7a54119dac223d417d6ca9349e52b559c264fe3cd2c3797910bcb9f23af7224a7d6b0143eb14c4c3555232428b6c51275b9b25a238fc3fefd345e6b36a410ec8871e65d0ddc7e11af77d75d8964c9e12bcc41165c0d677a67f9632849bd76fba1e1130e8c3740429d4a97c363a748c852181676454013d909eaba8deb5db853288c0838fe11569fe74fde5c1aa37c573a47ac05eb938e9b818cf33f36feeaad9d44b18a1ca6f2750cf71f88a551129f2756574200602574f9d9d86f011f9843054c108fc1dd38b9ce

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