Create CSS Only Modal Window Using Details Element

Category: CSS & CSS3 , Modal & Popup , Recommended | January 23, 2021
Author: Niels Voogt
Views Total: 236 views
Official Page: Go to website
Last Update: January 23, 2021
License: MIT

Preview:

Create CSS Only Modal Window Using Details Element

Description:

A CSS only modal window that uses HTML <details> element to show & hide modal content.

How to use it:

1. Add the modal trigger button & background overlay to the <summary> element.

<summary>
  <div class="button">
    Launch The Modal
  </div>
  <div class="details-modal-overlay"></div>
</summary>

2. Then insert them together with your own modal content into the <details> element.

<details>
  <summary>
    <div class="button">
      Launch The Modal
    </div>
  <div class="details-modal-overlay"></div>
  </summary>
  <div class="details-modal">
    <!-- Close Button -->
    <div class="details-modal-close">
      <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 14 14" fill="none">
        <path fill-rule="evenodd" clip-rule="evenodd" d="M13.7071 1.70711C14.0976 1.31658 14.0976 0.683417 13.7071 0.292893C13.3166 -0.0976311 12.6834 -0.0976311 12.2929 0.292893L7 5.58579L1.70711 0.292893C1.31658 -0.0976311 0.683417 -0.0976311 0.292893 0.292893C-0.0976311 0.683417 -0.0976311 1.31658 0.292893 1.70711L5.58579 7L0.292893 12.2929C-0.0976311 12.6834 -0.0976311 13.3166 0.292893 13.7071C0.683417 14.0976 1.31658 14.0976 1.70711 13.7071L7 8.41421L12.2929 13.7071C12.6834 14.0976 13.3166 14.0976 13.7071 13.7071C14.0976 13.3166 14.0976 12.6834 13.7071 12.2929L8.41421 7L13.7071 1.70711Z" fill="black" />
      </svg>
    </div>
    <!-- Modal Title -->
    <div class="details-modal-title">
      <h1>Modal Title Here</h1>
    </div>
    <!-- Modal Content -->
    <div class="details-modal-content">
      <p>
        Modal Content Here
      </p>
    </div>
  </div>
</details>

3. The required CSS styles for the modal window.

/* trigger button */
.button {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background: #16a34a;
  border-radius: 0.25em;
  color: white;
  cursor: pointer;
  display: inline-block;
  font-weight: 500;
  height: 3em;
  line-height: 3em;
  padding: 0 1em;
}

.button:hover {
  background-color: #17ac4e;
}

/* modal body */
.details-modal {
  background: #ffffff;
  border-radius: 0.5em;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  left: 50%;
  max-width: 90%;
  pointer-events: none;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 30em;
  text-align: left;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

/* close button */
.details-modal .details-modal-close {
  align-items: center;
  color: #111827;
  display: flex;
  height: 4.5em;
  justify-content: center;
  pointer-events: none;
  position: absolute;
  right: 0;
  top: 0;
  width: 4.5em;
}
.details-modal .details-modal-close svg {
  display: block;
}

/* modal title */
.details-modal .details-modal-title {
  color: #111827;
  padding: 1.5em 2em;
  pointer-events: all;
  position: relative;
  width: calc(100% - 4.5em);
}
.details-modal .details-modal-title h1 {
  font-size: 1.25rem;
  font-weight: 600;
  line-height: normal;
}

/* modal content */
.details-modal .details-modal-content {
  border-top: 1px solid #e0e0e0;
  padding: 2em;
  pointer-events: all;
  overflow: auto;
}

/* modal overlay */
.details-modal-overlay {
  transition: opacity 0.2s ease-out;
  pointer-events: none;
  background: rgba(15, 23, 42, 0.8);
  position: fixed;
  opacity: 0;
  bottom: 0;
  right: 0;
  left: 0;
  top: 0;
}

details[open] .details-modal-overlay {
  pointer-events: all;
  opacity: 0.5;
}

details summary:focus {
  outline: none;
}

details summary::-webkit-details-marker {
  display: none;
}

You Might Be Interested In:


One thought on “Create CSS Only Modal Window Using Details Element

Leave a Reply