Skip to main content

An accessible pop-up modal window using purely a mix of CSS and HTML and zero JavaScript

-

Accessible CSS3 modal pop-up windows

Pure XHTML and CSS3 with zero JavaScript.

Requires a modern browser: Firefox, Chrome, Safari, Opera, or IEv9+.

I use this code for a pop-up on Tesco Store Locator (though activation is different) which needed better support for older browsers.

A complete commented bare bones CSS3 modal pop-up demo version is available.

The XHTML

The activating links need to be associated by the href fragment identifier.


<ul id="links">
  <li><a href="#pop1">Pop-up One</a></li>
  <li><a href="#pop2">Pop-up Two</a></li>
  <li><a href="#pop3">Pop-up Three</a></li>
</ul>

Every fragment identifier must have an associated block id.


<div id="pop2" class="pop-up">
  <div class="popBox">
    <div class="popScroll">
      <h2>Pop-up Two</h2>
      <p>Blah, blah, blah. Yada, yada, yaa.</p>
    </div>
    <a href="#links" class="close"><span>Close</span></span></a>
  </div>
  <a href="#links" class="lightbox">Back to links</a>
</div>

The CSS

The outside container covers the entire display once activated via :target.


.pop-up {
  position: absolute;
  top: 0;
  left: -500em;
}
.pop-up:target {
  position: static;
  left: 0;
}

The first child container sets the pop-ups: look, size, position, and activation effect.


.popBox {
  background-color: #fff;

  /* alternatively fixed width / height and negative margins from 50% */
  position: absolute;
  left: 30%;
  right: 30%;
  top: 30%;
  bottom: 30%;
  z-index: 1;
  border: 1px solid #3a3a3a;
  border-radius: 0.25rem;
  box-shadow: 0 0.5rem 0.5rem rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.5s ease-out;
}
:target .popBox {
  position: fixed;
  opacity: 1;
}
.popBox:hover {
  box-shadow: 0 0 0.5rem 0.5rem rba(255, 0, 0, 0.5);
}

The lightbox is a link itself. On click it returns focus to the activating link set.


.lightbox {
  display: none;
  text-indent: -200em;
  background-color: rgba(0,0,0,.4);
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 5;
}
:target .lightbox {
  display: block;
}

The close pop-up graphic is actually just more CSS.


.close:link,
.close:visited {
  position: absolute;
  top: -0.75em;
  right: -0.75em;
  display: block;
  width: 2em;
  height: 2em;
  line-height: 1.8;
  padding: 0;
  text-align: center;
  text-decoration: none;
  background-color: #000;
  border: 2px solid #fff;
  color: #fff;
  border-radius: 50%;
  box-shadow: 0 0 .5rem .5rem rba(0,0,0,.5);
}
.close:before {content:"X";}
.close:hover,
.close:active,
.close:focus {
  box-shadow: 0 0 .5rem .5rem rba(255,0,0,.5);
  background-color: #c00;
  color: #fff;
}
.close span {
  text-indent: -200em;
  display: block;
}

The pop-up content may be bigger than the space defined, therefore allow it to scroll.


.popScroll {
  position: absolute;
  top: 1rem;
  left: 1rem;
  right: 1rem;
  bottom: 1rem;
  overflow-y: auto;
  *overflow-y: scroll;
  padding-right: 0.5em;
}

Socialise: