Accordion
Accordion is one of most important button class of CSS. It
is most important button class during designing. Accordion plays a vital role
when we want to toggle between hiding and showing large amount of content. Accordion
allows us to hide content that is not immediately relevant to user.
Creating an Accordion we have to follow following steps:
Step 1:
<body>
<h2>Accordion</h2>
<button class="accordion"
style="background-color:red;">Section 1</button>
<div class="panel">
<p>Lorem ipsum
dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<button class="accordion"
style="background-color:white;">Section 2</button>
<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat.</p>
</div>
<button class="accordion"
style="background-color:green;">Section 3</button>
<div class="panel">
<p>Lorem ipsum
dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</body>
Step 2:
<style>
button.accordion {
background-color:
#eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
button.accordion.active, button.accordion:hover {
background-color:
#ddd;
}
div.panel {
padding: 0 18px;
display: none;
background-color:
white;
}
div.panel.show {
display: block;
}
</style>
Step 3:
<script>
var acc =
document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick =
function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
</script>
for live demonstration click here
Comments
Post a Comment