Прескочи на главни садржај
← 📄 HTML & the platform·Модул A2 · Лекција 10
ЗадатакBuild a dialog with id 'hello'. Inside: <h2>Hi</h2>, <button>Close</button>. After the dialog, add a button that opens it. The JS should query #hello and call .showModal() once at load so the test sees it open.

<дијалог>: модали без оквира

100 XP8 min
Теорија

Native modals shipped in 2022

<dialog id="cookies">
  <h2>Cookies</h2>
  <p>We use cookies to remember your login.</p>
  <button onclick="this.closest('dialog').close()">OK</button>
</dialog>

<button onclick="document.querySelector('#cookies').showModal()">
  Show cookies dialog
</button>

showModal() opens it as a true modal — backdrop dims the page, focus traps inside the dialog, ESC closes it, content behind is inert. All free, all native.

Two modes

  • dialog.showModal() — true modal (overlay + backdrop + focus trap + ESC closes)
  • dialog.show() — non-modal (floats on top but page is still interactive)

Style the backdrop

dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

A pseudo-element only available on dialogs in modal mode. Use it.

Why not just a div?

Before <dialog>, a "modal" meant: a fixed div + JS to manage focus + JS to handle ESC + a separate div for the backdrop + ARIA roles + tab-trap polyfills. That's 100+ lines of JS for what <dialog> does in 2.

🔒

Региструј се да почнеш са кодом

Теорија је отворена за све. Едитор, преглед уживо и провера се откључавају са 7-дневним бесплатним пробним периодом — потребна картица.

Регистрација — бесплатан пробни период →

Првих 10 лекција у сваком путу су бесплатне. Без картице.

← ПретходнаСледећа лекција →

Један савет за Python или веб дневно — на имејл

Кратко, ручно писано, без спама. Одјава у једном клику.