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 Feb 04, 2026
Answer
Creating a responsive image gallery with CSS Grid is a straightforward process that allows for flexible layouts. Here's a simple example to get you started.
<!-- BEGIN COPY / PASTE -->
<div class="gallery">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
<img src="image4.jpg" alt="Image 4">
</div>
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 10px;
}
.gallery img {
width: 100%;
height: auto;
display: block;
}
</style>
<!-- END COPY / PASTE -->Additional Comment:
- The "grid-template-columns" property uses "repeat(auto-fill, minmax(150px, 1fr))" to create a responsive layout that adapts to the container size.
- "auto-fill" automatically places as many columns as will fit, each at least 150px wide.
- The "gap" property adds spacing between the images for a clean look.
- Ensure images have a uniform aspect ratio or use object-fit for consistent presentation.
Recommended Links:
