Lightbox

React Bootstrap 5 Lightbox component

Responsive lightbox built with the latest Bootstrap 5. Lightbox is a responsive gallery with the option to enlarge selected photos.

MDB lightbox is a group of images combined in one responsive gallery. Elements are grouped in a thumbnail grid, which can be displayed as a slideshow.

Note: Read the API tab to find all available options and advanced customization


Basic example

A basic example of a lightbox with the most common use case with the bootstrap grid.

        
            
          import React from 'react';
          import { MDBLightbox, MDBLightboxItem, MDBRow, MDBCol } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBLightbox>
                <MDBRow>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/1.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/1.webp'
                      className='w-100'
                    />
                  </MDBCol>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/2.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/2.webp'
                      className='w-100'
                    />
                  </MDBCol>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/3.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/3.webp'
                      className='w-100'
                    />
                  </MDBCol>
                </MDBRow>
              </MDBLightbox>
            );
          }
          
        
    

Image optimization

To ensure the proper performance of the page, it is recommended to include thumbnails of images in the src attribute. Then in the fullscreenSrc property add the path to the image with higher resolution. If the fullscreenSrc property is omitted, the lightbox will display the same image as in the reduced size.

        
            
          import React from 'react';
          import { MDBLightboxItem } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBLightboxItem
                src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/1.webp'
                fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/1.webp'
                className='w-100'
              />
            );
          }
          
        
    

Shadows and rounded corners

You can improve the look of the images by adding shadows and rounded corners.

        
            
          import React from 'react';
          import { MDBLightbox, MDBLightboxItem, MDBRow, MDBCol } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBLightbox>
                <MDBRow>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/1.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/1.webp'
                      className='w-100 shadow-1-strong rounded'
                    />
                  </MDBCol>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/2.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/2.webp'
                      className='w-100 shadow-1-strong rounded'
                    />
                  </MDBCol>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/3.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/3.webp'
                      className='w-100 shadow-1-strong rounded'
                    />
                  </MDBCol>
                </MDBRow>
              </MDBLightbox>
            );
          }
          
        
    

Different sizes

Loaded images can have any aspect ratio. Their size will be automatically adjusted to the window when you open the lightbox.

        
            
          import React from 'react';
          import { MDBLightbox, MDBLightboxItem, MDBRow, MDBCol } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBLightbox>
                <MDBRow>
                  <MDBCol lg='6'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/1.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/1.webp'
                      className='w-100 mb-2 mb-md-4'
                    />
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Square/1.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Square/1.webp'
                      className='w-100'
                    />
                  </MDBCol>
                  <MDBCol lg='6'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Vertical/1.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Vertical/1.webp'
                      className='w-100'
                    />
                  </MDBCol>
                </MDBRow>
              </MDBLightbox>
            );
          }
          
        
    

Zoom level

The zoomLevel property allows you to define the size of a single zoom in / zoom out.

        
            
          import React from 'react';
          import { MDBLightbox, MDBLightboxItem, MDBRow, MDBCol } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBLightbox zoomLevel={0.25}>
                <MDBRow>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/1.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/1.webp'
                      className='w-100'
                    />
                  </MDBCol>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/2.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/2.webp'
                      className='w-100'
                    />
                  </MDBCol>
                  <MDBCol lg='4'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/3.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/3.webp'
                      className='w-100'
                    />
                  </MDBCol>
                </MDBRow>
              </MDBLightbox>
            );
          }
          
        
    

Disabled image

By adding a disabled property to any MDBLightboxItem, you can exclude it from your lightbox. In the example below, the third image has been disabled.

        
            
          import React from 'react';
          import { MDBLightbox, MDBLightboxItem, MDBRow, MDBCol } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBLightbox>
                <MDBRow>
                  <MDBCol lg='3'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/1.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/1.webp'
                      className='w-100'
                    />
                  </MDBCol>
                  <MDBCol lg='3'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/2.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/2.webp'
                      className='w-100'
                    />
                  </MDBCol>
                  <MDBCol lg='3'>
                    <MDBLightboxItem
                      disabled
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/3.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/3.webp'
                      className='w-100'
                    />
                  </MDBCol>
                  <MDBCol lg='3'>
                    <MDBLightboxItem
                      src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/4.webp'
                      fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/4.webp'
                      className='w-100'
                    />
                  </MDBCol>
                </MDBRow>
              </MDBLightbox>
            );
          }
          
        
    

Captions

Image captions can be added using the caption property. If it is not set, the data is taken from the alt property. When you want to disable caption, leave the caption empty.

        
            
          import React from 'react';
          import { MDBLightboxItem } from 'mdb-react-ui-kit';
  
          export default function App() {
            return (
              <MDBLightboxItem
                src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/1.webp'
                fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/1.webp'
                className='w-100'
                caption='Image caption'
                alt='Lightbox image 1'
              />
            );
          }
        
        
    

Outside access

        
            
          import React, { useRef } from 'react';
          import { MDBLightbox, MDBLightboxItem, MDBRow, MDBCol, MDBBtn } from 'mdb-react-ui-kit';
  
          export default function App() {
            const outsideClickRef = useRef(null);

            const handleOutsideClick = () => {
              outsideClickRef.current?.click();
            };
            
            return (
              <>
                <MDBLightbox>
                  <MDBRow>
                    <MDBCol lg='4'>
                      <MDBLightboxItem
                        src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/1.webp'
                        fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/1.webp'
                        className='w-100'
                        alt='Lightbox image 1'
                      />
                    </MDBCol>
                    <MDBCol lg='4'>
                      <MDBLightboxItem
                        src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/2.webp'
                        fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/2.webp'
                        className='w-100'
                        alt='Lightbox image 2'
                        ref={outsideClickRef}
                      />
                    </MDBCol>
                    <MDBCol lg='4'>
                      <MDBLightboxItem
                        src='https://mdbootstrap.com/img/Photos/Thumbnails/Slides/3.webp'
                        fullscreenSrc='https://mdbootstrap.com/img/Photos/Slides/3.webp'
                        className='w-100'
                        alt='Lightbox image 3'
                      />
                    </MDBCol>
                  </MDBRow>
                </MDBLightbox>
                <div className='text-center mt-3'>
                  <MDBBtn onClick={handleOutsideClick}>Open second image</MDBBtn>
                </div>
              </>
            );
          }
          
        
    

Lightbox - API


Import

        
            
        import { MDBLightbox, MDBLightboxItem } from 'mdb-react-ui-kit';
      
        
    

Properties

MDBLightbox

Name Type Default Description Example
tag String 'span' Defines tag of the MDBLightbox element <MDBLightbox tag="section" />
className String '' Add custom class to MDBLightbox <MDBLightbox className="class" />
lightboxRef Reference '' A reference to the MDBLightbox <MDBLightbox lightboxRef={customRef} />

MDBLightboxItem

Name Type Default Description Example
className String '' Add custom class to MDBLightboxItem <MDBLightboxItem className="class" />
src String '' Defines a thumbnail source for the item <MDBLightboxItem src="..." />
fullscreenSrc String '' Defines a fullscreen source for the item <MDBLightboxItem fullscreenSrc="..." />
disabled Boolean '' Creates a disabled image in the lightbox <MDBLightboxItem disabled />