DEV Community

Sekti Wicaksono
Sekti Wicaksono

Posted on

1 1

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:

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs