JSFiddle - React, Tailwind, and code Playground
by Slava
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexible Image Expand Animation</title>
</head>
<body>
<div class="content">
<h1>Page Content</h1>
<p>This is some sample content. The expandable image is below:</p>
<a href="#expanded-image1" class="expandable-image" id="expanded-image1">
<img src="https://picsum.photos/800/600" alt="Expandable Image 1">
</a>
<p>More content can go here...</p>
<a href="#expanded-image2" class="expandable-image" id="expanded-image2">
<img src="https://picsum.photos/801/601" alt="Expandable Image 2">
</a>
</div>
<a href="#" class="close-button">CLOSE</a>
</body>
</html>
CSS
body {
padding: 20px;
}
.content {
}
.expandable-image {
display: inline-block;
position: relative;
margin: 10px;
cursor: pointer;
}
.expandable-image img {
display: block;
width: 200px; /* Initial width */
height: auto;
border-radius: 10px;
transition: all 0.3s ease;
}
.expandable-image::after {
content: "";
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.8);
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
}
.expandable-image:target::after {
opacity: 1;
visibility: visible;
}
.expandable-image:target img {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 90vw;
max-height: 90vh;
width: auto;
height: auto;
object-fit: contain;
z-index: 1000;
}
.close-button {
position: fixed;
top: 20px;
right: 20px;
color: white;
text-decoration: none;
font-size: 24px;
z-index: 1001;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
}
.expandable-image:target .close-button {
opacity: 1;
visibility: visible;
}