How to create Nested Accordion using Google AMP amp-accordion?


Nested accordion menus are an effective way to organize and present large amounts of information in a compact and intuitive manner. With the growing importance of mobile devices in accessing the internet, it has become increasingly important to optimize web pages for faster loading and improved user experience. This is where the Google Accelerated Mobile Pages (AMP) project comes into play, offering a streamlined and fast-loading way of creating web pages for mobile devices. In this article, we will explore how to create nested accordion menus using Google AMP’s amp-accordion component, providing a clear and organized way to present complex information on your mobile web pages.

Google AMP

Google's Accelerated Mobile Pages (AMP) initiative is a venture that began in 2015 with the objective of enhancing mobile web browsing by facilitating swifter and more responsive loading of web pages. This project offers a method to develop web pages that are nimble and load quickly, enabling them to be rendered almost immediately on mobile devices.

AMP pages are engineered to reduce the quantum of data that necessitates downloading and processing by a mobile device, thereby accelerating page load times and augmenting the overall user experience. AMP pages are usually simplified and condensed renditions of standard web pages, containing straightforward HTML, streamlined CSS, and restricted JavaScript.

Accordion

An accordion is a UI element that allows users to expand and collapse content sections on a webpage. It provides a compact and organized way of presenting information, especially when there is a lot of content to be displayed. An accordion typically consists of multiple content sections, each with a header. By clicking on the header, the corresponding content section can be expanded or collapsed, allowing the user to view the content or hide it. The accordion helps to reduce clutter and improve the user experience by allowing users to easily access and view the content they are interested in.

Syntax

<amp-accordion [id=”<accordion-id>”]
   [expand-single-section]
   [disable-session-states]
   [animate]
   [layout=”container”]
   [class=”<class-name>”]
   [style=”<style-properties>”]>
   <!—accordion sections go here 
</amp-accordion>

Amp-accordion Tag

The amp-accordion tag is an AMP component that allows you to create an accordion on your webpage. The amp-accordion component consists of a set of expandable and collapsible sections, each with a header and content. The header is usually a clickable element that toggles the visibility of the content section. When a user clicks on the header, the content section expands or collapses, depending on its current state.

Let’s go through each of the attributes and their possible values −

  • Id(optional) − Specifies a unique ID for the accordion component. This can be used to apply CSS styles or to target the component with JavaScript.

  • Expand-single-section (optional) − If present, only one section can be expanded at a time. When a new section is expanded, the previously expanded section will be collapsed.

  • Disable-session-states (optional) − If present, session states will be disabled for the component. This means that the state of the component will not be saved between page loads.

  • Animate (optional) − If present, the accordion will be animated when sections are expanded or collapsed.

  • Layout (optional) − Specifies the layout for the accordion. The default value is “container”, which sets the container to a fixed width and height. Other possible values include “fixed-height” and “flex-item”.

  • Class (optional) − Specifies one or more CSS class names to apply to the component.

  • Style (optional) − Specifies one or more inline CSS style properties to apply to the component.

Approach

To create a nested accordion using the amp-accordion component in Google AMP, you can follow the steps outlined below −

  • Include the AMP script in the <head> of your HTML file. This can be done by adding the following line: <script async src=https://cdn.ampproject.org/v0.js></script>.

  • Define your outer accordion using the amp-accordion tag. Within the accordion, you can define multiple sections using the <section> tag.

  • For each section, add a header and content. The header should be enclosed in an <h2> tag, while the content can be any HTML tags that you want to display.

  • To create a nested accordion, add another amp-accordion tag within the content of one of the sections of the outer accordion.

  • Within the inner accordion, you can define multiple sections using the <section> tag. Each section should have a header and content, just like the sections in the outer accordion.

  • To style the accordion, you can add custom CSS to your file. For example, you could add a border to the accordion or change the font size or color.

  • Once you have set up your accordion, users can click on the headers to expand or collapse the content sections. This provides a way to organize information and make it easier for users to find what they are looking for.

Example

The following HTML code is designed to create nested accordions using the Google AMP amp-accordion. The code includes two amp-accordion elements, one inside the other, to create a nested accordion structure. The outer-accordion and inner-accordion classes are used to style the accordions with borders and margins. The content for the accordions is added using section, h2, h3, and p tags. When the user clicks on the accordion headings, the content expands or collapses according to the Google AMP amp-accordion functionality.

<!DOCTYPE html>
<html>
<head>
   <title>How to create Nested Accordion using Google AMP amp-accordion?</title>
   <script async src="https://cdn.ampproject.org/v0.js"></script>
   <script async custom-element="amp-accordion" src="https://cdn.ampproject.org/v0/amp-accordion-0.1.js"></script>
   <style amp-custom>
      .outer-accordion {
         border: 1px solid #ccc;
         margin: 10px 0;
      }
      .inner-accordion {
         border: 1px solid #eee;
         margin: 10px 0;
      }
   </style>
</head>
<body>
   <h4>How to create Nested Accordion using Google AMP amp-accordion?</h4>
   <amp-accordion class="outer-accordion">
      <section>
         <h2>Outer Section 1</h2>
         <amp-accordion class="inner-accordion">
            <section>
               <h3>Inner Section 1</h3>
               <p>Content for inner section 1.</p>
            </section>
            <section>
               <h3>Inner Section 2</h3>
               <p>Content for inner section 2.</p>
            </section>
         </amp-accordion>
      </section>
      <section>
         <h2>Outer Section 2</h2>
         <p>Content for outer section 2.</p>
      </section>
   </amp-accordion>
</body>
</html>

Conclusion

To summarize, crafting a nested accordion on an AMP page utilizing Google AMP's amp-accordion component is an uncomplicated method that empowers you to order content in a compressed and intuitive fashion. The nested accordion formation is accomplished by embedding amp-accordion components within each other, and the product is a versatile and interactive UI element that can refine the user experience on your AMP page. Through this write-up, our aspiration is to have furnished an explicit comprehension of how to establish a nested accordion using amp-accordion, and how it can heighten the worth of your AMP page.

Updated on: 11-Apr-2023

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements