Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a responsive CSS image gallery using flexbox?
Asked on Apr 28, 2026
Answer
Creating a responsive image gallery with Flexbox is a straightforward process that allows images to adjust based on the screen size. Here's a simple example to get you started.
<!-- BEGIN COPY / PASTE -->
<style>
.gallery {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.gallery img {
flex: 1 1 calc(33.333% - 10px);
max-width: 100%;
height: auto;
}
@media (max-width: 768px) {
.gallery img {
flex: 1 1 calc(50% - 10px);
}
}
@media (max-width: 480px) {
.gallery img {
flex: 1 1 100%;
}
}
</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 ".gallery" class uses Flexbox to arrange images in a row, wrapping them to the next line as needed.
- Each image is set to take up one-third of the container's width, minus the gap, ensuring they fit neatly in a row.
- Media queries adjust the image width for smaller screens, with two breakpoints at 768px and 480px.
- Ensure all images have the same aspect ratio for a consistent layout.
- Adjust the "gap" value to change spacing between images.
Recommended Links:
