CSS Accordion

<!DOCTYPE html>
<html>
<head>
  <style>
.accordion input {
  display: none;
}
.accordion label {
  display: block;
  padding: 10px;
  background: #333;
  color: white;
  cursor: pointer;
  margin-top: 5px;
}
.accordion div {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s;
  background: #f4f4f4;
  padding: 0 10px;
}
.accordion input:checked + label + div {
  max-height: 100px;
  padding: 10px;
}
</style> </head> <body> <div class="accordion">
&lt;input type="checkbox" id="q1"&gt;
&lt;label for="q1"&gt;What is CSS?&lt;/label&gt;
&lt;div&gt;CSS stands for Cascading Style Sheets.&lt;/div&gt;

&lt;input type="checkbox" id="q2"&gt;
&lt;label for="q2"&gt;Why use CSS?&lt;/label&gt;
&lt;div&gt;It makes websites look beautiful.&lt;/div&gt;
</div> </body> </html>

Explanation:

  • Uses hidden checkboxes + CSS :checked.
  • Expands/collapses without JS.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *