HTML 및 CSS를 사용하여 아코디언 만들기

6201 단어 htmlcss
안녕하세요 여러분, HTML과 CSS를 사용하여 Accordion을 만드는 방법을 보여드리고자 합니다.

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>


이제 아코디언에 스타일을 지정해 보겠습니다.

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;
  }
}


결과:

좋은 웹페이지 즐겨찾기