Basic Modal Window Using CSS Pseudo Element

Category: CSS & CSS3 , Modal & Popup | August 6, 2018
Author: Timothy Long
Views Total: 448 views
Official Page: Go to website
Last Update: August 6, 2018
License: MIT

Preview:

Basic Modal Window Using CSS Pseudo Element

Description:

A CSS only modal window based on CSS :target pseudo-class without the need of JavaScript.

How to use it:

Create the HTML for the modal window.

<div id="open-modal" class="modal-window">
  <div>
    <a href="#modal-close" title="Close" class="modal-close">Close</a>
    <h1>Modal Title</h1>
    <div>Modal Content</div>
  </div>
</div>

Create a trigger link pointing to the modal window.

<a href="#open-modal">Launch</a>

Style the modal window in the CSS.

.modal-window {
  position: fixed;
  background-color: rgba(255, 255, 255, 0.25);
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 999;
  opacity: 0;
  pointer-events: none;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
}

.modal-window > div {
  width: 400px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 2em;
  background: #ffffff;
  color: #333333;
}

.modal-close {
  color: #aaa;
  line-height: 50px;
  font-size: 80%;
  position: absolute;
  right: 0;
  text-align: center;
  top: 0;
  width: 70px;
  text-decoration: none;
}

.modal-close:hover {
  color: #000;
}

Enable the trigger link to toggle the modal window.

.modal-window:target {
  opacity: 1;
  pointer-events: auto;
}

You Might Be Interested In:


Leave a Reply