Building a Bootstrap Modal with the dialog HTML element
Posted
on
Contents
Continuing my series of posts on building Bootstrap components using just CSS and native HTML elements, this post demonstrates how to create a Bootstrap modal dialog using the <dialog> HTML element.
The <dialog> element has been supported by all major browsers for a while now and can be used with Invoker Commands and is a great way to create a modal dialog without the need of JavaScript.
You can create a Bootstrap modal using the <dialog> element with just a tiny bit of custom CSS.
Invoker commands are now supported in all modern browsers. In order to support older browsers, you can add the invokers polyfill.
The above example allows the user to dismiss the modal by clicking outside of it or pressing the Escape key. If you want to prevent this behavior, you can remove the closedby="any" attribute from the <dialog> element.
The dialog element fades in when opened using the ::backdrop pseudo-element and Bootstrap’s own fade-in animation, but when dismissing the dialog it doesn’t fade out.
Bootstrap 6 will likely rename the modal component to dialog and use the <dialog> element and there’s an open PR on GitHub.