sum()
The total of a list: its numbers added up.
In CSS, a flex row at its max-content width is as wide as its items together; an auto column holds the row, and a query container in the column reads the width back.
Example
Result
Markup
<div class="sum">
<div>
<i style="--value: 5"></i>
<i style="--value: 1"></i>
<i style="--value: 2"></i>
<i style="--value: 3"></i>
<i style="--value: 4"></i>
</div>
<b><output></output></b>
</div>
CSS
@property --result { syntax: '<integer>'; inherits: true; initial-value: 0; }
.sum output::after { counter-reset: result var(--result); content: counter(result); }
.sum { display: inline-grid; width: max-content; grid-template-columns: auto; }
.sum > div { display: flex; }
.sum > div > i { flex: none; width: calc(var(--value) * 1px); }
.sum > b { container-type: inline-size; }
.sum > b > output { --result: round(100cqw / 1px); }
dCSS Example
With dCSS, an element whose class is named after the function (dCSS’s docs).
Result
Markup
<link rel="stylesheet" href="dcss.css">
<link rel="stylesheet" href="families/array.css">
<figure class="dcss-sum">
<ul>
<li style="--value: 5"></li>
<li style="--value: 1"></li>
<li style="--value: 2"></li>
<li style="--value: 3"></li>
<li style="--value: 4"></li>
</ul>
<output></output>
</figure>
Limits
These are the Example’s; the dCSS Example’s are in dCSS’s docs.
- values of 0 or more: calc() clamps a negative width to 0
- whole-number values, or halves and quarters: each width is laid out in 1/64 of a pixel in Chrome and WebKit and 1/60 in Firefox, so 256 values of 1/256 add up to 0
- no gap between the items: a gap adds to the sum
- the grid at its natural width (
width: max-content): a grid stretched by its parent, as an item of a grid or of a column flexbox, stretches its auto columns, and the read with them - a whole-number answer: the read rounds to 1px
- each answer under 1,000,000: from a million up, Chrome passes
--resulton as1e+06, whichcounter-resetrejects, so the counter prints 0 - no margins, padding or borders on the items or the query containers: they add to the widths read, and
100cqwreads the content box - each query container stretched across its track (
justify-itemsandjustify-selfat their defaults): aligned to a side or the center, it shrinks to its content, none, and reads 0 - no other registration of
--resulton the page: a later@property --resultthat doesn’t inherit leaves the read right and the counter at 0
Browser support
| Chrome | Safari | Firefox | |
|---|---|---|---|
| Example | 140 | 26 | 158 |
| dCSS Example | 140 | 26.2 | 158 |
Each row gives the first versions that support every feature its example uses, by MDN’s browser-compat-data. The Example was run in Chrome 155, Safari’s WebKit 26.4 and Firefox 158, the dCSS Example in Chrome 155 and Safari’s WebKit 26.4.