how create accordion with js html css
Creating an accordion involves using HTML for the structure, CSS for styling, and JavaScript to add the interactivity needed for the accordion to open and close. Here’s a simple example to guide you through creating a basic accordion:
HTML
The HTML structure consists of a container (div
) for each item of the accordion. Each item has a button that the user will click to toggle the visibility of the content that is associated with it.
<div class="accordion">
<div class="accordion-item">
<button class="accordion-button">Section 1</button>
<div class="accordion-content">
<p>This is the content of Section 1.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-button">Section 2</button>
<div class="accordion-content">
<p>This is the content of Section 2.</p>
</div>
</div>
<!-- Add more accordion items as needed -->
</div>
CSS
The CSS is used to style the accordion. Initially, the content of each accordion item is hidden. When an item is active, its content will be displayed.
.accordion-content {
display: none;
overflow: hidden;
transition: max-height 0.2s ease-out;
}
/* Style the accordion buttons */
.accordion-button {
cursor: pointer;
padding: 10px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: background-color 0.2s ease;
}
/* Optionally style the active class or use it in JS to manage active state */
.accordion-button.active {
background-color: #ccc;
}
JavaScript
The JavaScript adds functionality to the accordion. When a user clicks an accordion button, the script toggles the visibility of the associated content.
document.querySelectorAll('.accordion-button').forEach(button => {
button.addEventListener('click', () => {
const accordionContent = button.nextElementSibling;
button.classList.toggle('active');
if (button.classList.contains('active')) {
accordionContent.style.display = 'block';
} else {
accordionContent.style.display = 'none';
}
});
});
full example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>accordion</title>
<!-- HTML -->
<!-- Custom Styles -->
<style>
.accordion-content {
display: none;
overflow: hidden;
transition: max-height 0.2s ease-out;
background:#B39EFB
}
/* Style the accordion buttons */
.accordion-button {
cursor: pointer;
padding: 10px;
width: 90%;
text-align: left;
border: 10px;
outline: none;
transition: background-color 0.2s ease;
background:#9EFB9E;
margin:10px;
height:70px;
font-size: 25px;
}
/* Optionally style the active class or use it in JS to manage active state */
.accordion-button.active {
background-color: #ccc;
}
</style>
</head>
<body>
<div class="accordion">
<div class="accordion-item">
<button class="accordion-button">Section 1</button>
<div class="accordion-content">
<p>This is the content of Section 1.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-button">Section 2</button>
<div class="accordion-content">
<p>This is the content of Section 2.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-button">Section 3</button>
<div class="accordion-content">
<p>This is the content of Section 3.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-button">Section 4</button>
<div class="accordion-content">
<p>This is the content of Section 4.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-button">Section 5</button>
<div class="accordion-content">
<p>This is the content of Section 5.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-button">Section 6</button>
<div class="accordion-content">
<p>This is the content of Section 6.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-button">Section 7</button>
<div class="accordion-content">
<p>This is the content of Section 7.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-button">Section 8</button>
<div class="accordion-content">
<p>This is the content of Section 8.</p>
</div>
</div>
<!-- Add more accordion items as needed -->
</div>
<script>
document.querySelectorAll('.accordion-button').forEach(button => {
button.addEventListener('click', () => {
const accordionContent = button.nextElementSibling;
button.classList.toggle('active');
if (button.classList.contains('active')) {
accordionContent.style.display = 'block';
} else {
accordionContent.style.display = 'none';
}
});
});
</script>
</body>
</html>
This example demonstrates the basic functionality of an accordion. You can enhance it with more CSS for better styling and modify the JavaScript code to include animations or to ensure that only one accordion item is open at a time.