Sure! Below is an HTML code snippet that displays the specified image. When you click on the image, it will zoom in by displaying a larger version in the center of the page with a slightly darkened background. Clicking on the darkened area will close the zoomed image.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Zoom Example</title>
<style>
body, html {
margin: 0;
padding: 0;
}
/* Thumbnail Image */
.thumbnail {
display: block;
margin: 50px auto;
max-width: 90%;
cursor: pointer;
transition: transform 0.2s;
}
.thumbnail:hover {
transform: scale(1.05);
}
/* Modal Container */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.8);
}
/* Modal Content (Zoomed Image) */
.modal-content {
display: block;
margin: 5% auto;
max-width: 80%;
box-shadow: 0 0 10px #000;
}
/* Close Button (Optional) */
.close {
position: absolute;
top: 20px;
right: 35px;
color: #fff;
font-size: 40px;
font-weight: bold;
text-decoration: none;
cursor: pointer;
}
.close:hover {
color: #ccc;
}
</style>
</head>
<body>
<!-- Thumbnail Image -->
<img
src="https://variety.com/wp-content/uploads/2021/07/Rick-Astley-Never-Gonna-Give-You-Up.png?w=1000&h=667&crop=1"
alt="Thumbnail Image"
class="thumbnail"
id="thumbnailImage"
>
<!-- Modal Structure -->
<div id="imageModal" class="modal">
<!-- Close Button (Optional) -->
<a href="#" class="close" id="closeBtn">×</a>
<!-- Zoomed Image -->
<img class="modal-content" id="zoomedImage">
</div>
<script>
// Get elements
const modal = document.getElementById('imageModal');
const thumbnail = document.getElementById('thumbnailImage');
const zoomedImg = document.getElementById('zoomedImage');
const closeBtn = document.getElementById('closeBtn');
// On thumbnail click, open modal and display zoomed image
thumbnail.onclick = function() {
modal.style.display = 'block';
zoomedImg.src = this.src;
};
// Close modal when clicking on the darkened background or close button
modal.onclick = function(event) {
if (event.target == modal || event.target == closeBtn) {
modal.style.display = 'none';
}
};
</script>
</body>
</html>
Explanation:
How to Use:
Save the Code:
Copy the entire code and save it in a file with an .html extension, like zoom_image.html.
Open in a Browser:
Open the file using any modern web browser (e.g., Chrome, Firefox, Edge).
Interact with the Image:
Upon loading, you will see the thumbnail image.
Click on the image to trigger the zoom effect.
To close the zoomed image, click anywhere on the darkened background or on the close button (×).
Notes:
Responsive Design: The modal and images are styled to be responsive and should work well on different screen sizes.
Customization:
You can adjust the max-width properties in the CSS to change how large the images appear.
The background opacity can be modified by changing the alpha value in rgba(0, 0, 0, 0.8).
Example Screenshot:
Here's how the zoomed image appears over the darkened background:
[image: Zoomed Image Example] open
(Note: Replace the URL with an actual screenshot if necessary.)
Troubleshooting:
Image Not Displaying: Ensure you have an active internet connection since the image is loaded from an external URL.
Modal Not Working: Check that JavaScript is enabled in your browser, as the zoom functionality relies on it.
Feel free to modify the code to suit your needs or to integrate it into an existing project!