AMP
  • websites

amp-accordion

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>

Basic usage

An amp-accordion consists of multiple sections. Each <section> must have two direct children. The first child 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.

Section 1

Bunch of awesome content.

Section 2

Bunch of even more awesome content. This time in a
.

Section 3

Images work as well.
<amp-accordion class="sample">
  <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>
        <div>
      </code>.</div>
  </section>
  <section>
    <h4>Section 3</h4>
    <figure>
      <amp-img src="/static/samples/img/clean-1.jpg"
        layout="responsive"
        width="400"
        height="710"></amp-img>
      <figcaption>Images work as well.</figcaption>
    </figure>
  </section>
</amp-accordion>
Abrir esse snippet no playground

Custom styling

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.:

amp-accordion section[expanded] .show-more {
  display: none;
}

amp-accordion section:not([expanded]) .show-less {
  display: none;
}

This example shows a different title based on whether the section is expanded or not.

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 class="sample"
  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>
Abrir esse snippet no playground

Hidden header

This sample which hides the "Show more" button once it's been clicked.

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 class="sample 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>
Abrir esse snippet no playground

Nested accordion

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

Section 1

Bunch of content.

Section 2

Nested Section 2.1

Bunch of content.

Nested Section 2.2

Bunch of more content.

<amp-accordion class="sample"
  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>
Abrir esse snippet no playground

Auto-collapsing accordion

The expand-single-section attribute forces the accordion to only allow one expanded section at any given time. Expanding one section will cause any other open sections to close.

Section 1

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.

Section 2

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.

Section 3

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 class="sample"
  expand-single-section>
  <section>
    <h4>Section 1</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>
  <section>
    <h4>Section 2</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>
  <section>
    <h4>Section 3</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>
Abrir esse snippet no playground

Animated accordion

The animate attribute animates the expansion and collapse of accordion sections.

Section 1

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.

Section 2

Section 3

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 class="sample"
  animate>
  <section>
    <h4>Section 1</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>
  <section>
    <h4>Section 2</h4>
    <amp-img src="/static/samples/img/clean-1.jpg"
      layout="responsive"
      width="400"
      height="710"></amp-img>
  </section>
  <section>
    <h4>Section 3</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>
Abrir esse snippet no playground
Precisa de mais explicação?

If the explanations on this page don't cover all of your questions feel free to reach out to other AMP users to discuss your exact use case.

Ir para Stack Overflow
An unexplained feature?

The AMP project strongly encourages your participation and contributions! We hope you'll become an ongoing participant in our open source community but we also welcome one-off contributions for the issues you're particularly passionate about.

Editar exemplos do GitHub