What CSS methods are available to display a modal? What are the pros and cons?
Requirement
My page will contain multiple identical siblings, each containing an image and some text.
On click, I want a lightbox to display the same elements that are on the page, including the image, as well as some elements which are hidden when the lightbox isn’t open.
Want to avoid repeating content in the HTML file.
Should work nice with a mobile device or desktop.
No bootstrap or other external libs.
Lightboxes
It seems that various CSS image lightboxes may be better solutions than any of the modal methods listed in this post. Lightboxes might not repeat image urls twice, and often support captions that are visible only when the image is expanded. i don’t need carousel or slideshow. Maybe i shouldn’t call this post “modal” at all?
So, haven’t found one that doesn’t repeat the image.
Target method
This method puts the modal content into a targeted div which is hidden when not active, using the :not(:target)
pseudoclasses. It uses display: flex
to display the modal, tho’ i dont quite understand how the flex helps. I don’t understand the relationship between the "modal"
div and the "modal__window"
div.
Here’s another target example. Not sure if it’s different in any essential way.
Checkbox method
This method uses the hidden checkbox hack. It uses the :checked
state of the checkbox to show the modal. I’m curious about doing it with radio buttons, since we only want to allow one modal at a time. This method uses fig
and figcaption
, altho it seems fig, figcaption
might be intended where the caption contains different information than the fig. It uses content: attr
to avoid duplicate content in the HTML – not sure if that can work for image.
This method seems a variation on the checkbox method. Not sure if it differs in any essential way from the above.
Some very nice JS-free work here, apparently using radio buttons. My current favorite.
Accessible method
This method allows user to open the modal via keyboard. It’s said to be semantic. However, it appears to depend on JS, which i’d like to avoid if possible.
Dialog method
This one uses the dialog element. Can it be used without JS?
Multi image
This one I can’t tell how the hide/unhide process works. It appears to repeat the image url twice for every image, once for the page and once for the modal.
Multi image with text
This one strongly resembles what i’m after. It appears to list each image url just once. The modal contains the same image that appears on the page, plus additional text. I can’t tell how the show/hide is done. I found it on this page called “CSS Modal Windows”, but i see a bunch of JS.
Social media
No idea how this one works, but i see the same image on the page as in the modal, plus additional content in the modal only.
Other
Can any of the above methods be simplified or improved? Any other methods?