🐨CoalaCoding
DocsExamplesTry itBoardB반
🐨CoalaCoding

개발자를 위한 한국어 웹 기술 문서

문서

  • JavaScript
  • Web Publishing
  • React
  • Python

커뮤니티

  • 게시판
  • 예제 모음
  • Try it 에디터

기타

  • GitHub
  • 관리자
© 2026 CoalaCoding. All rights reserved.
  • 01_웹접근성
  • 02_dialog
  • 03_aboutweb
  • 04_개발환경설정
  • 05_문서구조
  • 06_문단과텍스트요소
  • 07_링크요소
  • 08_테이블요소
  • 09_폼요소
  • 10_미디어요소
  1. 홈
  2. 문서
  3. HTML & CSS
  4. HTML
  5. 02_dialog

02_dialog

코드 블록의 Try it Yourself 버튼으로 직접 실행할 수 있다.

구문

🔗https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog

모달팝업을 태그로 구현할수 있다. esc 키 를 누르면 창닫기 기능도 지원 한다.

  1. 기본형 구현
<dialog open>
  <p>스크립트 안써도 정말 된다구?</p>
  <form method="dialog">
    <button>OK</button>
  </form>
</dialog>

실행화면 ok버튼 클릭시 창이 닫힌다.

  1. 오버레이 배경추가
<dialog>
  <button autofocus>Close</button>
  <p>이번엔 배경색 넣기</p>
</dialog>
<button>얍얍</button>
::backdrop {
  background-image: linear-gradient(
    45deg,
    magenta,
    rebeccapurple,
    dodgerblue,
    green
  );
  opacity: 0.75;
}

::backdrop 선택자는 dialog 요소의 배경색을 선택할수 있다. 아주 약간의 자바스크립트를 추가한다

const dialog = document.querySelector("dialog");
const showButton = document.querySelector("dialog + button");
const closeButton = document.querySelector("dialog button");

// "Show the dialog" button opens the dialog modally
showButton.addEventListener("click", () => {
  dialog.showModal();
});

// "Close" button closes the dialog
closeButton.addEventListener("click", () => {
  dialog.close();
});

showModal(), close() 는 dialog api 가 제공하는 메서드이다. 실행화면

목차

  • 구문