Components

Accordion
Experimental

This component is currently experimental because more research is needed to validate it.

The accordion component lets users show and hide sections of related content on a page.

<div class="app-example app-example--tabs">
  <div class="app-example__toolbar">
    <a href="/components/accordion/default/index.html" class="app-example__new-window" target="_blank">
      Open this
      <span class="govuk-visually-hidden">accordion</span>
      example in a new tab
    </a>
  </div>
  <iframe title="Accordion example" data-module="app-example-frame" class="app-example__frame app-example__frame--resizable app-example__frame--xl" src="/components/accordion/default/index.html" frameBorder="0" loading="lazy"></iframe>
</div>

<span id="options-accordion-example"></span>
<ul class="app-tabs" role="tablist">
  <li class="app-tabs__item js-tabs__item" role="presentation"><a href="#accordion-example-html" role="tab" aria-controls="accordion-example-html" data-track="tab-html">HTML</a></li>
  <li class="app-tabs__item js-tabs__item" role="presentation"><a href="#accordion-example-nunjucks" role="tab" aria-controls="accordion-example-nunjucks" data-track="tab-nunjucks">Nunjucks</a></li>
</ul>

  <div class="app-tabs__heading js-tabs__heading"><a href="#accordion-example-html" aria-controls="accordion-example-html" data-track="tab-html">HTML</a></div>

<div class="app-tabs__container js-tabs__container" id="accordion-example-html" role="tabpanel">
  <div class="app-example__code">
    <pre data-module="app-copy" tabindex="0"><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;govuk-accordion&quot;</span> <span class="hljs-attr">data-module</span>=<span class="hljs-string">&quot;govuk-accordion&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;accordion-default&quot;</span>&gt;</span>

<div class="govuk-accordion__section "> <div class="govuk-accordion__section-header"> <h2 class="govuk-accordion__section-heading"> <span class="govuk-accordion__section-button" id="accordion-default-heading-1"> Writing well for the web </span> </h2> </div> <div id="accordion-default-content-1" class="govuk-accordion__section-content" aria-labelledby="accordion-default-heading-1"> <p class='govuk-body'>This is the content for Writing well for the web.</p> </div> </div> <div class="govuk-accordion__section "> <div class="govuk-accordion__section-header"> <h2 class="govuk-accordion__section-heading"> <span class="govuk-accordion__section-button" id="accordion-default-heading-2"> Writing well for specialists </span> </h2> </div> <div id="accordion-default-content-2" class="govuk-accordion__section-content" aria-labelledby="accordion-default-heading-2"> <p class='govuk-body'>This is the content for Writing well for specialists.</p> </div> </div> <div class="govuk-accordion__section "> <div class="govuk-accordion__section-header"> <h2 class="govuk-accordion__section-heading"> <span class="govuk-accordion__section-button" id="accordion-default-heading-3"> Know your audience </span> </h2> </div> <div id="accordion-default-content-3" class="govuk-accordion__section-content" aria-labelledby="accordion-default-heading-3"> <p class='govuk-body'>This is the content for Know your audience.</p> </div> </div> <div class="govuk-accordion__section "> <div class="govuk-accordion__section-header"> <h2 class="govuk-accordion__section-heading"> <span class="govuk-accordion__section-button" id="accordion-default-heading-4"> How people read </span> </h2> </div> <div id="accordion-default-content-4" class="govuk-accordion__section-content" aria-labelledby="accordion-default-heading-4"> <p class='govuk-body'>This is the content for How people read.</p> </div> </div> </div>

  <div class="app-tabs__heading js-tabs__heading"><a class="app-tabs__heading-link" href="#accordion-example-nunjucks" aria-controls="accordion-example-nunjucks" data-track="tab-nunjucks">Nunjucks</a></div>
<div class="app-tabs__container js-tabs__container" id="accordion-example-nunjucks" role="tabpanel">
    

    <details class="govuk-details app-options" data-module="govuk-details" id="options-accordion-example-details">
Nunjucks macro options
      <p>
      Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.
      </p>
      <p>
      Some options are required for the macro to work; these are marked as "Required" in the option description.
      </p>
      <p>
      If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against  <a href="https://developer.mozilla.org/en-US/docs/Glossary/Cross-site_scripting">cross-site scripting exploits</a>.
      </p>
      
        <table class="govuk-table app-options__table" id="options-accordion-example--primary">
          <caption class="govuk-table__caption govuk-heading-m  govuk-visually-hidden">Primary options</caption>
          <thead class="govuk-table__head">
            <tr class="govuk-table__row">
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Name</th>
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Type</th>
              <th class="govuk-table__header" scope="col">Description</th>
            </tr>
          </thead>
          <tbody class="govuk-table__body">
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">id</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  Must be unique across the domain of your service (as the expanded state of individual instances of the component persists across page loads using <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage"><code>sessionStorage</code></a>). Used as an <code>id</code> in the HTML for the accordion as a whole, and also as a prefix for the <code>id</code>s of the section contents and the buttons that open them, so that those <code>id</code>s can be the target of <code>aria-labelledby</code> and <code>aria-control</code> attributes.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">headingLevel</th>
                <td class="govuk-table__cell ">integer</td>
                <td class="govuk-table__cell ">
                  
                  Heading level, from <code>1</code> to <code>6</code>. Default is <code>2</code>.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">classes</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  Classes to add to the accordion.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">attributes</th>
                <td class="govuk-table__cell ">object</td>
                <td class="govuk-table__cell ">
                  
                  HTML attributes (for example data attributes) to add to the accordion.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">hideAllSectionsText</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The text content of the &#39;Hide all sections&#39; button at the top of the accordion when all sections are expanded.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">hideSectionText</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The text content of the &#39;Hide&#39; button within each section of the accordion, which is visible when the section is expanded.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">hideSectionAriaLabelText</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  Text made available to assistive technologies, like screen-readers, as the final part of the toggle&#39;s accessible name when the section is expanded. Defaults to &#39;Hide this section&#39;.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">showAllSectionsText</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The text content of the &#39;Show all sections&#39; button at the top of the accordion when at least one section is collapsed.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">showSectionText</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The text content of the &#39;Show&#39; button within each section of the accordion, which is visible when the section is collapsed.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">showSectionAriaLabelText</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  Text made available to assistive technologies, like screen-readers, as the final part of the toggle&#39;s accessible name when the section is collapsed. Defaults to &#39;Show this section&#39;.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">items</th>
                <td class="govuk-table__cell ">array</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  An array of sections within the accordion.
                  
                  
                    See <a href="#options-accordion-example--items">items</a>.
                  </td>
              </tr>
            
          </tbody>
        </table>
      
        <table class="govuk-table app-options__table" id="options-accordion-example--items">
          <caption class="govuk-table__caption govuk-heading-m ">Options for items</caption>
          <thead class="govuk-table__head">
            <tr class="govuk-table__row">
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Name</th>
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Type</th>
              <th class="govuk-table__header" scope="col">Description</th>
            </tr>
          </thead>
          <tbody class="govuk-table__body">
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">heading.text</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  The title of each section. If <code>heading.html</code> is supplied, this is ignored.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">heading.html</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  The HTML content of the header for each section. Used as the title for each section. The header is inside the HTML <code>&lt;button&gt;</code> element, so you can only add <a href="https://html.spec.whatwg.org/#phrasing-content">phrasing content</a> to it.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">summary.text</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  Text content for summary line. If <code>summary.html</code> is supplied, this is ignored.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">summary.html</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The HTML content for the summary line. The summary line is inside the HTML <code>&lt;button&gt;</code> element, so you can only add <a href="https://html.spec.whatwg.org/#phrasing-content">phrasing content</a> to it.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">content.text</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  The text content of each section, which is hidden when the section is closed. If <code>content.html</code> is supplied, this is ignored.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">content.html</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  The HTML content of each section, which is hidden when the section is closed.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">expanded</th>
                <td class="govuk-table__cell ">boolean</td>
                <td class="govuk-table__cell ">
                  
                  Sets whether the section should be expanded when the page loads for the first time. Defaults to <code>false</code>.
                  
                  </td>
              </tr>
            
          </tbody>
        </table>
  <div class="app-example__code">
    <pre data-module="app-copy" tabindex="0"><code class="hljs js">{% <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;govuk/components/accordion/macro.njk&quot;</span> <span class="hljs-keyword">import</span> govukAccordion %}

{{ govukAccordion({ id: "accordion-default", items: [ { heading: { text: "Writing well for the web" }, content: { html: "<p class='govuk-body'>This is the content for Writing well for the web.</p>" } }, { heading: { text: "Writing well for specialists" }, content: { html: "<p class='govuk-body'>This is the content for Writing well for specialists.</p>" } }, { heading: { text: "Know your audience" }, content: { html: "<p class='govuk-body'>This is the content for Know your audience.</p>" } }, { heading: { text: "How people read" }, content: { html: "<p class='govuk-body'>This is the content for How people read.</p>" } } ] }) }}