Create a accordion with html css and javascript
create a accordion with html css and javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.accordion {
width: 300px;
border: 1px solid #ccc;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.accordion-section {
border-bottom: 1px solid #ccc;
}
.accordion-header {
padding: 10px;
cursor: pointer;
background-color: #f5f5f5;
user-select: none;
}
.accordion-body {
padding: 10px;
display: none;
}
</style>
</head>
<body>
<div class="accordion">
<div class="accordion-section">
<div class="accordion-header" onclick="toggleSection(1)">Section 1</div>
<div class="accordion-body" id="section1">This is the content for Section 1.</div>
</div>
<div class="accordion-section">
<div class="accordion-header" onclick="toggleSection(2)">Section 2</div>
<div class="accordion-body" id="section2">This is the content for Section 2.</div>
</div>
<div class="accordion-section">
<div class="accordion-header" onclick="toggleSection(3)">Section 3</div>
<div class="accordion-body" id="section3">This is the content for Section 3.</div>
</div>
</div>
<script>
function toggleSection(sectionNumber) {
var section = document.getElementById('section' + sectionNumber);
section.style.display = (section.style.display === 'block') ? 'none' : 'block';
// Hide other sections
for (var i = 1; i <= 3; i++) {
if (i !== sectionNumber) {
document.getElementById('section' + i).style.display = 'none';
}
}
}
</script>
</body>
</html>

More in 21 days of HTML CSS JS
See all →- Ultimate Guide to Free Web Development Resources In today’s digital age, web development is a crucial skill that opens up numerous opportunities. Whether you’re a beginner looking to build...
- 10 Effective Ways to Center a Div Hello, myself Ajink. In this article, we will explore the various techniques to center a div element on a web page. Centering a div might se...
- Learn HTML HTML Document Structure: : Defines the root of an HTML document. : Contains meta-information about the document. : Sets the title of the doc...
- How to Create an Valentine’s Day Website? How to Create an Valentine’s Day Website , lets get started Demo : Here is Complete SourceCode : body background-color: #fde8e8; font-family...
- Create a virtual keyboard that users can interact with on the webpage. Virtual Keyboard body font-family: 'Arial', sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center;...
- Create a virtual canvas where users can draw and paint using HTML5 canvas and JavaScript. Hey , I am Ajink in this post we will create a virtual canvas where you can draw and paint using html5 and javascript ! Virtual Canvas body...