Is there a way to make a modal close with just html and css

The modal will open up and can be closed via a exit button,but also by clicking outside of the modal window.;is there any way to do this with just pure html and css,no javascript?
15 Replies
13eck
13eck4mo ago
Yes. Use the dialog element (to get the "click outside to close" functionality) and add the popover attribute to get the open/close functionality.
13eck
13eck4mo ago
MDN Web Docs
Using the Popover API - Web APIs | MDN
The Popover API provides developers with a standard, consistent, flexible mechanism for displaying popover content on top of other page content. Popover content can be controlled either declaratively using HTML attributes, or via JavaScript. This article provides a detailed guide to using all of its features.
13eck
13eck4mo ago
You add the popover-target="<id of popover element>" to a button and it'll toggle the popover. Or add the popovertargetaction="hide" to be only a close button.
ἔρως
ἔρως4mo ago
actually you can do much worse, but works much better add an hidden form inside the dialog give it an id - ideally <dialog id>-close then, add a button of type submit, with the form attribute set to that form oh, and the form needs to have method="dialog" when you submit a form with that method, inside a dialog, it closes the dialog the click outside can work if you go 3 levels deeper into the jank, which i do not recommend
13eck
13eck4mo ago
Is it better or worse? 🤔
ἔρως
ἔρως4mo ago
much much worse than your method, but more compatible by the way, with my method the opening needs javascript but you will need to use javascript anyways, until the popover support is better
13eck
13eck4mo ago
Yep. The popover attribute is new: 2024
ἔρως
ἔρως4mo ago
yup in short, the answer is "kinda almost" a little tangent: why close the dialog when clicking outside the area? thats the fastest way to get angry emails from users who lost the form data (that and throwing away everybody's settings for your app)
Chocolate U. G
Chocolate U. GOP4mo ago
Its for a blog where the layout will have a modal come out and take up the entire right side to show more contextual info
ἔρως
ἔρως4mo ago
ah that makes sense to have the dialog close when you click outside
curiousmissfox
curiousmissfox4mo ago
Popover allows light dismiss (clicking outside) and doesn’t require js ; as 13 mentioned you can map open and close buttons via popovertargetaction. But depends how good you need support to be; as of the time of writing , popover / popovertarget / popovertargetaction attrs are Baseline Support caniuse
Chocolate U. G
Chocolate U. GOP4mo ago
Thats good enough i think,im using it for tauri and theyre using native webview.the primary platforms im aiming for are windows and linux
curiousmissfox
curiousmissfox4mo ago
These tables are for browser support fyi (chrome safari opera Firefox etc) and windows / Linux are operating systems/oS
b1mind
b1mind4mo ago
Tauri
Webview Versions
The cross-platform app building toolkit
b1mind
b1mind4mo ago
So long as they are targeting the right webkit for macOS. Then what ever Edge supports.

Did you find this page helpful?