Create a CSS-only image gallery with hover effects and lightbox functionality.
- User
Create a CSS-only image gallery with hover effects and lightbox functionality.
Assistant
Contemplating Requirements
I'm focused on the initial requirements now. First, it's clear the primary directive is a CSS-only image gallery, which immediately limits options. Beyond that, the hover effects on thumbnails and the lightbox functionality for larger images are key. I'm considering different CSS techniques to achieve both.
Analyzing Implementation Details
Now I'm digging deeper into the implementation plan. The HTML structure feels straightforward: a container, thumbnails linked to modals, and the modals themselves, hidden initially. The crucial part now is the CSS. I'm leaning towards a grid layout for the thumbnails, with each image acting as a link to its corresponding modal via the
:targetpseudo-class.Outlining the Structure
I'm now concretizing the HTML and CSS structures. The HTML will be straightforward, including thumbnails linked to unique modal IDs. The CSS plan is solidifying, with grid layout for the thumbnails and
:targetfor the lightbox. I'm focusing on the:targetimplementation now to ensure smooth transitions and functionality.Defining the Strategy
I'm now solidifying the core approach. Given the constraints, the
:targetpseudo-class for lightbox functionality appears the most viable. This involves links from thumbnails to modals, changing the URL fragment. The key is to manage the display states. Now I will focus on the structure and then the finer points.Structuring the Approach
I'm now determining the optimal strategy. It's clear that the
:targetpseudo-class and URL anchors are the correct approach, given the CSS-only constraint. I'm focusing on a clean, responsive grid layout for the thumbnails, with each leading to a modal. I'll need to handle modal display states with:target, and ensure smooth transitions. Now I need to define the markup.Here is a complete, responsive code solution for a CSS-only image gallery. It uses the
:targetpseudo-class to handle the lightbox functionality without a single line of JavaScript.Features
- CSS Grid Layout: Fully responsive.
- Hover Effects: Thumbnails zoom in and have a caption overlay on hover.
- Lightbox: Clicking an image opens it full screen with a dark backdrop.
- Navigation: "Next" and "Previous" arrows inside the lightbox.
The Code
You can copy and paste this into a single HTML file to test it.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS Only Gallery</title> <style> /* 1. RESET & BASICS */ * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: 'Segoe UI', sans-serif; background-color: #f4f4f4; padding: 20px; } h1 { text-align: center; margin-bottom: 20px; color: #333; } /* 2. GALLERY GRIDS */ .gallery { display: grid; /* Auto-fit creates responsive columns without media queries */ grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px; max-width: 1200px; margin: 0 auto; } /* 3. THUMBNAIL STYLING */ .gallery-item { position: relative; overflow: hidden; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); cursor: pointer; height: 250px; /* Fixed height for uniformity */ } .gallery-item img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.4s ease; display: block; } /* 4. HOVER EFFECTS */ /* Zoom the image */ .gallery-item:hover img { transform: scale(1.1); } /* Overlay Caption (Hidden by default) */ .caption { position: absolute; bottom: -100%; /* Push off screen */ left: 0; width: 100%; background: rgba(0, 0, 0, 0.7); color: white; padding: 10px; text-align: center; transition: bottom 0.3s ease; } /* Show caption on hover */ .gallery-item:hover .caption { bottom: 0; } /* 5. LIGHTBOX MECHANISM (The CSS Magic) */ .lightbox { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.9); display: flex; justify-content: center; align-items: center; z-index: 1000; /* Hidden state */ opacity: 0; pointer-events: none; transition: opacity 0.3s ease; } /* When the ID in the URL matches the ID of the lightbox div */ .lightbox:target { opacity: 1; pointer-events: auto; } /* Lightbox Image Styling */ .lightbox-content { max-width: 90%; max-height: 85vh; box-shadow: 0 0 20px rgba(0,0,0,0.5); border: 2px solid white; animation: zoomIn 0.3s ease; } @keyframes zoomIn { from { transform: scale(0.8); } to { transform: scale(1); } } /* 6. CONTROLS (Close, Next, Prev) */ .close-btn { position: absolute; top: 20px; right: 30px; color: white; font-size: 40px; text-decoration: none; font-weight: bold; transition: color 0.2s; } .nav-btn { position: absolute; top: 50%; transform: translateY(-50%); color: white; font-size: 40px; text-decoration: none; padding: 20px; transition: background 0.2s; } .prev { left: 10px; } .next { right: 10px; } .close-btn:hover, .nav-btn:hover { color: #ff5e57; background: rgba(255,255,255,0.1); border-radius: 5px; } </style> </head> <body> <h1>CSS-Only Gallery & Lightbox</h1> <div class="gallery"> <!-- ITEM 1 --> <div class="gallery-item"> <a href="#img1"> <img src="https://images.unsplash.com/photo-1506744038136-46273834b3fb?w=600&q=80" alt="Landscape"> <div class="caption">Beautiful Mountains</div> </a> </div> <!-- ITEM 2 --> <div class="gallery-item"> <a href="#img2"> <img src="https://images.unsplash.com/photo-1472214103451-9374bd1c798e?w=600&q=80" alt="Nature"> <div class="caption">Green Valley</div> </a> </div> <!-- ITEM 3 --> <div class="gallery-item"> <a href="#img3"> <img src="https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=600&q=80" alt="Fog"> <div class="caption">Misty Morning</div> </a> </div> <!-- ITEM 4 --> <div class="gallery-item"> <a href="#img4"> <img src="https://images.unsplash.com/photo-1501785888041-af3ef285b470?w=600&q=80" alt="Lake"> <div class="caption">Calm Lake</div> </a> </div> </div> <!-- LIGHTBOX OVERLAYS (These live outside the grid to sit on top) --> <!-- Lightbox 1 --> <div id="img1" class="lightbox"> <a href="#" class="close-btn">×</a> <!-- Closes by clearing hash --> <a href="#img4" class="nav-btn prev">❮</a> <img src="https://images.unsplash.com/photo-1506744038136-46273834b3fb?w=1200&q=90" class="lightbox-content"> <a href="#img2" class="nav-btn next">❯</a> </div> <!-- Lightbox 2 --> <div id="img2" class="lightbox"> <a href="#" class="close-btn">×</a> <a href="#img1" class="nav-btn prev">❮</a> <img src="https://images.unsplash.com/photo-1472214103451-9374bd1c798e?w=1200&q=90" class="lightbox-content"> <a href="#img3" class="nav-btn next">❯</a> </div> <!-- Lightbox 3 --> <div id="img3" class="lightbox"> <a href="#" class="close-btn">×</a> <a href="#img2" class="nav-btn prev">❮</a> <img src="https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?w=1200&q=90" class="lightbox-content"> <a href="#img4" class="nav-btn next">❯</a> </div> <!-- Lightbox 4 --> <div id="img4" class="lightbox"> <a href="#" class="close-btn">×</a> <a href="#img3" class="nav-btn prev">❮</a> <img src="https://images.unsplash.com/photo-1501785888041-af3ef285b470?w=1200&q=90" class="lightbox-content"> <a href="#img1" class="nav-btn next">❯</a> </div> </body> </html>How It Works
-
The Anchor Tags (
<a href="#img1">): When you click a thumbnail, the browser changes the URL to end with#img1(this is called a "fragment identifier"). -
The
:targetSelector: In the CSS, we have the rule.lightbox:target. The browser looks for a div with the ID matching the URL hash (e.g.,id="img1"). When it finds a match, that specificdivgets the styles applied (opacity becomes 1, showing the modal). -
Closing the Lightbox: The X button is simply a link to
#or#gallery. Clicking it removes the specific ID from the URL, causing the:targetselector to stop matching, which hides the modal again. -
Navigation: Inside the lightbox for Image 1, the "Next" button is a hardcoded link to
<a href="#img2">. This jumps the:targetstate to the next ID.
Pros & Cons of CSS-Only
- Pros: Very fast, works even if JS is disabled, no dependencies.
- Cons: Changing images updates the browser history (clicking "Back" closes the image instead of going to the previous page), and you have to hardcode the HTML for every popup (or generate it with a server-side language).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.