Outlining CSS-only gallery with container queries and cq-fill
The plan uses a container with container-type inline-size to enable container queries that switch the gallery from one column to a masonry-like multi-column layout based on container width. The "cq-fill" technique employs grid-template-columns: repeat(auto-fit, minmax(min(250px, 100%), 1fr)) to make columns fill the container responsively. Scroll-driven animations use animation-timeline: view() and animation-range to fade images in on viewport entry, all without JavaScript.Finalizing CSS gallery with container queries, cq-fill, and scroll animations
The plan applies container queries on a container-type inline-size wrapper to switch from one-column to multi-column masonry using CSS columns, while each item uses inline-size: 100cqw to fill the container track per the cq-fill technique. Scroll-driven fade-in animations are added via animation-timeline: view() for a fully CSS-only solution. A complete HTML+CSS demo is prepared accordingly.
Below is a self‑contained example that ticks all of the boxes you asked for:
- Uses a container query on the gallery itself (no media queries).
- Uses the cq‑fill trick (
inline-size: 100cqw) so each item always fills the width of its column/track.
- Switches from a single column to a masonry‐style multi‑column layout based on the gallery’s own width.
- Fades items in with a scroll‑driven animation (CSS only, no JS).
Note: Scroll‑driven animations (animation-timeline: view()) are currently shipping in Chromium‑based browsers and Safari TP. The rest of the layout will still work elsewhere. A prefers-reduced-motion guard is included for accessibility.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CQ Masonry Gallery (CSS‑only)</title>
<style>
:root {
--gap: 1rem;
--radius: 12px;
color-scheme: light dark;
font-family: system-ui, sans-serif;
}
body {
margin: 0;
background: radial-gradient(circle at 10% 10%, #f5f5f8, #e8ebf4 50%, #e5e6ed);
min-height: 100vh;
}
.page {
max-width: 1100px;
margin: 0 auto;
padding: 2.5rem 1.25rem 3rem;
}
h1 {
margin: 0 0 1.5rem;
font-size: clamp(1.7rem, 2vw + 1rem, 2.4rem);
}
/* The gallery is the query container */
.gallery {
container: gallery / inline-size;
column-count: 1; /* default: one column */
column-gap: var(--gap);
}
/* Switch to masonry columns when the container gets wide enough */
@container gallery (min-width: 640px) {
.gallery {
column-count: 3;
}
}
/* Items */
.gallery figure {
inline-size: 100cqw; /* cq‑fill: fill the column width */
margin: 0 0 var(--gap);
break-inside: avoid;
border-radius: var(--radius);
overflow: hidden;
background: color-mix(in srgb, Canvas 85%, #000 7%);
box-shadow:
0 10px 30px rgba(0, 0, 0, 0.08),
0 4px 12px rgba(0, 0, 0, 0.06);
opacity: 0;
transform: translateY(12px);
animation: fade-in 0.8s ease forwards;
animation-timeline: view(); /* scroll-driven */
animation-range: entry 10% cover 40%;
}
.gallery img {
width: 100%;
display: block;
height: auto;
}
.gallery figcaption {
padding: 0.75rem 0.85rem 0.9rem;
font-size: 0.9rem;
color: color-mix(in srgb, currentColor 80%, transparent);
}
/* Fade-in keyframes */
@keyframes fade-in {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: translateY(0); }
}
/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
.gallery figure {
animation: none;
opacity: 1;
transform: none;
}
}
</style>
</head>
<body>
<main class="page">
<h1>CSS‑Only Container‑Query Masonry</h1>
<section class="gallery">
<figure>
<img src="https://picsum.photos/seed/1/800/600" alt="">
<figcaption>Mountain morning</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/seed/2/800/900" alt="">
<figcaption>Evening meadow</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/seed/3/800/700" alt="">
<figcaption>Urban shadows</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/seed/4/800/1100" alt="">
<figcaption>Forest depth</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/seed/5/800/650" alt="">
<figcaption>River bend</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/seed/6/800/950" alt="">
<figcaption>Golden hour cliffs</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/seed/7/800/820" alt="">
<figcaption>Sea breeze</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/seed/8/800/500" alt="">
<figcaption>Sunlit trail</figcaption>
</figure>
<figure>
<img src="https://picsum.photos/seed/9/800/780" alt="">
<figcaption>Foggy hills</figcaption>
</figure>
</section>
</main>
</body>
</html>