Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

W3.CSS Accordions

Share

Basic Accordion

Accordions show and hide sections of content.

They are useful when you want to organize a lot of information in a small space.

Use a button to open and close the accordion content.

The w3-hide class hides the content.

JavaScript adds or removes the w3-show class:

Some text...

Example

<button onclick="myFunction('Demo1')" class="w3-button w3-block">
  Open the Section
</button>

<div id="Demo1" class="w3-container w3-hide">
  <p>Some text...</p>
</div>

<script>
function myFunction(id) {
  document.getElementById(id).classList.toggle("w3-show");
}
</script>

Try It Yourself »


Accordion vs. Dropdown

Accordions and dropdowns both show hidden content, but they behave differently:

AccordionDropdown
Content becomes part of the pageContent appears over the page
Often spans the available widthOften appears as a smaller menu
Useful for sections of contentUseful for menus and choices

Accordion Buttons

Accordion buttons are often set to span the entire width of its parent element (100% width) with w3-block.

Use w3-left-align to align the button text to the left:

Some text...

Example

<button onclick="myFunction('Demo1')" class="w3-button w3-block w3-left-align w3-green">
  Open Section
</button>

<div id="Demo1" class="w3-hide w3-container">
  <p>Some text...</p>
</div>

Try It Yourself »



Accordion Content

Accordion content can contain links, buttons, images, lists, or any other HTML content.

Example

<button onclick="myFunction('Demo1')" class="w3-button w3-block w3-left-align">Menu</button>

<div id="Demo1" class="w3-hide">
  <a href="#" class="w3-button w3-block w3-left-align">Link 1</a>
  <a href="#" class="w3-button w3-block w3-left-align">Link 2</a>
  <a href="#" class="w3-button w3-block w3-left-align">Link 3</a>
</div>

Try It Yourself »


Active Accordion Buttons

You can change the button style when an accordion is open:

Some text...

Example

function myFunction(button, id) {
  const element = document.getElementById(id);
  element.classList.toggle("w3-show");
  button.classList.toggle("w3-black");
  button.classList.toggle("w3-red");
}

Try It Yourself »


Accordion Width

By default, the width of block is 100%. To override this, change the CSS width property of the accordion container:

Some text..

Some text..

Some text..

Some text..

Example

<div class="w3-light-grey" style="width:50%">
  <button onclick="myFunction('Demo1')" class="w3-button w3-block">
    50%
  </button>
  <div id="Demo1" class="w3-hide">
    <p>Some text..</p>
  </div>
</div>

Try it Yourself »


What You Have Learned

In this chapter you have learned how to:

  • Show and hide content with w3-hide and w3-show
  • Create full-width accordion buttons with w3-block
  • Put links, lists, and other HTML inside accordions
  • Style active accordion buttons

More Examples

Explore more ways to use accordions.


Accordion with a List

  • Jill
  • Eve
  • Adam

Try It Yourself »

Example

<button onclick="myFunction('Demo1')" class="w3-button w3-block w3-left-align">
Accordion</button>

<div id="Demo1" class="w3-hide">
  <ul class="w3-ul">
    <li>Jill</li>
    <li>Eve</li>
    <li>Adam</li>
  </ul>
</div>

Try it Yourself »


Accordion in a Sidebar

Accordions can also be used inside sidebars (You will learn more about sidebars later).

Try It Yourself »


Animated Accordions

Use any of the w3-animate classes to fade, zoom or slide in the accordion content:

Example

<div id="Demo1" class="w3-hide w3-animate-zoom">

Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.