EN ES
Home > Web development > HTML Tutorials > How to Create an Accordion System Using Only HTML and CSS

How to Create an Accordion System Using Only HTML and CSS

Diego Cortés
Diego Cortés
September 29, 2024
How to Create an Accordion System Using Only HTML and CSS

An accordion system is an excellent way to display information in a user interface, allowing content to expand or collapse as the user interacts with it. In this article, you will learn how to create an accordion system using only HTML and CSS, without any JavaScript.

What is an Accordion?

An accordion is a design component that allows you to display and hide additional content. It is particularly useful on web pages where space is limited and you want to keep the interface clean and organized. Accordions can be used to show descriptions, lists, FAQs, and much more.

Advantages of Using an Accordion System

  • Space Saving: They allow information to be presented compactly.
  • Interactivity: They encourage user engagement.
  • Organization: They help structure information clearly.

Basic HTML Structure

To create an accordion, we first need to establish the basic HTML structure. The structure will consist of several div elements, each containing a button and the associated content.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Accordion System</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="accordion">
        <div class="accordion-item">
            <input type="checkbox" id="section1">
            <label for="section1" class="accordion-header">Section 1</label>
            <div class="accordion-content">
                <p>This is the content of section 1.</p>
            </div>
        </div>
        <div class="accordion-item">
            <input type="checkbox" id="section2">
            <label for="section2" class="accordion-header">Section 2</label>
            <div class="accordion-content">
                <p>This is the content of section 2.</p>
            </div>
        </div>
        <div class="accordion-item">
            <input type="checkbox" id="section3">
            <label for="section3" class="accordion-header">Section 3</label>
            <div class="accordion-content">
                <p>This is the content of section 3.</p>
            </div>
        </div>
    </div>
</body>
</html>

Breakdown of the HTML Structure

  • <div class="accordion">: The main container for the accordion.
  • <h2 class="accordion-header">: The section header, which will serve as the button to expand or collapse the content.
  • <div class="accordion-content">: The container for the expandable content that will be shown when the header is clicked.

CSS Styles for the Accordion

Once we have our HTML structure, the next step is to apply CSS styles so that our accordion looks attractive and functional. Here’s an example of styles you can use:

body {
    font-family: Arial, sans-serif;
}

.accordion {
    width: 80%;
    margin: 20px auto;
    border: 1px solid #ccc;
    border-radius: 5px;
    overflow: hidden;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.accordion-item {
    border-bottom: 1px solid #ccc;
    padding: 20px 4px;
}

.accordion-item:last-child {
    border-bottom: none;
}

.accordion-header {
    background: #007BFF;
    color: #ffffff;
    padding: 15px;
    cursor: pointer;
    transition: background 0.3s ease;
}

.accordion-header:hover {
    background: #0056b3;
}

.accordion-content {
    display: none;
    padding: 15px;
    background: #f9f9f9;
    border-top: 1px solid #ccc;
}

input[type="checkbox"] {
    display: none;
}

input[type="checkbox"]:checked + .accordion-header + .accordion-content {
    display: block;
}

Description of the Styles

  • General Style: The accordion is set to a width of 80% with automatic margins to center it.
  • Accordion Header: Background color, text color, and padding are applied. Additionally, a hover effect is added.
  • Accordion Content: Initially hidden (display: none). It is displayed when the header is active.

Interactivity Using Only CSS

Although you would typically need JavaScript to manage the interaction of an accordion, in this case, we will take advantage of the CSS :checked selector along with hidden <input> elements. This will allow us to change the class of the headers so that the contents can be shown and hidden.

Modifying the HTML Structure

Next, we will make a small modification to the HTML structure to include hidden inputs. We will replace the header with a <label> that is linked to the input.

<div class="accordion">
    <input type="checkbox" id="section1">
    <label for="section1" class="accordion-header">Section 1</label>
    <div class="accordion-content">
        <p>This is the content of section 1.</p>
    </div>

    <input type="checkbox" id="section2">
    <label for="section2" class="accordion-header">Section 2</label>
    <div class="accordion-content">
        <p>This is the content of section 2.</p>
    </div>

    <input type="checkbox" id="section3">
    <label for="section3" class="accordion-header">Section 3</label>
    <div class="accordion-content">
        <p>This is the content of section 3.</p>
    </div>
</div>

Adjustments to the CSS Styles

Now, we will modify the CSS a bit to accommodate the new structure. Instead of using the active class, we will reference the state of the input:

.accordion-header {
    /* ...same styles as before... */
}

.accordion-content {
    display: none; /* Hide content by default */
}

input[type="checkbox"]:checked + .accordion-header + .accordion-content {
    display: block; /* Show content when the checkbox is checked */
}

Final Results

With the changes made, you will have a functional accordion that allows users to expand and collapse sections of content using just HTML and CSS. This implementation is very efficient and easy to incorporate into any web project.

Conclusion

Creating an accordion system using only HTML and CSS is a simple task that can significantly enhance the user experience on your website. By following the steps outlined in this article, you can ensure that your page design is clean and easy to navigate. So feel free to try it out and adapt the design to your needs.

With this article, you're ready to implement an accordion system on your own website. Try, experiment, and customize as you wish!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 48.66 ms