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

모달

원문 보기 위키 갱신

모달 (Modal)

Modal은 버튼을 클릭하면 대화 상자(dialog)나 박스를 보여주는 컴포넌트예요.

출처: 문서

본문

Modal은 버튼 클릭 시 화면 위에 겹쳐 보이는 다이얼로그 창을 만들 때 사용합니다. 내용, 액션, 배경(backdrop) 등의 파트로 구성되며, 여는 방법에 따라 여러 가지 방식이 있습니다.

클래스명 타입 설명
modal Component 모달
modal-box Part 내용 파트
modal-action Part 액션(버튼 등) 파트
modal-backdrop Part 모달이 열렸을 때 페이지를 덮는 라벨. 바깥 클릭으로 닫을 수 있게 함
modal-toggle Part 모달 상태를 제어하는 숨은 체크박스
modal-open Modifier 모달을 열린 상태로 유지 (JS로 클래스 추가 가능)
modal-top Placement 모달을 위로 이동
modal-middle Placement 모달을 중앙으로 이동 [기본값]
modal-bottom Placement 모달을 아래로 이동
modal-start Placement 모달을 가로 시작점으로 이동
modal-end Placement 모달을 가로 끝으로 이동

모달을 사용하는 4가지 방법

방법 열기/닫기 방식 Esc 키로 닫기 배경 상호작용 잠금
1. Dialog (권장) JavaScript ✅ 가능 ✅ 잠김
2. Popover HTML 속성 ✅ 가능 ❌ 잠기지 않음
3. Checkbox (레거시) 숨은 체크박스 ❌ 불가 ❌ 잠기지 않음
4. Anchor Link (레거시) URL 파라미터 ❌ 불가 ❌ 잠기지 않음

모달을 열면 페이지에 스크롤바 여백(scrollbar-gutter)이 추가되어, 고정 스크롤바를 쓰는 운영체제에서 레이아웃이 흔들리지 않게 됩니다. 최신 Chromium 기반 브라우저에서는 세로 스크롤바의 존재를 자동으로 감지해요. Safari와 모바일 기기에서는 스크롤바가 오버레이로 표시되므로 여백이 생기지 않고, Firefox에서는 세로 스크롤바 존재를 직접 감지해서 :root에 scrollbar-gutter: stable 또는 scrollbar-gutter: unset을 직접 설정해야 합니다. 이 기능을 쓰고 싶지 않다면 rootscrollgutter를 제외하면 됩니다.

방법 1: HTML dialog 요소 (권장)

HTML dialog 요소는 모달을 만드는 네이티브 방식이에요. 접근성이 좋고 Esc 키로 닫을 수 있습니다. JS의 ID.showModal() 메서드로 열고, ID.close() 메서드로 닫아요. 각 모달의 ID는 유일해야 합니다.

Dialog 모달

클릭 시 ID.showModal() 메서드로 열리고 ID.close() 메서드로 닫을 수 있어요.

<!-- Open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_1.showModal()">open modal</button>
<dialog id="my_modal_1" class="modal">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
    <div class="modal-action">
      <form method="dialog">
        <!-- if there is a button in form, it will close the modal -->
        <button class="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>

React에서는 document.getElementById('ID').showModal() 방식으로 동일하게 열 수 있어요.

{/* Open the modal using document.getElementById('ID').showModal() method */}
<button className="btn" onClick={()=>document.getElementById('my_modal_1').showModal()}>open modal</button>
<dialog id="my_modal_1" className="modal">
  <div className="modal-box">
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Press ESC key or click the button below to close</p>
    <div className="modal-action">
      <form method="dialog">
        {/* if there is a button in form, it will close the modal */}
        <button className="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>

바깥 클릭 시 닫히는 Dialog 모달

modal-backdrop 클래스가 있는 두 번째 form이 화면을 덮어서, 바깥을 클릭하면 모달을 닫을 수 있어요.

<!-- Open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_2.showModal()">open modal</button>
<dialog id="my_modal_2" class="modal">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click outside to close</p>
  </div>
  <form method="dialog" class="modal-backdrop">
    <button>close</button>
  </form>
</dialog>
{/* Open the modal using document.getElementById('ID').showModal() method */}
<button className="btn" onClick={()=>document.getElementById('my_modal_2').showModal()}>open modal</button>
<dialog id="my_modal_2" className="modal">
  <div className="modal-box">
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Press ESC key or click outside to close</p>
  </div>
  <form method="dialog" className="modal-backdrop">
    <button>close</button>
  </form>
</dialog>

모서리에 닫기 버튼이 있는 Dialog 모달

<!-- You can open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_3.showModal()">open modal</button>
<dialog id="my_modal_3" class="modal">
  <div class="modal-box">
    <form method="dialog">
      <button class="btn btn-sm btn-circle btn-ghost absolute right-2 top-2">✕</button>
    </form>
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click on ✕ button to close</p>
  </div>
</dialog>
{/* You can open the modal using document.getElementById('ID').showModal() method */}
<button className="btn" onClick={()=>document.getElementById('my_modal_3').showModal()}>open modal</button>
<dialog id="my_modal_3" className="modal">
  <div className="modal-box">
    <form method="dialog">
      {/* if there is a button in form, it will close the modal */}
      <button className="btn btn-sm btn-circle btn-ghost absolute right-2 top-2">✕</button>
    </form>
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Press ESC key or click on ✕ button to close</p>
  </div>
</dialog>

커스텀 너비 Dialog 모달

modal-box에 w-나 max-w- 유틸리티 클래스를 붙여서 너비를 자유롭게 조절할 수 있어요.

<!-- You can open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_4.showModal()">open modal</button>
<dialog id="my_modal_4" class="modal">
  <div class="modal-box w-11/12 max-w-5xl">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Click the button below to close</p>
    <div class="modal-action">
      <form method="dialog">
        <!-- if there is a button, it will close the modal -->
        <button class="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>
{/* You can open the modal using document.getElementById('ID').showModal() method */}
<button className="btn" onClick={()=>document.getElementById('my_modal_4').showModal()}>open modal</button>
<dialog id="my_modal_4" className="modal">
  <div className="modal-box w-11/12 max-w-5xl">
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Click the button below to close</p>
    <div className="modal-action">
      <form method="dialog">
        {/* if there is a button, it will close the modal */}
        <button className="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>

반응형 (Responsive)

모달은 SM 화면 크기에서는 아래(bottom)로, MD 화면 크기에서는 중앙(middle)으로 갑니다. modal-bottom sm:modal-middle처럼 배치 클래스를 조합하면 돼요.

<!-- Open the modal using ID.showModal() method -->
<button class="btn" onclick="my_modal_5.showModal()">open modal</button>
<dialog id="my_modal_5" class="modal modal-bottom sm:modal-middle">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
    <div class="modal-action">
      <form method="dialog">
        <!-- if there is a button in form, it will close the modal -->
        <button class="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>
{/* Open the modal using document.getElementById('ID').showModal() method */}
<button className="btn" onClick={()=>document.getElementById('my_modal_5').showModal()}>open modal</button>
<dialog id="my_modal_5" className="modal modal-bottom sm:modal-middle">
  <div className="modal-box">
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Press ESC key or click the button below to close</p>
    <div className="modal-action">
      <form method="dialog">
        {/* if there is a button in form, it will close the modal */}
        <button className="btn">Close</button>
      </form>
    </div>
  </div>
</dialog>

방법 2: HTML popover

Popover는 dialog와 비슷하지만 배경 요소에도 계속 포커스를 둘 수 있다는 차이가 있어요. 접근성이 좋고 Esc 키로 닫을 수 있습니다.

Popover 모달

버튼의 popovertarget 속성이 모달의 ID를 가리켜요. 각 모달의 ID는 유일해야 합니다.

<!-- The button's popovertarget attribute points to the modal's ID. ID must be unique for each modal -->
<button class="btn" popovertarget="my-modal-1">Open</button>

<div class="modal" id="my-modal-1" popover>
  <div class="modal-box">
    <h3 class="font-bold text-lg">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
    <div class="modal-action">
      <button popovertarget="my-modal-1" popovertargetaction="hide">close</button>
    </div>
  </div>
</div>

바깥 클릭 시 닫히는 Popover 모달

<!-- The button's popovertarget attribute points to the modal's ID. ID must be unique for each modal -->
<button class="btn" popovertarget="my-modal-2">Open</button>

<div class="modal" id="my-modal-2" popover>
  <div class="modal-box">
    <h3 class="font-bold text-lg">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
  </div>
  <div class="modal-backdrop">
    <button popovertarget="my-modal-2" popovertargetaction="hide">close</button>
  </div>
</div>

방법 3: checkbox (레거시)

숨은 체크박스가 모달의 상태를 제어하고, 라벨이 그 체크박스를 토글해서 모달을 열고 닫게 하는 방식이에요.

checkbox를 사용하는 모달 (Modal using checkbox)

<!-- The button to open modal -->
<label for="my_modal_6" class="btn">open modal</label>

<!-- Put this part before </body> tag -->
<input type="checkbox" id="my_modal_6" class="modal-toggle" />
<div class="modal" role="dialog">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">This modal works with a hidden checkbox!</p>
    <div class="modal-action">
      <label for="my_modal_6" class="btn">Close!</label>
    </div>
  </div>
</div>

바깥 클릭 시 닫히는 모달 (Modal that closes when clicked outside)

<!-- The button to open modal -->
<label for="my_modal_7" class="btn">open modal</label>

<!-- Put this part before </body> tag -->
<input type="checkbox" id="my_modal_7" class="modal-toggle" />
<div class="modal" role="dialog">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">This modal works with a hidden checkbox!</p>
  </div>
  <label class="modal-backdrop" for="my_modal_7">Close</label>
</div>

방법 4: 앵커 링크 (레거시)

링크가 URL에 파라미터를 추가해서, URL에 그 파라미터가 있을 때만 모달이 보이는 방식이에요. 모달이 닫히면 앵커 링크 때문에 페이지가 맨 위로 스크롤됩니다. 일부 SPA 프레임워크에서는 앵커 링크가 잘 동작하지 않을 수 있으니, 문제가 있다면 다른 방법을 사용하세요.

<!-- The button to open modal -->
<a href="#my_modal_8" class="btn">open modal</a>

<!-- Put this part before </body> tag -->
<div class="modal" role="dialog" id="my_modal_8">
  <div class="modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">This modal works with anchor links</p>
    <div class="modal-action">
      <a href="#" class="btn">Yay!</a>
    </div>
  </div>
</div>

더 알아보기 (Learn more)

모달은 새 프로젝트에서 만들 때 HTML dialog 요소(방법 1)를 권장해요. 접근성과 Esc 키 닫기가 기본 지원되니까요. 바깥 클릭으로 닫고 싶으면 modal-backdrop을, 위치는 modal-top/modal-bottom 등으로, 너비는 w-/max-w- 클래스로 조절할 수 있습니다. 배경 스크롤 잠금이 필요한 경우엔 dialog 방식이, 배경과 상호작용이 필요한 경우엔 popover 방식이 적합해요.