Responsive modal dialogs

Screenshot of modal dialog masking content
Modal window with full-viewport mask

A super simple responsive modal window with minimal JavaScript.

  • Mobile friendly (mobile first, natch!), fills the viewport on mobiles
  • Keyboard navigable
  • Marked up for accessibility
  • Masks the content on desktop

Particular attention here on the accessible aspects of modal markup: aria-labeledby, aria-describedby and the toggling of aria-hidden on show/hide.

Basic modal markup with these aria properties:

<div class="modal"
 data-modal="dialog1"
 role="dialog" 
 aria-labelledby="modal-label"
 aria-describedby="modal-description"
 aria-hidden="false">

Then matching elements inside the modal itself:

<h2 id="modal-label">
 My modal title
</h2>
...
<p id="modal-description">Modal description</p>
Screenshot of modal dialog filling viewport
Fills viewport on smaller devices

A demo can be found on CodePen.

Leave a Reply

Your email address will not be published. Required fields are marked *