Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a responsive image gallery using CSS Grid?
Asked on Jan 22, 2026
Answer
To create a responsive image gallery using CSS Grid, you can utilize the grid layout to define a flexible grid structure that adapts to different screen sizes. Here's a simple example to get you started.
<!-- BEGIN COPY / PASTE -->
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 10px;
}
.gallery img {
width: 100%;
height: auto;
display: block;
}
</style>
<div class="gallery">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
<!-- Add more images as needed -->
</div>
<!-- END COPY / PASTE -->Additional Comment:
- The CSS Grid property "grid-template-columns" with "repeat(auto-fill, minmax(150px, 1fr))" creates a responsive layout that adjusts the number of columns based on the container's width.
- "gap: 10px" adds spacing between the grid items.
- Ensure images are responsive by setting their width to 100% and height to auto.
- This setup automatically adjusts the number of images per row depending on the screen size.
Recommended Links:
