Photo Gallery Website

Project Structure

photo-gallery/
├── index.html
├── styles.css
└── script.js

1. index.html

<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8">
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
&lt;link rel="stylesheet" href="styles.css">
&lt;title>Photo Gallery&lt;/title>
</head> <body>
&lt;div class="gallery">
    &lt;div class="gallery-item">
        &lt;img src="https://via.placeholder.com/300" alt="Photo 1">
    &lt;/div>
    &lt;div class="gallery-item">
        &lt;img src="https://via.placeholder.com/300" alt="Photo 2">
    &lt;/div>
    &lt;div class="gallery-item">
        &lt;img src="https://via.placeholder.com/300" alt="Photo 3">
    &lt;/div>
    &lt;div class="gallery-item">
        &lt;img src="https://via.placeholder.com/300" alt="Photo 4">
    &lt;/div>
    &lt;div class="gallery-item">
        &lt;img src="https://via.placeholder.com/300" alt="Photo 5">
    &lt;/div>
    &lt;div class="gallery-item">
        &lt;img src="https://via.placeholder.com/300" alt="Photo 6">
    &lt;/div>
&lt;/div>
&lt;div class="modal" id="modal">
    &lt;span class="close" id="close">&amp;times;&lt;/span>
    &lt;img class="modal-content" id="modal-img">
&lt;/div>
&lt;script src="script.js">&lt;/script>
</body> </html>

2. styles.css

cssCopy codebody {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
} .gallery {
display: flex;
flex-wrap: wrap;
gap: 10px;
padding: 20px;
} .gallery-item {
flex: 1 1 calc(33.333% - 10px);
cursor: pointer;
} .gallery-item img {
width: 100%;
border-radius: 8px;
transition: transform 0.3s;
} .gallery-item img:hover {
transform: scale(1.05);
} .modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.8);
} .modal-content {
margin: auto;
display: block;
max-width: 80%;
} .close {
position: absolute;
top: 20px;
right: 30px;
color: white;
font-size: 40px;
font-weight: bold;
cursor: pointer;
}

3. script.js

javascriptCopy codeconst galleryItems = document.querySelectorAll('.gallery-item img');
const modal = document.getElementById('modal');
const modalImg = document.getElementById('modal-img');
const closeModal = document.getElementById('close');

galleryItems.forEach(item => {
item.addEventListener('click', () =&gt; {
    modal.style.display = 'block';
    modalImg.src = item.src;
});
}); closeModal.addEventListener('click', () => {
modal.style.display = 'none';
}); window.addEventListener('click', (event) => {
if (event.target === modal) {
    modal.style.display = 'none';
}
});

Instructions

  1. Create the Project Structure: Make a new directory and create the three files listed above.
  2. Copy the Code: Copy the respective code into each file.
  3. Open index.html: Open this file in a web browser to view your photo gallery.

Comments

Leave a Reply

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