2023 Update: Video Walkthrough
Here's the GitHub page if you want to cut to the chase and install.
Keep reading to find out more about using it in a Bootstrap 5 project 👇
I'm building a portfolio site for an independent game developer who needs a marketing page to showcase the game, and I like grabbing Bootstrap 5 to get things running quickly.
I, like you, am pretty bored with the sameness of many of the Bootstrap sites out there. So, in addition to adding some custom CSS, we wanted to add a lightbox photo gallery component.
A little tweaking can really help sections to not feel like they were simply
CTR+C | CTR+Vfrom Bootstrap examples.
There's not an out of the box solution for this. We had three options as I saw it:
Use Modals from Bootstrap
Write custom JS and probably use the Modals on top of it
Find somebody smarter who wrote better JS and use their solution
Bootstrap Modals 📷
Bootstrap has modals which allow for this behavior. However, they are best used for one or two things at a time. When multiple photos are required, you'd have to repeat a bunch of CSS that gets ugly and unruly quickly.
Don't do this.
You could also write custom JS to prevent the repeat code. But this requires you doing a lot of querySelecting and such that is great if you're learning how JS works and how to write stuff from scratch.
But it's not great when you need to ship a product and want the thing to be functional and as clean as possible.
--from bs5-lightbox GitHub description
Because I'm simple, I grabbed the CDN approach for our project.
Throw the script at the bottom of your
<body> and you're good to use this handy library.
Don't Forget BootstrapJS and Popper 😉
<body> as well:
<script src="https://firstname.lastname@example.org/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
Add two things to the elements you want to target for the lightbox:
I was targeting images, and wanted to have them appear as links, so I wrapped them in an
<a href="/assets/img/small-screen3.jpg"> <img class="img-fluid rounded pb-1" data-toggle="lightbox" data-src="/assets/img/small-screen3.jpg" src="/assets/img/small-screen3.jpg" alt="screenshot"> </a>
Go and Do Likewise! 🏆
Hope this has been helpful to you! It was a great find for me in this project, and surprisingly took a fair bit of googling before stumbling across. There are many other custom built components that I came across, but this seemed the cleanest.
Drop a star over on its repo, and let me know if you've used something else similar.
Come say hey 👋...you can find me on Twitter.
Have a great one!