Responsive CSS Image Gallery
This example demonstrates how to create a responsive image gallery using HTML and CSS Grid. The gallery automatically adjusts the number of columns based on the available screen space and includes image captions, hover effects, and consistent image sizing.
Mountain Landscape
A landscape image demonstrating how CSS Grid arranges gallery items into responsive columns.
Ocean View
An example of a gallery item with a descriptive caption displayed below the image.
Forest Trail
A gallery item demonstrating consistent image dimensions and spacing between cards.
City Skyline
This item demonstrates how additional images automatically fit into the grid layout.
Desert Sunset
A responsive gallery card with a colored placeholder image and supporting text.
Lake Reflection
The final gallery item demonstrates how the grid handles multiple images across rows.
Try It: Resize the browser window to see the gallery automatically change its number of columns. Move your mouse over the gallery items to see the lift and shadow effects. Replace the placeholder image URLs with your own images to customize the gallery.
How the Image Gallery Works
The gallery uses CSS Grid to arrange images into flexible columns and rows.
Each image is placed inside an HTML figure element, with a figcaption element providing descriptive information.
The following CSS creates the responsive grid:
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));
gap: 20px;
The auto-fit keyword allows the grid to automatically adjust the number of columns.
The minmax() function defines flexible column sizes, while gap creates consistent spacing between gallery items.
Controlling Image Sizes
The width: 100% declaration makes each image fill the width of its gallery item.
The height: 200px declaration establishes a consistent displayed image height.
The object-fit: cover declaration allows images to fill their containers while maintaining their original proportions.
When an image's aspect ratio differs from the container, portions of the image may be cropped.
.gallery-item img {
display: block;
width: 100%;
height: 200px;
object-fit: cover;
}
You can change the height or use object-fit: contain when displaying the entire image is more important than filling the available space.
Adding Gallery Hover Effects
The :hover pseudo-class applies visual changes when the pointer moves over a gallery item.
The translateY() function moves the item slightly upward, while box-shadow increases its visual depth.
.gallery-item:hover {
transform: translateY(-5px);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}
The transition property creates a smooth visual change between the normal and hover states.
The prefers-reduced-motion media query removes the movement effect for visitors who request reduced motion.
Responsive Gallery Design
CSS Grid automatically adjusts the number of gallery columns as the viewport width changes.
On wider screens, several images appear in the same row. On smaller screens, fewer images fit across the available width.
A media query at 600 pixels or less reduces the gallery spacing, image height, and caption padding.
This approach allows the gallery to adapt without defining a separate column count for every screen size.
Image Gallery Accessibility
Use meaningful alt text to describe informative images for visitors using assistive technology.
The figure and figcaption elements associate each image with its visible caption.
For decorative images that do not convey meaningful information, an empty alt attribute may be appropriate.
The loading="lazy" attribute allows images outside the initial viewport to be loaded when needed.
Ensure that captions remain readable and that important information is not available only through hover effects.
Customizing the Image Gallery
You can replace the placeholder images with photographs, illustrations, product images, or portfolio screenshots.
Change the 250px value in the grid declaration to control the preferred minimum column width.
Modify the gap property to increase or decrease the spacing between gallery items.
Adjust the image height, borders, shadows, and caption styling to match your website's design.
To add another image, duplicate an existing figure element and update its image source, alternative text, heading, and description.
Use descriptive image filenames and alternative text.
Optimize image file sizes to improve loading performance.
Use consistent image dimensions for a balanced appearance.
Keep captions concise and relevant to their images.
Test the gallery on desktop, tablet, and mobile screens.