Develop Accordion Menu using HTML, CSS and JavaScript

Let us develop a nice accordion menu using HTML, CSS and JavaScript

<link href='https://unpkg.com/boxicons@2.1.1/css/boxicons.min.css' rel='stylesheet'>
<!--1--->
<div class="accordion__content">
  <h2>Accordion Design</h2>

  <div class="accordion__item">
    <header class="item__header">
      <h4 class="item__question">
        What is your name? </h4>
      <div class="item__icon">
        <i class='bx bx-chevron-down'></i>
      </div>
    </header>
    <div class="item__content">
      <p class="item__answer">
        My name is Akin, my nickname is Àgbà Akin.
      </p>
      <p class="item__answer">
      </p>
    </div>
  </div>
  <div class="accordion__item">
    <header class="item__header">
      <h4 class="item__question">
        What is your favorite color? </h4>
      <div class="item__icon">
        <i class='bx bx-chevron-down'></i>
      </div>
    </header>
    <div class="item__content">

      <p class="item__answer">
        Red
      </p>
    </div>
  </div>
  <div class="accordion__item">
    <header class="item__header">
      <h4 class="item__question">
        Are you in a relationship? </h4>
      <div class="item__icon">
        <i class='bx bx-chevron-down'></i>
      </div>
    </header>
    <div class="item__content">
      <p class="item__answer">
        No dy question me guy!!! </p>
      <p class="item__answer">
      </p>
    </div>
  </div>
  <div class="accordion__item">
    <header class="item__header">
      <h4 class="item__question">
        Who's your favorite player? </h4>
      <div class="item__icon">
        <i class='bx bx-chevron-down'></i>
      </div>
    </header>
    <div class="item__content">
      <p class="item__answer">
      </p>
      <p class="item__answer"> Ronaldo
      </p>
    </div>
  </div>
</div>

@import url("https://fonts.googleapis.com/css2?family=Jost:wght@500&display=swap");
:root {
  --body-bg: #f9f9f9;
  --border: #ddd;
  --shadow: 0px 4px 16px rgba(0, 0, 0, 0.09);
  --icon-bg: #dedede;
}
body {
  font-family: "Poppins", jost;
  background: var(--body-bg);
  color: #444;
  display: grid;
  place-items: center;
  min-height: 100vh;
}

.accordion__content {
  max-width: 570px;
  margin: 0 auto;
  padding: 2rem;
  background: #fff;
  box-shadow: var(--shadow);
  border-radius: 8px;
}
.accordion__item {
  display: flex;
  flex-direction: column;
  padding: 0 1rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);

  background: var(--body-bg);
  margin-bottom: 0.5em;
}
.item__header {
  display: flex;
  justify-content: space-between;
  column-gap: 0.2em;
}

.item__icon {
  margin-top: 1rem;
  flex: 0 0 25px;
  display: grid;
  place-items: center;
  font-size: 1.25rem;
  height: 25px;
  width: 25px;
  border-radius: 4px;
  background: var(--icon-bg);
  cursor: pointer;
  box-shadow: var(--shadow);
}
.item__icon i {
  transition: all 0.25s cubic-bezier(0.5, 0, 0.1, 1);
}
.item__question {
  font-size: 1em;
  line-height: 1;
  font-weight: 500;
}

.active .item__icon i {
  transform: rotate(180deg);
}

.active .item__question {
  font-weight: 500;
}

.item__content {
  max-height: 0;
  overflow: hidden;
  transition: all 300ms ease;
}

.item__answer {
  line-height: 150%;
  opacity: 0.8;
}

const accordionBtns = document.querySelectorAll(".item__header");

accordionBtns.forEach((accordion) => {
  accordion.onclick = function () {
    this.classList.toggle("active");

    let content = this.nextElementSibling;
    console.log(content);

    if (content.style.maxHeight) {
      //this is if the accordion is open
      content.style.maxHeight = null;
    } else {
      //if the accordion is currently closed
      content.style.maxHeight = content.scrollHeight + "px";
      console.log(content.style.maxHeight);
    }
  };
});

2 Comments

  1. Mohammed kabiru says:

    Good job Bro more talent

Leave a Reply

Your email address will not be published.