Ask any question about HTML here... and get an instant response.
How do I create a responsive image gallery using HTML and CSS?
Asked on Dec 06, 2025
Answer
To create a responsive image gallery, you can use a combination of HTML for structure and CSS for styling and responsiveness. The gallery can be made responsive using CSS Flexbox or Grid, which allows the images to adjust based on the screen size.
<!-- BEGIN COPY / PASTE -->
<div class="gallery">
<div class="gallery-item">
<img src="image1.jpg" alt="Image 1">
</div>
<div class="gallery-item">
<img src="image2.jpg" alt="Image 2">
</div>
<div class="gallery-item">
<img src="image3.jpg" alt="Image 3">
</div>
<!-- Add more images as needed -->
</div>
<style>
.gallery {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.gallery-item {
flex: 1 1 calc(33.333% - 10px);
box-sizing: border-box;
}
.gallery-item img {
width: 100%;
height: auto;
display: block;
}
@media (max-width: 768px) {
.gallery-item {
flex: 1 1 calc(50% - 10px);
}
}
@media (max-width: 480px) {
.gallery-item {
flex: 1 1 100%;
}
}
</style>
<!-- END COPY / PASTE -->Additional Comment:
- The
<div class="gallery">acts as a container for the images. - Using
flex-wrap: wrap;allows the images to wrap into new lines as needed. - The
calc()function helps maintain consistent spacing between images. - Media queries adjust the number of images per row based on screen size.
✅ Answered with HTML best practices.
Recommended Links:
