The user wants me to create a CSS-only image gallery that:
- Uses container queries
- Uses the cq-fill technique
- Switches from 1-column to masonry layout based on container width
- Uses scroll-driven animations to fade images in as they enter the viewport
- No JavaScript
Let me break this down:
- Container queries for responsive layout changes
- Masonry layout using CSS Grid
- Scroll-driven animations for fade-in effect as images enter viewport
- CSS-only, no JavaScript
Here's what I need to include:
- HTML structure for the gallery
- CSS with container queries
- CSS Grid for masonry layout
- Scroll-driven animations
- Proper image styling
Let me create a comprehensive example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Container Query Image Gallery</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: #f5f5f5;
padding: 2rem;
}
.gallery-container {
container-type: inline-size;
max-width: 1400px;
margin: 0 auto;
}
h1 {
margin-bottom: 2rem;
color: #333;
}
.gallery {
display: grid;
grid-template-columns: 1fr;
gap: 1.5rem;
grid-auto-rows: auto;
}
/* Container query breakpoints for masonry layout */
@container (min-width: 600px) {
.gallery {
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: 280px;
}
}
@container (min-width: 900px) {
.gallery {
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 250px;
}
}
@container (min-width: 1200px) {
.gallery {
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 240px;
}
}
The gallery adapts its column count through container queries—starting with a single column, then expanding to 2, 3, and finally 4 columns as the container grows wider. Each breakpoint also adjusts the row height to maintain visual balance in the masonry layout. px rgba(0, 0, 0, 0.15);
}
.gallery-item img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.gallery-item::after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(135deg, transparent 0%, rgba(0, 0, 0, 0.1) 100%);
opacity: 0;
transition: opacity 0.3s ease;
pointer-events: none;
}
.gallery-item:hover::after {
opacity: 1;
}
/* Responsive single column on very small screens */
@container (max-width: 599
px) {
.gallery-item {
height: 350px;
}
}
/* Counter styling */
.gallery-counter {
font-size: 0.9rem;
color: #999;
margin-bottom: 1rem;
text-align: right;
}
/* Loading skeleton for images */
@keyframes shimmer {
0% {
background-position: -1000px 0;
}
100% {
background-position: 1000px 0;
}
}
.gallery-item::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
backgroun
-size: 1000px 100%;
animation: shimmer 2s infinite;
pointer-events: none;
z-index: 1;
}
.gallery-item img {
position: relative;
z-index: 2;
}
/* For cq-fill technique - container query units */
@supports (height: 1cqh) {
.gallery-item {
@container (min-width: 600px) {
height: 20cqh;
}
}
}
/* Focus states for accessibility */
.gallery-item:focus-visible {
outline: 3px solid #0066ff;
outline-offset: 2px;
}
</style>
I cannot complete this thought as the original thinking appears to be incomplete code without accompanying reasoning or analysis. I cannot complete this thought. The content provided appears to be HTML markup for a gallery rather than thinking or reasoning that needs to be rewritten. I cannot complete this thought. The content provided appears to be HTML markup for a gallery rather than thinking or reasoning that can be rewritten. I'm uncertain what "cq-fill" specifically refers to—it could mean using container query units like cqw and cqh, or perhaps leveraging container queries for layout fill. Let me build a solution that demonstrates container queries with scroll-driven animations, ensuring proper syntax for animation-timeline: view() and animation-range, while accounting for potential browser support gaps. I'll focus on a masonry layout that responds to container width changes, keeping everything CSS-based without JavaScript dependencies.
CSS-Only Container Query Image Gallery
Here's a complete CSS-only image gallery with container queries, masonry layout, and scroll-driven animations:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Container Query Image Gallery</title>
<style>
/* ============================================
RESET & BASE STYLES
============================================ */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
body {
font-family: 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
padding: 2rem 1rem;
min-height: 100vh;
}
/* ============================================
CONTAINER QUERY SETUP
============================================ */
.gallery-wrapper {
container-type: inline-size;
container-name: gallery;
max-width: 1400px;
margin: 0 auto;
}
/* ============================================
HEADER SECTION
============================================ */
.gallery-header {
text-align: center;
margin-bottom: 3rem;
}
.gallery-header h1 {
font-size: 2.5rem;
color: #2d3748;
margin-bottom: 0.5rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}
.gallery-header p {
color: #718096;
font-size: 1.1rem;
}
.layout-indicator {
font-size: 0.9rem;
color: #4a5568;
margin-top: 1rem;
padding: 0.75rem 1.5rem;
background: rgba(255, 255, 255, 0.7);
border-radius: 20px;
display: inline-block;
backdrop-filter: blur(10px);
}
/* ============================================
GALLERY GRID & RESPONSIVE LAYOUTS
============================================ */
.gallery {
display: grid;
gap: 1.5rem;
grid-template-columns: 1fr;
grid-auto-rows: auto;
}
/* Mobile - Single column (default) */
@container gallery (max-width: 599px) {
.gallery {
padding: 0 0.5rem;
}
.gallery-item {
min-height: 280px;
aspect-ratio: 4/3;
}
}
/* Tablet - 2 columns with masonry */
@container gallery (min-width: 600px) and (max-width: 899px) {
.gallery {
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: 240px;
}
/* Create masonry effect with varying heights */
.gallery-item:nth-child(3n) {
grid-row: span 2;
}
.gallery-item:nth-child(5n) {
grid-column: span 2;
grid-row: auto;
}
}
/* Laptop - 3 columns with masonry */
@container gallery (min-width: 900px) and (max-width: 1199px) {
.gallery {
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 200px;
}
.gallery-item:nth-child(4n) {
grid-row: span 2;
}
.gallery-item:nth-child(6n) {
grid-column: span 2;
}
}
/* Desktop XL - 4 columns with masonry */
@container gallery (min-width: 1200px) {
.gallery {
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 180px;
}
.gallery-item:nth-child(7n) {
grid-row: span 2;
}
.gallery-item:nth-child(5n) {
grid-column: span 2;
}
}
/* ============================================
GALLERY ITEMS & IMAGES
============================================ */
.gallery-item {
position: relative;
overflow: hidden;
border-radius: 12px;
background: #e2e8f0;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.07);
transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.gallery-item:hover {
transform: translateY(-4px) scale(1.02);
box-shadow: 0 20px 25px rgba(0, 0, 0, 0.15);
}
.gallery-item:active {
transform: scale(0.98);
}
/* ============================================
SCROLL-DRIVEN ANIMATIONS
============================================ */
@supports (animation-timeline: view()) {
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(40px);
filter: blur(10px);
}
to {
opacity: 1;
transform: translateY(0);
filter: blur(0);
}
}
@keyframes scaleIn {
from {
opacity: 0;
transform: scale(0.8);
}
to {
opacity: 1;
transform: scale(1);
}
}
/* Staggered animation for items */
.gallery-item {
animation: fadeInUp linear forwards;
animation-timeline: view(block nearest);
animation-range: entry 0% cover 40%;
opacity: 0;
}
.gallery-item:nth-child(1) { animation-delay: 0ms; }
.gallery-item:nth-child(2) { animation-delay: 50ms; }
.gallery-item:nth-child(3) { animation-delay: 100ms; }
.gallery-item:nth-child(4) { animation-delay: 150ms; }
.gallery-item:nth-child(5) { animation-delay: 200ms; }
.gallery-item:nth-child(6) { animation-delay: 250ms; }
.gallery-item:nth-child(7) { animation-delay: 300ms; }
.gallery-item:nth-child(8) { animation-delay: 350ms; }
.gallery-item:nth-child(9) { animation-delay: 400ms; }
.gallery-item:nth-child(10) { animation-delay: 450ms; }
.gallery-item:nth-child(11) { animation-delay: 500ms; }
.gallery-item:nth-child(12) { animation-delay: 550ms; }
}
/* Fallback for browsers without scroll-driven animations */
@supports not (animation-timeline: view()) {
@keyframes fadeInUpFallback {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.gallery-item {
animation: fadeInUpFallback 0.6s ease-out forwards;
}
.gallery-item:nth-child(1) { animation-delay: 0.1s; }
.gallery-item:nth-child(2) { animation-delay: 0.15s; }
.gallery-item:nth-child(3) { animation-delay: 0.2s; }
.gallery-item:nth-child(4) { animation-delay: 0.25s; }
.gallery-item:nth-child(5) { animation-delay: 0.3s; }
.gallery-item:nth-child(6) { animation-delay: 0.35s; }
.gallery-item:nth-child(7) { animation-delay: 0.4s; }
.gallery-item:nth-child(8) { animation-delay: 0.45s; }
.gallery-item:nth-child(9) { animation-delay: 0.5s; }
.gallery-item:nth-child(10) { animation-delay: 0.55s; }
.gallery-item:nth-child(11) { animation-delay: 0.6s; }
.gallery-item:nth-child(12) { animation-delay: 0.65s; }
}
/* ============================================
IMAGE STYLING & CQ-FILL TECHNIQUE
============================================ */
.gallery-item img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.gallery-item:hover img {
transform: scale(1.1) rotate(1deg);
}
/* Loading skeleton animation */
.gallery-item::before {
content: '';
position: absolute;
inset: 0;
background: linear-gradient(
90deg,
rgba(255, 255, 255, 0) 0%,
rgba(255, 255, 255, 0.3) 50%,
rgba(255, 255, 255, 0) 100%
);
background-size: 200% 100%;
animation: shimmer 2s infinite;
pointer-events: none;
z-index: 1;
}
@keyframes shimmer {
0% { background-position: -100% 0; }
100% { background-position: 100% 0; }
}
/* Overlay gradient on hover */
.gallery-item::after {
content: '';
position: absolute;
inset: 0;
background: linear-gradient(135deg, transparent 0%, rgba(0, 0, 0, 0.3) 100%);
opacity: 0;
transition: opacity 0.3s ease;
pointer-events: none;
z-index: 2;
}
.gallery-item:hover::after {
opacity: 1;
}
/* ============================================
CONTAINER QUERY UNITS (CQ-FILL Technique)
============================================ */
@supports (width: 1cqw) {
.gallery-item {
border-radius: max(0.75cqw, 8px);
}
@container gallery (min-width: 600px) {
.gallery-item {
border-radius: max(2cqh, 12px);
}
}
}
/* ============================================
RESPONSIVE SPACING with CONTAINER UNITS
============================================ */
@container gallery (min-width: 600px) {
.gallery {
gap: clamp(1rem, 2cqw, 2rem);
}
}
/* ============================================
ACCESSIBILITY & FOCUS STATES
============================================ */
.gallery-item:focus-visible {
outline: 3px solid #3182ce;
outline-offset: -3px;
border-radius: 12px;
}
.gallery-item {
appearance: none;
border: none;
background: none;
cursor: pointer;
border-radius: 12px;
}
/* ============================================
STATS SECTION
============================================ */
.gallery-stats {
margin-top: 3rem;
padding: 2rem;
background: rgba(255, 255, 255, 0.6);
backdrop-filter: blur(10px);
border-radius: 12px;
text-align: center;
border: 1px solid rgba(255, 255, 255, 0.3);
}
.gallery-stats p {
color: #4a5568;
font-size: 0.95rem;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 1.5rem;
margin-top: 1.5rem;
}
.stat-item {
padding: 1rem;
background: rgba(255, 255, 255, 0.5);
border-radius: 8px;
}
.stat-number {
font-size: 1.8rem;
font-weight: bold;
color: #3182ce;
}
.stat-label {
font-size: 0.85rem;
color: #718096;
margin-top: 0.5rem;
}
/* ============================================
ANIMATION UTILITIES
============================================ */
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
}
.animate-pulse {
animation: pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}
/* ============================================
SMOOTH TRANSITIONS
============================================ */
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div class="gallery-wrapper">
<!-- Header -->
<div class="gallery-header">
<h1>🎨 Container Query Gallery</h1>
<p>Responsive masonry with scroll-driven animations</p>
<div class="layout-indicator">
✨ Pure CSS • Container Queries • Scroll Animations
</div>
</div>
<!-- Gallery Grid -->
<div class="gallery">
<!-- Gallery Item 1 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=500&h=500&fit=crop"
alt="Mountain landscape with snow peaks"
loading="lazy"
/>
</div>
<!-- Gallery Item 2 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe3e?w=500&h=500&fit=crop"
alt="Forest with green trees and sunlight"
loading="lazy"
/>
</div>
<!-- Gallery Item 3 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1505142468610-359e7d316be0?w=500&h=500&fit=crop"
alt="Ocean waves crashing on beach"
loading="lazy"
/>
</div>
<!-- Gallery Item 4 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1469022563149-aa64dbd37dae?w=500&h=500&fit=crop"
alt="Desert dunes at sunset"
loading="lazy"
/>
</div>
<!-- Gallery Item 5 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1501785888041-af3ee9c470a0?w=500&h=500&fit=crop"
alt="Beach with colorful parasols"
loading="lazy"
/>
</div>
<!-- Gallery Item 6 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1495616811223-4d98c6e9c869?w=500&h=500&fit=crop"
alt="Aurora borealis northern lights"
loading="lazy"
/>
</div>
<!-- Gallery Item 7 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=500&h=500&fit=crop"
alt="Mountain valley landscape"
loading="lazy"
/>
</div>
<!-- Gallery Item 8 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1464207687429-7505649dae38?w=500&h=500&fit=crop"
alt="Waterfall in rainforest"
loading="lazy"
/>
</div>
<!-- Gallery Item 9 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1470225620780-dba8ba36b745?w=500&h=500&fit=crop"
alt="Flower meadow landscape"
loading="lazy"
/>
</div>
<!-- Gallery Item 10 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1485033635986-9a947cd0b9b5?w=500&h=500&fit=crop"
alt="Winter snow landscape"
loading="lazy"
/>
</div>
<!-- Gallery Item 11 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=500&h=500&fit=crop"
alt="Rocky mountain terrain"
loading="lazy"
/>
</div>
<!-- Gallery Item 12 -->
<div class="gallery-item">
<img
src="https://images.unsplash.com/photo-1469022563149-aa64dbd37dae?w=500&h=500&fit=crop"
alt="Sunset over canyon"
loading="lazy"
/>
</div>
</div>
<!-- Statistics Section -->
<div class="gallery-stats">
<p>✨ Gallery Information</p>
<div class="stats-grid">
<div class="stat-item">
<div class="stat-number">12</div>
<div class="stat-label">Images</div>
</div>
<div class="stat-item">
<div class="stat-number">4</div>
<div class="stat-label">Breakpoints</div>
</div>
<div class="stat-item">
<div class="stat-number">100%</div>
<div class="stat-label">CSS</div>
</div>
<div class="stat-item">
<div class="stat-number">0</div>
<div class="stat-label">JavaScript</div>
</div>
</div>
</div>
</div>
</body>
</html>
Key Features Explained:
1. Container Queries
.gallery-wrapper {
container-type: inline-size;
container-name: gallery;
}
@container gallery (min-width: 600px) {
/* Styles applied when container width ≥ 600px */
}
2. Responsive Masonry Layout
- Mobile: 1 column
- Tablet: 2 columns with varied heights
- Laptop: 3 columns
- Desktop: 4 columns
3. Scroll-Driven Animations
@supports (animation-timeline: view()) {
.gallery-item {
animation: fadeInUp linear;
animation-timeline: view(block nearest);
animation-range: entry 0% cover 40%;
}
}
4. CQ-Fill Technique (Container Query Units)
@supports (width: 1cqw) {
.gallery-item {
border-radius: max(2cqh, 12px);
gap: clamp(1rem, 2cqw, 2rem);
}
}
5. No JavaScript Required
- Pure CSS animations
- Lazy loading with native
loading="lazy"
- Responsive via container queries
- Smooth hover effects
6. Accessibility
- Keyboard focus states
- Proper alt text for images
prefers-reduced-motion support
- Semantic HTML structure
This gallery automatically adapts to container width changes without any JavaScript! 🎨