본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

드롭다운 (Dropdown)

원문 보기 위키 갱신

드롭다운 메뉴와 커스텀 셀렉트를 JavaScript 없이 최소한의 시맨틱 HTML만으로 만들 수 있어요. <details class="dropdown">을 래퍼로, <summary>와 <ul>을 직계 자식으로 사용해서 구성해요. Nav 안에 있지 않다면 드롭다운은 기본적으로 width: 100%;를 가져요.

드롭다운은 클래스리스 버전에서는 사용할 수 없어요.

폼 요소와 스타일 일관성을 유지하기 위해, 드롭다운은 기본적으로 셀렉트(select)처럼 스타일링돼요.

출처: 문서

본문

<!-- Dropdown -->
<details class="dropdown">
  <summary>Dropdown</summary>
  <ul>
    <li><a href="#">Solid</a></li>
    <li><a href="#">Liquid</a></li>
    <li><a href="#">Gas</a></li>
    <li><a href="#">Plasma</a></li>
  </ul>
</details>

<!-- Select -->
<select name="select" aria-label="Select" required>
  <option selected disabled value="">Select</option>
  <option>Solid</option>
  <option>Liquid</option>
  <option>Gas</option>
  <option>Plasma</option>false
</select>

체크박스·라디오가 있는 드롭다운 (Dropdowns with checkboxes and radios)

드롭다운은 <input type="radio"> 또는 <input type="checkbox">와 함께 커스텀 셀렉트로 사용할 수 있어요.

<!-- Radios -->
<details class="dropdown">
  <summary>
    Select a phase of matter...
  </summary>
  <ul>
    <li>
      <label>
        <input type="radio" name="phase" value="solid" />
        Solid
      </label>
    </li>
    <li>
      <label>
        <input type="radio" name="phase" value="liquid" />
        Liquid
      </label>
    </li>
    <li>
      <label>
        <input type="radio" name="phase" value="gas" />
        Gas
      </label>
    </li>
    <li>
      <label>
        <input type="radio" name="phase" value="plasma" />
        Plasma
      </label>
    </li>
  </ul>
</details>

<!-- Checkboxes -->
<details class="dropdown">
  <summary>
    Select phases of matter...
  </summary>
  <ul>
    <li>
      <label>
        <input type="checkbox" name="solid" />
        Solid
      </label>
    </li>
    <li>
      <label>
        <input type="checkbox" name="liquid" />
        Liquid
      </label>
    </li>
    <li>
      <label>
        <input type="checkbox" name="gas" />
        Gas
      </label>
    </li>
    <li>
      <label>
        <input type="checkbox" name="plasma" />
        Plasma
      </label>
    </li>
  </ul>
</details>

Pico는 JavaScript 코드를 포함하지 않아요. 이 커스텀 드롭다운과 상호작용하려면 아마 몇 가지 JavaScript를 직접 작성해야 할 거예요.

버튼 변형 (Button variants)

<summary role="button">은 드롭다운을 버튼으로 바꿔줘요.

<details class="dropdown">
  <summary role="button">
    Dropdown as a button
  </summary>
  <ul>
    ...
  </ul>
</details>

일반 버튼처럼 .secondary, .contrast, .outline 스타일을 지원해요 (단, 클래스리스 버전에서는 사용할 수 없어요).

<!-- Primary -->
<details class="dropdown">
  <summary role="button">
    Primary
  </summary>
  <ul>
    ...
  </ul>
</details>

<!-- Secondary -->
<details class="dropdown">
  <summary role="button" class="secondary">
    Secondary
  </summary>
  <ul>
    ...
  </ul>
</details>

<!-- Contrast -->
<details class="dropdown">
  <summary role="button" class="contrast">
    Contrast
  </summary>
  <ul>
    ...
  </ul>
</details>

<!-- Primary outline -->
<details class="dropdown">
  <summary role="button" class="outline">
    Primary outline
  </summary>
  <ul>
    ...
  </ul>
</details>

<!-- Secondary outline -->
<details class="dropdown">
  <summary role="button" class="outline secondary">
    Secondary outline
  </summary>
  <ul>
    ...
  </ul>
</details>

<!-- Contrast outline -->
<details class="dropdown">
  <summary role="button" class="outline contrast">
    Contrast outline
  </summary>
  <ul>
    ...
  </ul>
</details>

검증 상태 (Validation states)

다른 폼 요소와 마찬가지로 aria-invalid로 검증 상태를 제공해요.

<details class="dropdown">
  <summary aria-invalid="false">
    Valid phase of matter: Solid
  </summary>
  <ul>
    ...
  </ul>
</details>

<details class="dropdown">
  <summary aria-invalid="true">
    Debated classification: Plasma
  </summary>
  <ul>
    ...
  </ul>
</details>

드롭다운은 Nav 안에서도 사용할 수 있어요.

서브메뉴의 정렬을 바꾸려면 <ul dir="rtl">만 사용하면 돼요.

<nav>
  <ul>
    <li><strong>Acme Corp</strong></li>
  </ul>
  <ul>
    <li><a href="#" class="secondary">Services</a></li>
    <li>
      <details class="dropdown">
        <summary>
          Account
        </summary>
        <ul dir="rtl">
          <li><a href="#">Profile</a></li>
          <li><a href="#">Settings</a></li>
          <li><a href="#">Security</a></li>
          <li><a href="#">Logout</a></li>
        </ul>
      </details>
    </li>
  </ul>
</nav>

더 알아보기