Styling Dialog

Preview

Native dialog element with custom backdrop styling and entrance animation.

Dialog Structure

Native dialog element with modal behavior and inline close handler.

  <dialog>
    <h1>Styling Dialog</h1>
    <p>This dialog is styled with CSS.</p>
    <button onclick="this.closest('dialog').close()">Close</button>
  </dialog>

Backdrop Customization

Use CSS custom properties for dialog-specific backdrop styling via inheritance.

  :root {
    /* Any ::backdrop can access this custom property */
    --backdrop-color: #333;
  }
  dialog {
    /* The ::backdrop for dialog can access this custom property */
    --backdrop-color: rgba(0, 0, 0, 0.5);
  }
  ::backdrop {
    background-color: var(--backdrop-color);
  }

Entrance Animation

Apply transition with @starting-style for smooth entrance effect on :open state.

  dialog:open {
    display: grid;
    justify-items: center;
    gap: 1rem;
    padding: 1rem;
    inset: 0;
    margin: auto;
    border-radius: 1rem;
    opacity: 1;
    translate: 0;
    transition: opacity 0.3s ease, translate 0.3s ease;

    @starting-style {
      opacity: 0;
      translate: 0 10%;
    }
  }

Simple event handlers for showModal() and close() methods.

    const trigger = document.getElementById('trigger');
    const dialog = document.querySelector('dialog');
    const close = dialog.querySelector('button');
    trigger.addEventListener('click', () => {
      dialog.showModal()
    });
    close.addEventListener('click', () => {
      dialog.close();
    });