Minimal Overlay Modal In Pure CSS

Category: CSS & CSS3 , Modal & Popup | July 19, 2016
Author:johnuberbacher
Views Total:11,891 views
Official Page:Go to website
Last Update:July 19, 2016
License:MIT

Preview:

Minimal Overlay Modal In Pure CSS

Description:

Just another Pure CSS overlay modal solution without any JS and checkbox & label tricks. Click on the blank area of background overlay to close the modal.

How to use it:

Add the heading, body content and close button to the modal like this:

<div id="modal1" class="overlay">
  <a class="cancel" href="#"></a>
  <div class="modal">
    <h2>This is Modal Overlay 1</h2>
    <div class="content">
    <p>Click outside the modal to close.</p>
    </div>
  </div>
</div>

Create a modal toggle link pointing to the modal ID you just created.

<a href="#modal1" class="button">Open Modal</a>

Style the modal in the CSS.

.modal {
  margin: 100px auto;
  padding: 20px;
  background: #fff;
  border: 1px solid #666;
  width: 300px;
  border-radius: 6px;
  box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
  position: relative;
}
.modal h2 { margin-top: 0; }
.modal .close {
  position: absolute;
  width: 20px;
  height: 20px;
  top: 20px;
  right: 20px;
  opacity: 0.8;
  transition: all 200ms;
  font-size: 24px;
  font-weight: bold;
  text-decoration: none;
  color: #777;
}
.modal .close:hover { opacity: 1; }
.modal .content {
  max-height: 400px;
  overflow: auto;
}
.modal p {
  margin: 0 0 1em;
  text-align: center;
}
.modal p:last-child { margin: 0; }

Style the fullscreen background overlay.

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.5);
  transition: opacity 200ms;
  visibility: hidden;
  opacity: 0;
}
.overlay .cancel {
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: default;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

You Might Be Interested In:


2 thoughts on “Minimal Overlay Modal In Pure CSS

  1. Stian Dahlberg

    I wasn’t able to get the info-box to display until I added display: block for the .modal style. When I checked the calculated styleset in DevTool it had a display: none style set for .modal, but not sure if it’s an inheritance issue in my setup, or a problem with this code itself.

    Reply
  2. Stian Dahlberg

    Actually, I was able to confirm that my initial issue was caused by crashing css-styles in an externally linked css file. Thanks for a nice and simple solution!

    Reply

Leave a Reply