Skip to main content

Responsive Modal Image Gallery

Clicking an image in a gallery can open a larger copy of it without taking the visitor away from the page. That larger copy sits in a popup window, a modal, layered above the main content on a semi-transparent background, and a close button or "X" sign in its top-right corner dismisses it.

The example below combines HTML, CSS and JavaScript to build such a gallery, and it is responsive.

CSS builds the modal window and keeps it hidden at first, and JavaScript reveals it with the right image after the user clicks on it.

Example

Prompt
Create a responsive image gallery with four landscape images. When a user clicks an image, a modal overlay appears showing the image larger with a caption and a close button. Use CSS flexbox for the gallery layout with media queries so images show 4 per row on desktop, 2 on tablets, and 1 on mobile. Add a zoom-in animation when the modal opens.
Output: complete HTML page