惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
雷峰网
雷峰网
The Register - Security
The Register - Security
The Cloudflare Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
I
InfoQ
博客园 - 三生石上(FineUI控件)
H
Help Net Security
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
B
Blog
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recorded Future
Recorded Future
小众软件
小众软件
Martin Fowler
Martin Fowler
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
V
Visual Studio Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
V
V2EX
Blog — PlanetScale
Blog — PlanetScale

Master.dev Blog RSS Feed

Thinking Horizontally in CSS @layer How to Make an Interactive Element Invisible but Accessible The Field Guide to Grid Lanes Cloudflare Workers and Hyperdrive with TanStack Start Totally Free Course: Claude Code Uber for Dogs: How to Stop & Think for Design Fluid Typography with progress() Introduction to Cloudflare Workers for Web Apps Modern Web Guidance Let’s Play With Gap Decorations!
Boundary-Aware Styling in CSS
Preethi Sam on July 7, 2026 · 2026-07-07 · via Master.dev Blog RSS Feed

There are times we might prefer specific styles for elements located near the edges of their containers or the screen.

Different colors, sizes, or rotations based on how close they are to the edge and which side of the container they are on. Instead of relying on class names, item counts, or sibling positions, we can achieve this with a single CSS function.

The view() CSS function is known for its use in scroll-driven animations, affecting elements based on their visibility within a scroll container. However, it can be used without involving any actual scrolling or motion effect.

This article shows you how to do that to create more adaptive interfaces based on their closeness to their container’s boundaries.

How view() Works

The view() function links an animation’s progress to its visibility within its scroll container. When we apply animation-timeline: view(), the browser tracks the position of the element relative to its nearest scrollable container. The animation progress goes like this:

  • 0% Progress (Start): Triggered the exact moment the top/left/leading edge of the element enters the visible boundary of the scrollport.
  • 100% Progress (End): Reached when the bottom/right/trailing edge of the element completely exits the opposite side of the scrollport.

Boundary-Aware Style on Static Elements

Here’s a group of elements that are arranged in a three-column grid.

<main>
  <div class="card">Contacts</div>
  <div class="card">Shipping information</div>
  <div class="card">Find a product</div>
  <!-- more -->
</main>Code language: HTML, XML (xml)
.card {
  background: rgb(16, 111, 235);
}Code language: CSS (css)

I want the cards to have different colors based on their horizontal distribution in their container. So, first, we turn the cards’ parent, <main>, into a scroll container.

How does an element become a scroll container? An element automatically turns into a scroll container when you apply one of these:

  • overflow: scroll;
  • overflow: auto;
  • overflow: hidden; (No scrollbars, but the content is scrollable programmatically)

Let’s update <main> to be a scroll container:

main {
  overflow: hidden;
}Code language: CSS (css)

If <main> doesn’t become a scroll container, the view() function we’ll later apply might take the <body> element as the nearest scroll container.

To change the cards’ colors, I’ll use the hue-rotate() filter. This goes into a @keyframes declaration.

@keyframes cr {
  /* Set the color range */
  from { filter: hue-rotate(180deg); }
}Code language: CSS (css)

Use the keyframes animation on the cards with the view(x) timeline. x is the horizontal axis.

.card {
  background: rgb(16, 111, 235);
  animation: cr;
  animation-timeline: view(x);
}Code language: CSS (css)

Another example:

@property --a {
  syntax: "<angle>";
  initial-value: 0deg;
  inherits: false;
}
@keyframes cr {
  from { --a: 360deg; }
}
main {
  overflow: hidden;
}
.card {
  background: conic-gradient( from var(--a), red, pink, crimson, fuchsia, orange);
  animation: cr;
  animation-timeline: view(x);
}Code language: CSS (css)

Here’s what some simple style changes look like:

Boundary-aware style on draggable elements

<div id="draggable-box">
  Draggable box
</div>Code language: HTML, XML (xml)

The #draggable-box element is a draggable (by JavaScript). Its position on screen, the default scroll container, changes as we drag it across the viewport. So does its color.

@keyframes shiftColor {
  to {
    background-color: rgb(203, 49, 242);
  }
}
#draggable-box {
  background-color: rgb(41, 113, 248);
  animation: shiftColor;
  animation-timeline: view(x);
}Code language: CSS (css)

See the box’s color change as you drag it from the left to the right side of the screen.

You can apply the same principle in HTML sliders.

<input id="slider-range" type="range" min="0" max="100" value="50">Code language: CSS (css)
@keyframes shiftColor {
  from { background-color: red; }
  50% { background-color: yellow; }
  to { background-color: green; }
}
#slider-range {
  /* the slider track */
  &::-webkit-slider-runnable-track{ /* Webkit */
    overflow: hidden;
  }
  &::-moz-range-track {             /* Firefox */
    overflow: hidden;
  }
 /* the slider thumb */
  &::-webkit-slider-thumb {         /* Webkit */
    animation: shiftColor;
    animation-timeline: view(x);
  }
  &::-moz-range-thumb {             /* Firefox */
    animation: shiftColor;
    animation-timeline: view(x);
  }
}Code language: CSS (css)

The slider’s track is set as the scrollable container with overflow: hidden, and view(x) is applied to the thumb. There are three color stops in the @keyframes animation that view() drives. As we slide the thumb along the track from one edge to the other, its color changes between the edges and the middle.

Here’s another example where an anchor-positioned element to the thumb changes.

<div id="slider-container">
  <input id="slider-range" type="range" min="0" max="100" value="50">
  <!-- the anchor-positioned custom thumb -->
  <div id="slider-thumb">&#x2B50;&#xFE0F;</div>
</div>Code language: HTML, XML (xml)
@property --a {
  syntax: "<angle>";
  initial-value: 0deg;
  inherits: false;
}
@keyframes shiftColor {
  from { --a: 370deg }
  50%  { --a: 180deg }
  to   { --a: -10deg }
}
#slider-container {
  overflow: hidden;
}
#slider-thumb {
  background-image: conic-gradient(crimson var(--a), dodgerblue var(--a));
  animation: shiftColor;
  animation-timeline: view(x);
}Code language: CSS (css)

You can experiment with the axes, animation range, animation steps, and more to achieve different results. Just make sure the right ancestor is set as the scroll container using the overflow property.