The Idea
Use native HTML <dialog> element for image lightbox instead of JavaScript libraries.
Context
Building a gallery lightbox for a wedding invitation. The plan said “NO complex JavaScript lightbox libraries” to keep dependencies minimal.
Raw Exchange
Plan: “Implement CSS-only lightbox using native
<dialog>” Implementation:
<dialog id="lightbox" class="lightbox">
<button class="lightbox-close">×</button>
<img id="lightbox-image" />
</dialog>
lightbox.showModal(); // opens with backdrop
lightbox.close(); // closes
Expansion Potential
Article: “Ditch Your Lightbox Library: Native Dialog Does It Better”
Key points:
showModal()vsshow()- modal blocks interaction, non-modal doesn’t::backdroppseudo-element for overlay styling- Escape key closes automatically
- Click outside closes with simple event listener
- Focus trap is built-in
- Works in all modern browsers (95%+ support)
Code comparison:
- Lightbox2: 8KB + jQuery dependency
- GLightbox: 12KB
- Native dialog: 0KB, 10 lines of JS
Technical Notes
dialog::backdrop {
background: rgba(0, 0, 0, 0.95);
}
dialog {
border: none;
max-width: 100vw;
max-height: 100vh;
}