HTML Questions & Answers Logo
HTML Questions & Answers Part of the Q&A Network
Q&A Logo

How can I create a responsive image gallery using HTML elements?

Asked on Oct 19, 2025

Answer

To create a responsive image gallery using HTML, you can utilize the <div> and <img> elements along with CSS Flexbox or Grid for layout. Here's a basic example using Flexbox to ensure the gallery adjusts to different screen sizes.
<!-- 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;
      }
    </style>
    <!-- END COPY / PASTE -->
Additional Comment:
  • The <div class="gallery"> acts as a container for the images.
  • Using flex-wrap: wrap; allows images to wrap to the next line on smaller screens.
  • The calc(33.333% - 10px) ensures three images per row with a 10px gap.
  • Images are set to width: 100%; to make them responsive within their container.

✅ Answered with HTML best practices.


← Back to All Questions
The Q&A Network