Related Articles

Related Articles

How to create Animated accordion using Google AMP amp-accordion ?
  • Last Updated : 25 Oct, 2020

 

Sometimes we have a lot of content to display and to make the website look pretty and short we make use of the collapsible text boxes. Collapsible text boxes are that division which is the combination of heading and content, generally only heading is visible but when it is hit the content is displayed.

Setup:

You have to import amp-accordion component in your header to use this tag.

HTML



filter_none

edit
close

play_arrow

link
brightness_4
code

<script async custom-element="amp-accordion" src=
</script>

chevron_right


To make an Animated accordion use the code mentioned below. The animate attribute slows down the speed of collapsing and expansion of the text box making it look animated.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<amp-accordion class="sample" animate>
    <!-- Code -->
</amp-accordion>

chevron_right


Example:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!doctype html>
<html amp>
  
<head>
    <meta charset="utf-8">
    <title>Google amp amp-accordion</title>
  
    <script async src=
    </script>
      
    <script async custom-element="amp-accordion" 
    </script>
      
    <link rel="canonical" href=
      
    <meta name="viewport" content=
"width=device-width,minimum-scale=1,initial-scale=1">
  
    <style amp-boilerplate>
        body {
            -webkit-animation: -amp-start 8s 
                steps(1, end) 0s 1 normal both;
  
            -moz-animation: -amp-start 8s 
                steps(1, end) 0s 1 normal both;
  
            -ms-animation: -amp-start 8s 
                steps(1, end) 0s 1 normal both;
  
            animation: -amp-start 8s 
                steps(1, end) 0s 1 normal both;
        }
  
        @-webkit-keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
  
        @-moz-keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
  
        @-ms-keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
  
        @-o-keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
  
        @keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
    </style>
  
    <noscript>
        <style amp-boilerplate>
            body {
                -webkit-animation: none;
                -moz-animation: none;
                -ms-animation: none;
                animation: none
            }
        </style>
    </noscript>
  
    <style amp-custom>
        amp-accordion section[expanded] .show-more {
            display: none;
        }
  
        /* These styles are not required for 
            the samples to work */
        :root {
            --space-2: 1rem;
        }
  
        amp-accordion.sample {
            margin: var(--space-2);
        }
  
        h1,
        h4 {
            color: green;
        }
  
        h1 {
            text-align: center;
        }
  
        section {
            color: crimson;
        }
    </style>
    <meta name="robots" content="noindex, nofollow">
</head>
  
<body>
    <h1>
        Geeks For Geeks
    </h1>
      
    <amp-accordion class="sample" animate>
        <section>
            <h4>Section 1</h4>
  
              
<p>
                GeeksforGeeks is a Computer Science 
                portal for geeks. It contains well 
                written, well thought and well explained
                computer science and programming 
                articles, quizzes etc.
            </p>
  
        </section>
  
        <section>
            <h4>Section 2</h4>
              
              
<p>
                GeeksforGeeks is a Computer Science 
                portal for geeks. It contains well 
                written, well thought and well explained
                computer science and programming 
                articles, quizzes etc.
            </p>
  
  
        </section>
  
        <section>
            <h4>Section 3</h4>
              
              
<p>
                GeeksforGeeks is a Computer Science 
                portal for geeks. It contains well 
                written, well thought and well explained
                computer science and programming 
                articles, quizzes etc.
            </p>
  
        </section>
    </amp-accordion>
</body>
  
</html>

chevron_right


Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :