DEV Community

Sekti Wicaksono
Sekti Wicaksono

Posted on

Creating Accordion using HTML and CSS

Hello guys, I want to show you how to create Accordion using HTML and CSS.

Let start with the html

<details>
  <summary>I am awesome</summary>
  <p>Hi there this is an awesome paragraph</p>
</details>
<details>
  <summary>I am awesome</summary>
  <p>Hi there this is an awesome paragraph</p>
</details>
<details>
  <summary>I am awesome</summary>
  <p>Hi there this is an awesome paragraph</p>
</details>
<details>
  <summary>I am awesome</summary>
  <p>Hi there this is an awesome paragraph</p>
</details>
Enter fullscreen mode Exit fullscreen mode

Now let's give it a style to the accordion.

html {
  height: 100vh;
}

body {
  height: 100%;
  display: grid;
  place-content: center;

  font-family: 'Montserrat', sans-serif;
}

details {
  background: mistyrose;
  padding: 0;
  width: 20rem;
  background: #eee;
  border-bottom: 1px solid #242424;
  box-shadow: 0 2px 1px 2px #242424;
}

details:nth-child(1) {
  border-top: 1px solid #242424;
}

details p {
  background-color: #9effed;
  padding: 1em;
  margin-top: 0;
  margin-bottom: 0;
}
details[open] {
  background-color: #ddd; 
}
details[open] summary~* {
  animation: anim .5s ease-in;
}

summary {
  padding: 1em;
  margin: 0;
  outline: none;
  cursor: pointer;
}

details:hover{
  background-color: #ddd; 
}

@keyframes anim {
  0% {
    opacity: 0;
    margin-top: -50px;
  }

  100% {
    opacity: 1;
    margin-left: 0;
  }
}
Enter fullscreen mode Exit fullscreen mode

Result:

Top comments (0)