返回资讯中心
外部精选
软件工程
#开发者实时流

开发者利用CSS布局实现浏览器端计算

来源标题:Computing with CSS Layout

一种利用CSS布局引擎进行数值计算的方法被提出:借助 inline-grid 的自动列宽和容器查询单位 cqw,把元素宽度转换为数字,再用 round() 做取整运算。作者称,浏览器每帧运行的高优化布局算法因此可被当作计算工具使用。

Hacker News 最新speckx2026/11/12 · 5 天前30 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

浏览器每一帧都在运行高度优化的算法,只为摆放盒子。这正是布局的意义所在。

当自动列找到最宽的元素时,我们可以把它看作一次最大值运算。flex 行把子元素的宽度相加,得到一次求和。order 按数字对项目排序。每个答案最终都表现为页面上的一个尺寸或位置,而现代 CSS 可以把它作为值读回来。

inline-grid 中的项目默认会拉伸填满其所在列。如果我们给它加上 container-type: inline-size,它就变成一个查询容器:一个其内容可以读取其宽度、而其自身内容永远不会把宽度撑大的元素。

在这个特殊元素内部,100cqw 以长度形式表示该列的宽度,比如 300px。用长度除以 1px 会去掉单位,round() 则把它取整。这就是一个我们可以在 CSS 中用来计算的数字!

<div class="read"> <i style="--value: 300">300</i> <b class="column-width">width = <output></output></b> </div> .column-width { display: block; container-type: inline-size; > * { --result: round(100cqw / 1px); } /* the example: one auto column, each item as wide as its --value */ .read { display: inline-grid; grid-template-columns: auto; > i { width: calc(var(--value) * 1px); } /* content can't print a number, but a counter can */ output::after { counter-reset: result var(--result); content: counter(result); } 通过 @property 注册为 <integer> 后,这个自定义属性在它被设置的地方就变成了数字 300。内部的元素继承该值(而不是再去测量一次 100cqw),然后由计数器把它打印出来。

宽度也可以是一段距离。用 right: anchor(--target left) 把一个元素从父元素左边缘拉伸到另一个元素,它内部的查询容器就能读出那个元素最终落在了多远的位置。这里的 target 位于 flex 行中两根条形之后,因此这段距离就是它们之和。

<div class="position"> <label><input type="radio" name="position" id="position-1" checked> 60 + 40</label> <label><input type="radio" name="position" id="position-2"> 100 + 125</label> <label><input type="radio" name="position" id="position-3"> 150 + 150</label> <div class="position-track"> <i class="position-a"></i> <i class="position-b"></i> <i class="position-target">target</i> <div class="position-distance"> <b class="position-container"><output></output></b> </div> </div> </div> .position-track { --a: 60; --b: 40; position: relative; display: flex; height: 72px; > i { flex: none; } > .position-a { width: calc(var(--a) * 1px); } > .position-b { width: calc(var(--b) * 1px); } > .position-target { anchor-name: --target; } > .position-distance { position: absolute; top: 36px; left: 0; right: anchor(--target left); > .position-container { display: block; container-type: inline-size; > output { --result: round(100cqw / 1px); } /* the radios set the two bars */ body:has(#position-2:checked) .position-track { --a: 100; --b: 125; } body:has(#position-3:checked) .position-track { --a: 150; --b: 150; } /* the bars' numbers, and the sum the read is */ .position-a::before { counter-reset: a var(--a); content: counter(a); } .position-b::before { counter-reset: b var(--b); content: counter(b); } .position-container::before { counter-reset: a var(--a) b var(--b); content: counter(a) " + " counter(b) " = "; } output::after { counter-reset: result var(--result); content: counter(result); } 1.2. 找出最宽者 如果我们把三个元素放进一个只有一列自动列的 grid 中,该列会增长以容纳最宽者,因此其宽度为 max(60, 200, 150),其中的查询容器读出 200。我们从没在 calc() 里写过 max。grid 算法为了摆放这些元素,本来就必须求出一个最大值,而我们借用了那次计算。 <div class="max"> <i style="--value: 60">60</i> <i style="--value: 200">200</i> <i style="--value: 150">150</i> <b class="column-width">max(60, 200, 150) = <output></output></b> </div> .column-width { display: block; container-type: inline-size; } /* the calculation: an auto column, as wide as its widest bar */ .max { display: inline-grid; grid-template-columns: auto; > i { width: calc(var(--value) * 1px); } /* the query container: reads the column, 200 */ > .column-width > * { --result: round(100cqw / 1px); } /* only prints the numbers */ output::after { counter-reset: result var(--result); content: counter(result); } 1.3. 嵌套计算 我们可以像嵌套元素那样嵌套计算。例如,一个贴合内容的 flex 行,其宽度等于各项目之和,因此每一行就是一次求和。容纳这些行的自动列取其中最宽者,因此其查询容器读出 max(60 + 200, 150 + 35 + 45),即 260。 <div class="nest"> <div> <i style="--value: 60">60</i> <i style="--value: 200">200</i> </div> <div> <i style="--value: 150">150</i> <i style="--value: 35">35</i> <i style="--value: 45">45</i> </div> <b class="column-width">max(60 + 200, 150 + 35 + 45) = <output></output></b> </div> .column-width { display: block; container-type: inline-size; } /* the calculation: each row as wide as its bars added up, in an auto column as wide as the widest row */ .nest { display: inline-grid; grid-template-columns: auto; > div { display: flex; > i { flex: none; width: calc(var(--value) * 1px); } /* the query container: reads the column, 260 */ > .column-width > * { --result: round(100cqw / 1px); } /* only prints the numbers */ output::after { counter-reset: result var(--result); content: counter(result); } 1.4. 把它喂回去 一旦布局给了我们一个数字,我们就可以把它再次用作宽度、order、grid-column,或用在另一次计算中。和往常一样,有一个前提:100cqw 测量的是最近的查询容器,因此任何使用该结果的东西都必须位于读取它的元素内部。标记最终会有三层:计算、查询容器,以及它内部的元素。 <div class="max"> <i style="--value: 60">60</i> <i style="--value: 200">200</i> <i style="--value: 150">150</i>

(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →