Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I use CSS to create a responsive image gallery with grid layout?
Asked on May 12, 2026
Answer
Creating a responsive image gallery using CSS Grid is an efficient way to ensure your layout adapts to different screen sizes. Here's a simple approach to achieve this.
<!-- 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 "grid-template-columns" property uses "repeat(auto-fill, minmax(150px, 1fr))" to create flexible columns that adjust based on the container's width.
- "gap" defines the space between grid items, ensuring a consistent look.
- Images are set to "width: 100%" and "height: auto" to maintain their aspect ratio and fill their grid cell.
- This setup ensures the gallery is responsive, automatically adjusting the number of columns based on screen size.
Recommended Links:
