Building a lightbox solution
I’m thinking about implementing a lightbox using CSS. The simplest way might be with a .lightbox:target selector to display the image as a flexbox. To close it, we could link a close anchor to # at the top. But if the background is clicked, it won't close without the pointer. So, I could position a background anchor behind the image using z-index. I'll make a code snippet that’s easy for the user to drop into an HTML file with sample images, ensuring to include proper alt text. Below is a complete, CSS-only image gallery with hover effects and lightbox functionality.
Copy this into an .html file and open it in a browser. Replace the image URLs and captions with your own.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>CSS-Only Image Gallery with Lightbox</title>
<link rel="stylesheet" href="styles.css" />
<style>
:root {
--bg: #0f172a;
--accent: #38bdf8;
--caption-bg: rgba(15, 23, 42, 0.85);
--radius: 10px;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
sans-serif;
background: radial-gradient(circle at top, #1e293b 0, #020617 60%);
color: #e5e7eb;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 2rem;
}
.page {
max-width: 1100px;
width: 100%;
}
h1 {
text-align: center;
margin-bottom: 1.25rem;
font-size: clamp(1.6rem, 3vw, 2.1rem);
letter-spacing: 0.04em;
}
.sub {
text-align: center;
margin-bottom: 2rem;
color: #9ca3af;
font-size: 0.95rem;
}
/* ===== GALLERY GRID ===== */
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 1.2rem;
}
.gallery a {
text-decoration: none;
color: inherit;
}
.gallery figure {
position: relative;
overflow: hidden;
border-radius: var(--radius);
cursor: zoom-in;
background: #020617;
box-shadow: 0 14px 40px rgba(15, 23, 42, 0.8);
transition: transform 0.3s ease, box-shadow 0.3s ease;
isolation: isolate;
}
.gallery img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.45s ease, filter 0.45s ease;
transform-origin: center center;
}
/* Hover overlay */
.gallery figure::before {
content: "";
position: absolute;
inset: 0;
background: radial-gradient(
circle at top,
rgba(56, 189, 248, 0.4),
transparent 60%
);
mix-blend-mode: screen;
opacity: 0;
transition: opacity 0.35s ease;
z-index: 1;
pointer-events: none;
}
.gallery figcaption {
position: absolute;
left: 0;
right: 0;
bottom: 0;
padding: 0.7rem 0.9rem;
background: linear-gradient(
to top,
var(--caption-bg),
transparent
);
color: #f9fafb;
font-size: 0.88rem;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 0.75rem;
transform: translateY(100%);
opacity: 0;
transition: transform 0.35s ease, opacity 0.35s ease;
z-index: 2;
}
.gallery figcaption span {
font-weight: 500;
}
.gallery figcaption small {
color: #a5b4fc;
font-size: 0.78rem;
text-transform: uppercase;
letter-spacing: 0.08em;
}
/* Hover effects */
.gallery figure:hover {
transform: translateY(-4px);
box-shadow: 0 20px 50px rgba(15, 23, 42, 0.9);
}
.gallery figure:hover img {
transform: scale(1.08);
filter: brightness(0.85) saturate(1.15);
}
.gallery figure:hover::before {
opacity: 1;
}
.gallery figure:hover figcaption {
transform: translateY(0);
opacity: 1;
}
/* ===== LIGHTBOX (CSS-ONLY) ===== */
.lightbox {
position: fixed;
inset: 0;
background: rgba(15, 23, 42, 0.9);
display: flex;
align-items: center;
justify-content: center;
padding: 1.5rem;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s ease;
z-index: 100;
}
/* Show when :target is active */
.lightbox:target {
opacity: 1;
pointer-events: auto;
}
.lightbox__inner {
position: relative;
max-width: 900px;
width: 100%;
max-height: 90vh;
display: flex;
flex-direction: column;
align-items: center;
gap: 0.8rem;
}
.lightbox__img {
max-width: 100%;
max-height: 80vh;
border-radius: var(--radius);
box-shadow: 0 24px 70px rgba(15, 23, 42, 1);
object-fit: contain;
background: #020617;
}
.lightbox__caption {
font-size: 0.9rem;
color: #e5e7eb;
text-align: center;
}
.lightbox__caption span {
color: var(--accent);
font-weight: 500;
}
/* Clickable dark background to close */
.lightbox__close-area {
position: fixed;
inset: 0;
z-index: -1;
}
.lightbox__close-area:hover {
cursor: zoom-out;
}
/* Close button */
.lightbox__close {
position: absolute;
top: -0.8rem;
right: -0.8rem;
width: 2.2rem;
height: 2.2rem;
border-radius: 999px;
background: rgba(15, 23, 42, 0.95);
display: flex;
align-items: center;
justify-content: center;
text-decoration: none;
color: #e5e7eb;
font-size: 1.3rem;
border: 1px solid rgba(148, 163, 184, 0.4);
box-shadow: 0 10px 30px rgba(15, 23, 42, 0.9);
transition: background 0.2s ease, transform 0.15s ease;
}
.lightbox__close:hover {
background: #020617;
transform: scale(1.05);
}
.lightbox__close:active {
transform: scale(0.96);
}
@media (max-width: 600px) {
.gallery {
gap: 0.9rem;
}
.lightbox__close {
top: -0.4rem;
right: -0.4rem;
}
}
</style>
</head>
<body>
<main class="page">
<h1>CSS‑Only Image Gallery</h1>
<p class="sub">Hover to reveal captions — click an image to open the lightbox.</p>
<!-- ===== GALLERY ===== -->
<section class="gallery">
<!-- Image 1 -->
<a href="#img1">
<figure>
<img
src="https://picsum.photos/id/1015/600/400"
alt="Mountain landscape with a river"
/>
<figcaption>
<span>Mountain River</span>
<small>Landscape</small>
</figcaption>
</figure>
</a>
<!-- Image 2 -->
<a href="#img2">
<figure>
<img
src="https://picsum.photos/id/1025/600/400"
alt="Close-up of a running dog"
/>
<figcaption>
<span>Running Free</span>
<small>Wildlife</small>
</figcaption>
</figure>
</a>
<!-- Image 3 -->
<a href="#img3">
<figure>
<img
src="https://picsum.photos/id/1035/600/400"
alt="Forest and mountain view at sunset"
/>
<figcaption>
<span>Golden Peaks</span>
<small>Sunset</small>
</figcaption>
</figure>
</a>
<!-- Image 4 -->
<a href="#img4">
<figure>
<img
src="https://picsum.photos/id/1041/600/400"
alt="Foggy forest road"
/>
<figcaption>
<span>Into the Fog</span>
<small>Moody</small>
</figcaption>
</figure>
</a>
<!-- Image 5 -->
<a href="#img5">
<figure>
<img
src="https://picsum.photos/id/1050/600/400"
alt="Sea waves hitting the rocks"
/>
<figcaption>
<span>Coastal Waves</span>
<small>Seascape</small>
</figcaption>
</figure>
</a>
<!-- Image 6 -->
<a href="#img6">
<figure>
<img
src="https://picsum.photos/id/1069/600/400"
alt="Night city skyline with lights"
/>
<figcaption>
<span>City Lights</span>
<small>Urban</small>
</figcaption>
</figure>
</a>
</section>
<!-- ===== LIGHTBOXES ===== -->
<!-- Lightbox 1 -->
<section class="lightbox" id="img1">
<!-- Click anywhere dark to close -->
<a href="#" class="lightbox__close-area" aria-label="Close"></a>
<div class="lightbox__inner">
<a href="#" class="lightbox__close" aria-label="Close">×</a>
<img
src="https://picsum.photos/id/1015/1200/800"
alt="Mountain landscape with a river"
class="lightbox__img"
/>
<p class="lightbox__caption">
<span>Mountain River</span> — a calm stream cutting through a rugged landscape.
</p>
</div>
</section>
<!-- Lightbox 2 -->
<section class="lightbox" id="img2">
<a href="#" class="lightbox__close-area" aria-label="Close"></a>
<div class="lightbox__inner">
<a href="#" class="lightbox__close" aria-label="Close">×</a>
<img
src="https://picsum.photos/id/1025/1200/800"
alt="Close-up of a running dog"
class="lightbox__img"
/>
<p class="lightbox__caption">
<span>Running Free</span> — pure energy captured mid‑stride.
</p>
</div>
</section>
<!-- Lightbox 3 -->
<section class="lightbox" id="img3">
<a href="#" class="lightbox__close-area" aria-label="Close"></a>
<div class="lightbox__inner">
<a href="#" class="lightbox__close" aria-label="Close">×</a>
<img
src="https://picsum.photos/id/1035/1200/800"
alt="Forest and mountain view at sunset"
class="lightbox__img"
/>
<p class="lightbox__caption">
<span>Golden Peaks</span> — the last light spilling over alpine ridges.
</p>
</div>
</section>
<!-- Lightbox 4 -->
<section class="lightbox" id="img4">
<a href="#" class="lightbox__close-area" aria-label="Close"></a>
<div class="lightbox__inner">
<a href="#" class="lightbox__close" aria-label="Close">×</a>
<img
src="https://picsum.photos/id/1041/1200/800"
alt="Foggy forest road"
class="lightbox__img"
/>
<p class="lightbox__caption">
<span>Into the Fog</span> — a quiet road fading into the unknown.
</p>
</div>
</section>
<!-- Lightbox 5 -->
<section class="lightbox" id="img5">
<a href="#" class="lightbox__close-area" aria-label="Close"></a>
<div class="lightbox__inner">
<a href="#" class="lightbox__close" aria-label="Close">×</a>
<img
src="https://picsum.photos/id/1050/1200/800"
alt="Sea waves hitting the rocks"
class="lightbox__img"
/>
<p class="lightbox__caption">
<span>Coastal Waves</span> — sea spray and rock‑carved textures.
</p>
</div>
</section>
<!-- Lightbox 6 -->
<section class="lightbox" id="img6">
<a href="#" class="lightbox__close-area" aria-label="Close"></a>
<div class="lightbox__inner">
<a href="#" class="lightbox__close" aria-label="Close">×</a>
<img
src="https://picsum.photos/id/1069/1200/800"
alt="Night city skyline with lights"
class="lightbox__img"
/>
<p class="lightbox__caption">
<span>City Lights</span> — an urban glow stretching into the night.
</p>
</div>
</section>
</main>
</body>
</html>
How it works (pure CSS):
- The gallery is a responsive CSS grid.
- Hover on
<figure>:
- Image scales and darkens.
- Caption slides up with a gradient overlay.
- Each thumbnail links to
#imgN.
The matching <section class="lightbox" id="imgN"> becomes visible using the :target selector.
- Clicking the dark background or the close button (
href="#") removes the :target, hiding the lightbox.
If you’d like, I can strip this down into separate index.html and styles.css files or help you add next/previous buttons (still CSS-only).