Simple Modal Box Using CSS and :target Pseudo Class

Category: CSS & CSS3 , Modal & Popup | April 11, 2015
Author:jakealbaugh
Views Total:4,471 views
Official Page:Go to website
Last Update:April 11, 2015
License:MIT

Preview:

Simple Modal Box Using CSS and :target Pseudo Class

Description:

A pure CSS solution to show a fullscreen modal window using :target pseudo class.

This modal is open because its id is the target in the href of the link. You can style an element’s target state with the selector :target.

When this modal is toggled, an invisible link with href=”#” is positioned absolutely behind the modal content. Clicking it will change the target and thus close the modal.

How to use it:

Create a toggle link to launch the modal window.

<a href="#target-content" id="button">Open A Modal</a>

Add the content to the modal.

<div id="target-content">
  <a href="#" class="close"></a>
  <div id="target-inner">
    <h2>Modal Heading</h2>
    <p>Modal Content</p>
  </div>
</div>

The Core CSS styles.

#target-content {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  pointer-events: none;
  opacity: 0;
  -webkit-transition: opacity 200ms;
  transition: opacity 200ms;
}
#target-content:target {
  pointer-events: all;
  opacity: 1;
}
#target-content #target-inner {
  position: absolute;
  display: block;
  padding: 48px;
  line-height: 1.8;
  width: 70%;
  top: 50%;
  left: 50%;
  -webkit-transform: translateX(-50%) translateY(-50%);
  -ms-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
  box-shadow: 0px 12px 24px rgba(0, 0, 0, 0.2);
  background: white;
  color: #34495E;
}
#target-content #target-inner h2 { margin-top: 0; }
#target-content #target-inner code { font-weight: bold; }
#target-content a.close {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: #34495E;
  opacity: 0.5;
  -webkit-transition: opacity 200ms;
  transition: opacity 200ms;
}
#target-content a.close:hover { opacity: 0.4; }

You Might Be Interested In:


3 thoughts on “Simple Modal Box Using CSS and :target Pseudo Class

Leave a Reply