HTML
htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Photo Gallery</title>
</head>
<body>
<div class="gallery">
<img src="image1.jpg" alt="Image 1" class="gallery-item">
<img src="image2.jpg" alt="Image 2" class="gallery-item">
<img src="image3.jpg" alt="Image 3" class="gallery-item">
<img src="image4.jpg" alt="Image 4" class="gallery-item">
<img src="image5.jpg" alt="Image 5" class="gallery-item">
<img src="image6.jpg" alt="Image 6" class="gallery-item">
</div>
<div class="lightbox" id="lightbox">
<span class="close" onclick="closeLightbox()">×</span>
<img class="lightbox-content" id="lightbox-img">
</div>
<script src="script.js"></script>
</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', () => {
lightbox.style.display = 'flex';
lightboxImg.src = item.src;
});
});
function closeLightbox() {
lightbox.style.display = 'none';
}
Instructions:
- Set Up Your Files: Create three files:
index.html
, styles.css
, and script.js
.
- Add Your Images: Replace
image1.jpg
, image2.jpg
, etc., with the paths to your own images.
- Open in Browser: Open
index.html
in your browser to see the gallery in action.
Leave a Reply