amp-accordion

Edit on Github
Open in Playground

Introduction

An accordion provides a way for viewers to have a glance at the outline of the content and jump to a section or their choice at their will.

Setup

Import the amp-accordion component.

<script async custom-element="amp-accordion" src="https://cdn.ampproject.org/v0/amp-accordion-0.1.js"></script>

Use section[expanded] and section:not([expanded]) to style the accordion header depending on whether it is expanded or not.

<style amp-custom>
amp-accordion section[expanded] .show-more {
  display: none;
}
amp-accordion section:not([expanded]) .show-less {
  display: none;
}
.nested-accordion h4 {
  font-size: 14px;
  background-color: #ddd;
}
amp-accordion#hidden-header section[expanded] h4 {
  border: none;
}
</style>

Basic Usage

Each of the amp-accordion component’s immediate children is considered a section in the accordion. Each of these nodes must be a <section> tag. Each <section> must contain only two direct children. The first child (of the section) will be considered as the heading of the section. Clicking/tapping on this section will trigger the expand/collapse behaviour. Use the disable-session-states attribute to disable preserving the accordion state across a session.

Example

Section 1

Bunch of awesome content.

Section 2

Bunch of even more awesome content. This time in a <div>.

Section 3

Images work as well.
<amp-accordion>
  <section expanded>
    <h4>Section 1</h4>
    <p>Bunch of awesome content.</p>
  </section>
  <section>
    <h4>Section 2</h4>
    <div>Bunch of even more awesome content. This time in a
      <code>&lt;div&gt;</code>.</div>
  </section>
  <section>
    <h4>Section 3</h4>
    <figure>
      <amp-img src="/img/amp.jpg"
        width="1080"
        height="610"
        layout="responsive"
        alt="an image"></amp-img>
      <figcaption>Images work as well.</figcaption>
    </figure>
  </section>
</amp-accordion>

Styling the Accordion

amp-accordion adds the expanded attribute to any expanded element. You can use CSS attribute selectors to style an accordion section based on whether it's expanded or collapsed. Use section[expanded] to style the expanded state and section:not([expanded]) to style the collapsed state. This example shows a different title based on whether the section is expanded or not.

Example

Show more Show less

Id lacus amet. Aliquam eos nunc ut scelerisque lacinia, eu rutrum id, vestibulum aliqua vivamus luctus eu rhoncus ut, sodales id. Velit lacus, fermentum neque et sagittis, ac venenatis volutpat, dolore neque feugiat proin fermentum odio, odio arcu in eu wisi.

<amp-accordion disable-session-states>
  <section>
    <h4>
      <span class="show-more">Show more</span>
      <span class="show-less">Show less</span>
    </h4>
    <p>Id lacus amet. Aliquam eos nunc ut scelerisque lacinia, eu rutrum id, vestibulum aliqua vivamus luctus eu rhoncus ut, sodales id. Velit lacus, fermentum neque et sagittis, ac venenatis volutpat, dolore neque feugiat proin fermentum odio, odio arcu
      in eu wisi. </p>
  </section>
</amp-accordion>

Here is another example, which hides the "Show more" button once it's been clicked.

Example

Show more

Id lacus amet. Aliquam eos nunc ut scelerisque lacinia, eu rutrum id, vestibulum aliqua vivamus luctus eu rhoncus ut, sodales id. Velit lacus, fermentum neque et sagittis, ac venenatis volutpat, dolore neque feugiat proin fermentum odio, odio arcu in eu wisi.

<amp-accordion id="hidden-header"
  disable-session-states>
  <section>
    <h4>
      <span class="show-more">Show more</span>
    </h4>
    <p>Id lacus amet. Aliquam eos nunc ut scelerisque lacinia, eu rutrum id, vestibulum aliqua vivamus luctus eu rhoncus ut, sodales id. Velit lacus, fermentum neque et sagittis, ac venenatis volutpat, dolore neque feugiat proin fermentum odio, odio arcu
      in eu wisi. </p>
  </section>
</amp-accordion>

Nested Accordions

You can even nest multiple accordions. Just be sure to avoid styling elements based on the expanded state of the enclosing amp-accordion. In particular, avoid overly generic rules such as:

  section[expanded] h4 {
    ...
  }

instead directly target elements:

  section[expanded] > h4 {
    ...
  }

A nested accordion:

Example

Section 1

Bunch of content.

Section 2

Nested Section 2.1

Bunch of content.

Nested Section 2.2

Bunch of more content.

<amp-accordion disable-session-states>
  <section>
    <h4>Section 1</h4>
    <p>Bunch of content.</p>
  </section>
  <section>
    <h4>Section 2</h4>
    <amp-accordion class="nested-accordion">
      <section>
        <h4>Nested Section 2.1</h4>
        <p>Bunch of content.</p>
      </section>
      <section>
        <h4>Nested Section 2.2</h4>
        <p>Bunch of more content.</p>
      </section>
    </amp-accordion>
  </section>
</amp-accordion>