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 Mar 07, 2026
Answer
Creating a responsive image gallery with CSS Grid is an efficient way to manage layout changes across different screen sizes. Here's a basic 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">
<img src="image4.jpg" alt="Image 4">
</div>
<!-- END COPY / PASTE -->Additional Comment:
- The "grid-template-columns" property uses "auto-fill" and "minmax" to automatically adjust the number of columns based on the available space.
- The "gap" property defines the space between grid items, making the layout more visually appealing.
- Images are set to fill their grid cell while maintaining their aspect ratio with "width: 100%" and "height: auto".
- This setup ensures that the gallery is responsive and adapts to different screen sizes without media queries.
Recommended Links:
