A gallery of thumbnail images where a click opens a larger view with paging controls goes by several names — pop-up, lightbox — but in Bootstrap's documentation the component is a modal. Building it from Bootstrap's own components avoids adding another library, leaves styling entirely under the author's control, and skips writing functionality from scratch for what is a modest requirement.

Wiring the thumbnail grid

The grid itself comes from Bootstrap's grid system. Interactivity is declared through data attributes, which Bootstrap reads to decide which elements respond and how. Two pieces of wiring are needed.

  • On the gallery's parent element (#gallery), add data-toggle="modal" and data-target="#exampleModal". Any click inside the gallery then opens the modal. The data-target value must later be reused as the modal's ID.
  • On each image, add data-target="#carouselExample" and data-slide-to, with the slide values following the images' order. These could equally sit on the image wrappers; the images work fine. The data-target value becomes the carousel's ID.
<div class="row" id="gallery">
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-1">
  </div>
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-2">
  </div>
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-3">
  </div>
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-4">
  </div>
</div>
<div class="row" id="gallery">
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-1.jpg">
  </div>
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-2.jpg">
  </div>
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-3.jpg">
  </div>
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-4.jpg">
  </div>
</div>

The finished component is a standard Bootstrap carousel placed inside a standard Bootstrap modal — one nested in the other, assembled mostly by copying the documentation's markup and substituting our attributes and images. Three consistency points matter:

  1. The modal ID must equal the gallery's data-target.
  2. The carousel ID must equal the images' data-target.
  3. The carousel slides must correspond to the gallery images, in the same order.
<!-- Modal markup: https://getbootstrap.com/docs/4.4/components/modal/ -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        
      <!-- Carousel markup goes here -->


      <div class="modal-footer">
        <button type="button" class="btn btn-secondary">Close</button>
      </div>
    </div>
  </div>
</div>

The carousel markup drops straight into the modal body.

<!-- Modal markup: https://getbootstrap.com/docs/4.4/components/modal/ -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        
      <!-- Carousel markup: https://getbootstrap.com/docs/4.4/components/carousel/ -->
      <div id="carouselExample" class="carousel slide">
          <div class="carousel-inner">
            <div class="carousel-item active">
              <img class="d-block w-100" src="https://css-tricks.com/image-1.jpg">
            </div>
            <div class="carousel-item">
              <img class="d-block w-100" src="https://css-tricks.com/image-2.jpg">
            </div>
            <div class="carousel-item">
              <img class="d-block w-100" src="https://css-tricks.com/image-3.jpg">
            </div>
            <div class="carousel-item">
              <img class="d-block w-100" src="https://css-tricks.com/image-4.jpg">
            </div>
          </div>
          <a class="carousel-control-prev" href="#carouselExample" role="button">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
          </a>
          <a class="carousel-control-next" href="#carouselExample" role="button">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
        </div>
      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-secondary">Close</button>
      </div>
    </div>
  </div>
</div>

The result is verbose, but no more than the documented components already are, minus our attribute and image substitutions.

Handling uneven image dimensions

If images in the carousel differ in size, CSS cropping with Sass normalizes them into a consistent shape. This is optional, not required for the gallery to function.

// Use Bootstrap breakpoints for consistency.
$bootstrap-sm: 576px;
$bootstrap-md: 768px;
$bootstrap-lg: 992px;
$bootstrap-xl: 1200px;


// Crop thumbnail images.
#gallery {
  img {
    height: 75vw;
    object-fit: cover;
    
    @media (min-width: $bootstrap-sm) {
      height: 35vw;
    }
    
    @media (min-width: $bootstrap-lg) {
      height: 18vw;
    }
  }
}


// Crop images in the coursel
.carousel-item {
  img {
    height: 60vw;
    object-fit: cover;
    
    @media (min-width: $bootstrap-sm) {
      height: 350px;
    }
  }
}

Separate files for thumbnails and full-size views

The markup above can reuse the same files in gallery and modal, but it need not. Serving smaller, lighter versions in the grid improves performance: the modal enlarges images to full size anyway, so top quality in the thumbnail is wasted. Bootstrap's data-attribute approach keeps the two sets independent — gallery entries and carousel slides can point at different files.

<div class="row" id="gallery">
  <div class="col-12 col-sm-6 col-lg-3">
    <img class="w-100" src="https://css-tricks.com/image-1-small.jpg">
  
  <!-- and so on... -->
</div>

A clean slate for styling

A themed Bootstrap setup carries the aesthetic automatically, so everything arrives styled to spec. Without theming, custom styles remain straightforward to add: customization is painless with this approach precisely because the markup is fully in hand and Bootstrap's default styling is relatively sparse.