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.
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.
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>
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; }
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.
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>
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 */ }
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.
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!
Page loaded in 26.15 ms