<div id="gallery">
<h1> THIS IS YOUR GALLERY</h1>
<div class="gallery-wrapper" >
<div class="gallery-container h-2">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/11/08/16/57/forest-5724397_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2013/10/02/23/03/mountains-190055_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/11/06/15/33/woman-5718089_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container h-2 w-3">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/11/09/22/20/bridge-5727975_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/11/04/15/29/coffee-beans-5712780_960_720.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2019/12/26/10/44/horse-4720178_960_720.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2017/05/25/21/26/bird-feeder-2344414_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container w-2 h-3">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2014/03/03/16/15/mosque-279015_960_720.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/11/08/10/25/dog-5723334_960_720.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/10/28/23/33/llamas-5694819_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container w-2 h-3">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2015/02/15/03/04/japanese-umbrellas-636870_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2018/10/04/11/31/river-3723439_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://loremflickr.com/320/240?random=13">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2019/12/17/17/09/woman-4702060_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/11/09/22/27/garlic-5728008_960_720.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://loremflickr.com/320/240?random=43">
</div>
</div>
</div>
<div class="gallery-container w-2">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/10/01/17/11/temple-5619197_1280.jpg">
</div>
</div>
</div>
<div class="gallery-container">
<div class="gallery-item">
<div class="image">
<img src="https://cdn.pixabay.com/photo/2020/09/06/20/21/cat-5550033_1280.jpg">
</div>
</div>
</div>
</div>
</div>
/* ***** GALLERIES ***** */
.gallery-wrapper {
padding: 2rem 7rem;
display: grid;
justify-content: center;
grid-template-columns: repeat(5, 1fr);
grid-auto-rows: auto;
grid-gap: 1rem;
grid-auto-flow: dense;
}
/* Another way to do the same without declaring in the classes - remove h-x w-x from classes */
/* .gallery-wrapper {
padding: 2rem 10rem;
display: grid;
justify-content: center;
grid-template-columns: repeat(5, 300px);
grid-auto-rows: 200px;
grid-gap: 1rem;
grid-auto-flow: dense;
}
*/
.gallery-item {
width: 100%;
height: 100%;
position: relative;
filter: drop-shadow(2px 2px 3px #333);
}
.gallery-item .image {
width: 100%;
height: 100%;
overflow: hidden;
}
.gallery-item .image img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 50% 50%;
cursor: pointer;
transition: all 400ms ease-in;
transform: scale(1);
}
.gallery-item .image img:hover {
transform: scale(1.1);
}
.w-1{
grid-column: span 1;
}
.w-2{
grid-column: span 2;
}
.w-3{
grid-column: span 3;
}
.w-4{
grid-column: span 4;
}
.w-5{
grid-column: span 5;
}
.h-1{
grid-row: span 1;
}
.h-2{
grid-row: span 2;
}
.h-3{
grid-row: span 3;
}
.h-4{
grid-row: span 4;
}
.h-5{
grid-row: span 5;
}
/* ***** LIGHTBOX EFFECT ***** */
#lightbox {
position: fixed;
z-index: 1000;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .92);
display: none;
}
#lightbox.active {
display: flex;
justify-content: center;
align-items: center;
}
#lightbox img {
max-width: 90%;
max-height: 80%;
padding: 4px;
/* border-radius: 1%; */
/* background-color: white; */
border: 0.2px solid silver;
}
/* Generic Styles */
:root {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}
html {
height: 100%;
font-family: sans-serif;
font-size: 1vw;
scroll-behavior: smooth;
}
body {
height: 100vh;
min-width: 100vw;
margin: 0;
padding: 0;
}
h1 {
text-align: center;
padding: 1rem;
background: -webkit-linear-gradient(130deg, #A155DA, #41B3A3);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
@media only screen and (max-width: 800px) {
.gallery-wrapper {
grid-template-columns: repeat(2, 2fr);
grid-template-rows: 1fr;
grid-gap: 2;
grid-auto-flow: dense;
}
.gallery-container:nth-child(3n+2) {
grid-column: 1 / span 2;
grid-row-end: span 2;
}
.gallery-container:nth-child(4n+3) {
grid-column: 2 / span 1;
grid-row-end: span 2;
}
/* reseting the css for the class within the divs to show a different way to achieve grid control using the nth childs*/
.w-1,
.w-2,
.w-3,
.w-4,
.w-5 {
grid-column: span 1;
}
.h-1,
.h-2,
.h-3,
.h-4,
.h-5 {
grid-row: span 1;
}
}
@media only screen and (max-width: 400px) {
/* An example to use combined with flex but could also be grids with different repetition of columns */
.gallery-wrapper {
display: flex;
flex-direction: column;
flex: wrap;
gap: 2.5rem;
margin: 5rem;
}
}
// ***** LIGHTBOX AREA *****
const lightbox = document.createElement('div')
const images = document.querySelectorAll('img:not(.profile)')
lightbox.id = 'lightbox'
document.body.appendChild(lightbox)
images.forEach(image => {
image.addEventListener('click', e => {
lightbox.classList.add('active')
const img = document.createElement('img')
img.src = image.src
while (lightbox.firstChild) {
lightbox.removeChild(lightbox.firstChild)
}
lightbox.appendChild(img)
// This blocks the background from scolling when lightbox is open - must set overflow in main css otherwise on the first time you click on the modal the page will scroll to the top
document.body.style.overflowY='hidden'
})
})
lightbox.addEventListener('click', e => {
if (e.target !== e.currentTarget) return
lightbox.classList.remove('active')
document.body.style.overflowY='scroll'
})
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.