Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thecodermehedi/de97ce1acf3c04483d4c31f40f9686ac to your computer and use it in GitHub Desktop.
Save thecodermehedi/de97ce1acf3c04483d4c31f40f9686ac to your computer and use it in GitHub Desktop.

A Modern Way to Create a Dialog or Modal Without JavaScript (Pure HTML and CSS Only) in 2025

This example demonstrates how to build a fully functional dialog/modal using only HTML and CSS.

HTML Structure

<!-- HTML Structure for the Dialog -->
<input type="checkbox" id="dialog-toggle" class="hidden">
<label for="dialog-toggle" class="dialog-open">Open</label>
<div class="dialog-backdrop">
  <dialog open>
    <label for="dialog-toggle" class="dialog-close" aria-label="Close">&times;</label>
    <h2>Dialog Title</h2>
    <p>
      This dialog uses a pure CSS backdrop <br> 
      element to provide blur and overlay, alongside the &lt;dialog&gt; element.
    </p>
  </dialog>
</div>

CSS Styles

/* CSS Styles for the Dialog */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f5f5f5;
  font-family: Arial, sans-serif;
}

.hidden {
  display: none;
}

.dialog-open {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 0.375rem;
  cursor: pointer;
  background: #3b82f6;
  color: #fff;
}

.dialog-backdrop {
  position: fixed;
  inset: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(12px);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

#dialog-toggle:checked ~ .dialog-backdrop {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

dialog {
  margin: auto;
  padding: 2rem;
  border: none;
  border-radius: 0.5rem;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  background: #fff;
  opacity: 0;
  transform: scale(0.95);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

#dialog-toggle:checked ~ .dialog-backdrop dialog {
  opacity: 1;
  transform: scale(1);
}

.dialog-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
}

How It Works:

  1. HTML Input Toggle: The checkbox input serves as a state toggle.
  2. CSS Sibling Selector: The ~ sibling combinator applies styles to the .dialog-backdrop and <dialog> based on the input's state.
  3. Accessibility: Semantic <dialog> element and aria-label improve user accessibility.

Run the code live in Codepen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment