Responsive UI design is a constant balancing act between visual polish and readability. Designers love adding floating decorative accents—abstract shapes, background patterns, arrows, and badges. Yet when the viewport shrinks or a container resizes, that decor often drifts over headings and body copy, turning a clean layout into a mess.
The usual fix is a media query like @media (max-width: 900px). But media queries track the browser viewport width, not the actual gap between two elements on the page. In multi-column grids, decor can collide with text even on a wide 4K monitor if an adjacent sidebar expands. Until recently, frontend developers had to wire up ResizeObserver or getBoundingClientRect() in JavaScript, triggering costly layout recalculations on the main thread.
Design engineer Ahmad Shadeed showed how modern CSS can solve this on its own by combining CSS Anchor Positioning, Scroll-Driven Animations, and Container Style Queries.
Collision Detection Mechanics: Gap Measurement and Intentional Overflow
The technique relies on a neat layout trick. First, the browser needs to continuously measure the distance between two elements, which CSS Anchor Positioning handles natively:
- Assign
anchor-name: --decorto the decorative element andanchor-name: --contentto the adjacent text block; - Place an invisible helper element
.measure-gapbetween them and pin its edges withanchor(): its left edge attaches to the right edge of--decor, and its right edge attaches to the left edge of--content.
As a result, .measure-gap always has a width equal to the exact physical gap between the decor and the text.
To turn that continuous width into a discrete signal, you use overflow. Place a ::after pseudo-element with a fixed width—say 24px, your minimum safe gap—inside .measure-gap, and set overflow-x: auto on .measure-gap. Whenever the gap is wider than 24px, nothing overflows. Once the gap shrinks to 23px or less, horizontal scrolling kicks in inside .measure-gap.
Hoisting Scope with timeline-scope and Triggering the Animation
Normally, a scroll-timeline is visible only to descendants of the scrolling container. To let an outside decorative element react to that overflow, you hoist the timeline with timeline-scope.
Declaring timeline-scope: --collision-timeline on a shared ancestor such as body lifts the named timeline upward so independent branches of the DOM can read it. This extends earlier pure-CSS experiments in element intersection detection.
Linking animation-timeline: --collision-timeline triggers a 1-millisecond animation that doesn't move any pixels—it simply flips a custom property --is-colliding: 1. Container Style Queries then take over: the decorative block watches @container style(--is-colliding: 1) and fades out smoothly via opacity: 0; pointer-events: none;.
Practical Implementation and Safe Fallback
Here is the complete declarative CSS setup for measuring the gap and hiding overlapping decor:
/* 1. Hoist the scroll timeline to a shared ancestor */
body {
--is-colliding: 0;
timeline-scope: --collision-timeline;
animation: toggle-collision 1ms linear;
animation-timeline: --collision-timeline;
}
/* 2. Register anchor names on layout elements */
.floating-decor {
anchor-name: --decor;
transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.text-section {
anchor-name: --content;
}
/* 3. Helper block measuring the gap between decor and text */
.measure-gap {
position: absolute;
top: anchor(--decor top);
bottom: anchor(--decor bottom);
left: anchor(--decor right);
right: anchor(--content left);
overflow-x: auto;
pointer-events: none;
visibility: hidden;
scroll-timeline: --collision-timeline x;
}
/* 4. Minimum safe-gap threshold (24px) */
.measure-gap::after {
content: "";
display: block;
width: 24px;
height: 100%;
}
/* 5. Flip the collision flag as soon as overflow occurs */
@keyframes toggle-collision {
from, to {
--is-colliding: 1;
}
}
/* 6. Hide decor reactively via Container Style Queries */
.floating-decor {
@container style(--is-colliding: 1) {
opacity: 0;
pointer-events: none;
}
}
Because this combines bleeding-edge specs, always include a progressive fallback for browsers without full Anchor Positioning support:
/* Safe baseline: hide decor below a fixed breakpoint */
@media (max-width: 992px) {
.floating-decor {
display: none;
}
}
/* Progressive enhancement: restore decor if collision detection is supported */
@supports (timeline-scope: --test) and (anchor-name: --test) {
@media (max-width: 992px) {
.floating-decor {
display: block;
}
}
}
Production Readiness
Today, both features work out of the box in Chromium 125+ browsers (Chrome, Edge, Brave). Safari (WebKit) and Firefox (Gecko) are actively implementing Anchor Positioning and timeline-scope in preview builds.
For design systems, this points toward truly self-contained components. Instead of relying on brittle global breakpoints or JavaScript resize handlers, the browser resolves spatial collisions directly inside its rendering pipeline.


