Skip to content

Modal

Dialogs for lightboxes, user notifications and custom content.

  • Each modal needs a unique id.
  • Open a modal with a data-modal link (its rel must equal the modal’s id) or from JavaScript.
  • While a modal is open, the class js-modal-opened is set on <html>.
  • Nested modals aren’t supported. Avoid placing modals inside position: fixed elements, and put the markup at the top level of the page when you can.
  • Default settings – using all defaults.
  • Set width – max width of 700px.
  • – modals.show('demo-modal-default', { width: 420 })
HTML
<ul>
<li>
<a href="#" rel="demo-modal-default" data-modal="">Default settings</a>
<span> &ndash; using all defaults.</span>
</li>
<li>
<a href="#" rel="demo-modal-wide" data-modal="width:700">Set width</a>
<span> &ndash; max width of 700px.</span>
</li>
<li>
<button type="button" id="demo-modal-js">Open from JavaScript</button>
<span> &ndash; <code>modals.show('demo-modal-default', { width: 420 })</code></span>
</li>
</ul>
<div class="modal" id="demo-modal-default">
<div class="modal-header">
<h2 class="modal-header-title">Modal header</h2>
<a href="#" class="modal-close">&times;</a>
</div>
<div class="modal-content">
<h3>Hello</h3>
<p>Click the close link, or the dark area behind the modal, to close it.</p>
</div>
</div>
<div class="modal" id="demo-modal-wide">
<div class="modal-header">
<h2 class="modal-header-title">Hello! I'm a header</h2>
<a href="#" class="modal-close">CLOSE &times;</a>
</div>
<div class="modal-content">
<img style="max-width:200px;" src="/img/carousel/eagle.jpg" alt="An eagle">
</div>
</div>
JavaScript
// Modals must not sit inside an element that creates its own stacking context:
// the overlay is added to <body>, so it would paint on top of them. This site's
// page content is such an element (Starlight sets `isolation: isolate` on it),
// so move the modals to <body> first. In your own page, just put the markup there.
var modalEls = document.querySelectorAll('.modal');
for (var i = 0; i < modalEls.length; i++) {
document.body.appendChild(modalEls[i]);
}
var modals = new ML.Modal();
modals.init();
// Open from JavaScript instead of (or as well as) data-modal.
document.getElementById('demo-modal-js').addEventListener('click', function () {
modals.show('demo-modal-default', { width: 420 });
});

Fired on document. event.detail.modal is the modal element.

Event Fired when
modal.opened a modal is opened
modal.closed a modal is closed
modal.destroyed destroy() runs (also on every init())
document.addEventListener('modal.opened', function (event) {
console.log('opened modal', event.detail.modal);
});
  • Put the modal markup directly in <body>, or inside ancestors that don’t create their own stacking context. The overlay is added to <body>, so a modal inside an element with isolation: isolate, a transform, opacity below 1 or a z-index is drawn underneath the overlay. This site wraps its content in such an element, which is why the demo moves its modals to <body> first.
  • The overlay and modal use z-index 1 and 2. A site header with a higher z-index (this site’s is 10) is drawn above the overlay and isn’t dimmed.
  • Resizing the window can throw the centering of the modal off.
  • Clicking the overlay (the dark area behind the modal), a .modal-close element, or anything inside one, closes the modal. The link that opens a modal can have other elements inside it, such as an icon: a click on them opens it.

See the generated Modal class reference for every method and option.

A checklist. An unticked item is not built, and not promised: it says what is missing today. A ticked item has been built.

  • Keyboard and screen readers. Esc does not close a modal, the focus is not moved into it or kept there, and the markup has no role="dialog", aria-modal or label.
  • Centering on resize. A modal is centered when it opens, so resizing the window can throw it off.
  • A modal the overlay cannot close. A static setting, for a modal that has to be answered.
  • Stacking. Moving the modal to <body> when it opens would stop an ancestor’s stacking context from drawing it under the overlay.
  • z-index variables. The overlay is 1 and the modal is 2. A setting would let a page change them.
  • Cancelable events. A modal.beforeopen event that a listener can cancel.
  • More than one modal at a time. Showing a modal hides the one that is open.