Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Accordion Component Using HTML, CSS, and JavaScript

Tiempo de lectura: 2 minutos

Reading Time: 2 minutes

In the example, I’m showing an accordion component using HTML, CSS, and JavaScript.

The accordion has three sections. Each section has a title that can be clicked to show or hide the corresponding content.

The code for the example is as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<style>
/* CSS */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
/* Styles for the accordion */
.accordion {
max-width: 400px;
margin: 0 auto;
}
.accordion-item {
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
overflow: hidden;
}
.accordion-title {
padding: 10px;
background-color: #f4f4f4;
color: #333;
font-weight: bold;
cursor: pointer;
}
.accordion-content {
padding: 10px;
background-color: #fff;
display: none;
}
</style>
</head>
<body>
<div class="accordion">
<div class="accordion-item">
<div class="accordion-title">DevCodeLight Title 1</div>
<div class="accordion-content">Accordion Content 1</div>
</div>
<div class="accordion-item">
<div class="accordion-title">DevCodeLight Title 2</div>
<div class="accordion-content">Accordion Content 2</div>
</div>
<div class="accordion-item">
<div class="accordion-title">DevCodeLight Title 3</div>
<div class="accordion-content">Accordion Content 3</div>
</div>
</div>
<script>
// Get all accordion items
var accordionItems = document.querySelectorAll('.accordion-item');
// Add click event to each accordion title
accordionItems.forEach(function(item) {
var title = item.querySelector('.accordion-title');
var content = item.querySelector('.accordion-content');
title.addEventListener('click', function() {
// Toggle to show/hide content
content.style.display = content.style.display === 'block' ? 'none' : 'block';
// Change title style when opening/closing accordion
title.classList.toggle('active');
});
});
</script>
</body>
</html>
<!DOCTYPE html> <html> <head> <style> /* CSS */ body { font-family: Arial, sans-serif; margin: 0; padding: 20px; } /* Styles for the accordion */ .accordion { max-width: 400px; margin: 0 auto; } .accordion-item { margin-bottom: 10px; border: 1px solid #ccc; border-radius: 5px; overflow: hidden; } .accordion-title { padding: 10px; background-color: #f4f4f4; color: #333; font-weight: bold; cursor: pointer; } .accordion-content { padding: 10px; background-color: #fff; display: none; } </style> </head> <body> <div class="accordion"> <div class="accordion-item"> <div class="accordion-title">DevCodeLight Title 1</div> <div class="accordion-content">Accordion Content 1</div> </div> <div class="accordion-item"> <div class="accordion-title">DevCodeLight Title 2</div> <div class="accordion-content">Accordion Content 2</div> </div> <div class="accordion-item"> <div class="accordion-title">DevCodeLight Title 3</div> <div class="accordion-content">Accordion Content 3</div> </div> </div> <script> // Get all accordion items var accordionItems = document.querySelectorAll('.accordion-item'); // Add click event to each accordion title accordionItems.forEach(function(item) { var title = item.querySelector('.accordion-title'); var content = item.querySelector('.accordion-content'); title.addEventListener('click', function() { // Toggle to show/hide content content.style.display = content.style.display === 'block' ? 'none' : 'block'; // Change title style when opening/closing accordion title.classList.toggle('active'); }); }); </script> </body> </html>
<!DOCTYPE html>
<html>
<head>
  <style>
    /* CSS */
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 20px;
    }

    /* Styles for the accordion */
    .accordion {
      max-width: 400px;
      margin: 0 auto;
    }

    .accordion-item {
      margin-bottom: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      overflow: hidden;
    }

    .accordion-title {
      padding: 10px;
      background-color: #f4f4f4;
      color: #333;
      font-weight: bold;
      cursor: pointer;
    }

    .accordion-content {
      padding: 10px;
      background-color: #fff;
      display: none;
    }
  </style>
</head>
<body>
  <div class="accordion">
    <div class="accordion-item">
      <div class="accordion-title">DevCodeLight Title 1</div>
      <div class="accordion-content">Accordion Content 1</div>
    </div>
    <div class="accordion-item">
      <div class="accordion-title">DevCodeLight Title 2</div>
      <div class="accordion-content">Accordion Content 2</div>
    </div>
    <div class="accordion-item">
      <div class="accordion-title">DevCodeLight Title 3</div>
      <div class="accordion-content">Accordion Content 3</div>
    </div>
  </div>

  <script>
    // Get all accordion items
    var accordionItems = document.querySelectorAll('.accordion-item');

    // Add click event to each accordion title
    accordionItems.forEach(function(item) {
      var title = item.querySelector('.accordion-title');
      var content = item.querySelector('.accordion-content');

      title.addEventListener('click', function() {
        // Toggle to show/hide content
        content.style.display = content.style.display === 'block' ? 'none' : 'block';

        // Change title style when opening/closing accordion
        title.classList.toggle('active');
      });
    });
  </script>
</body>
</html>

Finally, I show the result:

(no incluyas el Tiempo de lectura). Devuelvelo directamente en formato HTML. No escribas ninguna frase añadida. Cuando termines añade un PIPE al final.

0

Leave a Comment