My Blog

My WordPress Blog

My Blog

My WordPress Blog

Photo Gallery

HTML

htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;link rel="stylesheet" href="styles.css"&gt;
&lt;title&gt;Photo Gallery&lt;/title&gt;
</head> <body>
&lt;div class="gallery"&gt;
    &lt;img src="image1.jpg" alt="Image 1" class="gallery-item"&gt;
    &lt;img src="image2.jpg" alt="Image 2" class="gallery-item"&gt;
    &lt;img src="image3.jpg" alt="Image 3" class="gallery-item"&gt;
    &lt;img src="image4.jpg" alt="Image 4" class="gallery-item"&gt;
    &lt;img src="image5.jpg" alt="Image 5" class="gallery-item"&gt;
    &lt;img src="image6.jpg" alt="Image 6" class="gallery-item"&gt;
&lt;/div&gt;
&lt;div class="lightbox" id="lightbox"&gt;
    &lt;span class="close" onclick="closeLightbox()"&gt;&amp;times;&lt;/span&gt;
    &lt;img class="lightbox-content" id="lightbox-img"&gt;
&lt;/div&gt;
&lt;script src="script.js"&gt;&lt;/script&gt;
</body> </html>

CSS (styles.css)

cssCopy codebody {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
} .gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px;
padding: 10px;
} .gallery-item {
width: 100%;
height: auto;
cursor: pointer;
transition: transform 0.2s;
} .gallery-item:hover {
transform: scale(1.05);
} .lightbox {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
justify-content: center;
align-items: center;
} .lightbox-content {
max-width: 90%;
max-height: 90%;
} .close {
position: absolute;
top: 20px;
right: 30px;
color: white;
font-size: 40px;
cursor: pointer;
}

JavaScript (script.js)

javascriptCopy codeconst galleryItems = document.querySelectorAll('.gallery-item');
const lightbox = document.getElementById('lightbox');
const lightboxImg = document.getElementById('lightbox-img');

galleryItems.forEach(item => {
item.addEventListener('click', () =&gt; {
    lightbox.style.display = 'flex';
    lightboxImg.src = item.src;
});
}); function closeLightbox() {
lightbox.style.display = 'none';
}

Instructions:

  1. Set Up Your Files: Create three files: index.html, styles.css, and script.js.
  2. Add Your Images: Replace image1.jpg, image2.jpg, etc., with the paths to your own images.
  3. Open in Browser: Open index.html in your browser to see the gallery in action.
Photo Gallery

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top