Max of sums
The largest of several sums.
In CSS, each row is a flex row that asks for the total width of its items, and one auto grid column is as wide as the largest of those totals; a query container under the rows reads the column.
Example
Result
Markup
<div class="max-of-sums">
<div>
<i style="--value: 60"></i>
<i style="--value: 200"></i>
</div>
<div>
<i style="--value: 150"></i>
<i style="--value: 35"></i>
<i style="--value: 45"></i>
</div>
<b><output></output></b>
</div>
CSS
@property --result { syntax: '<integer>'; inherits: true; initial-value: 0; }
.max-of-sums output::after { counter-reset: result var(--result); content: counter(result); }
.max-of-sums { display: inline-grid; width: max-content; grid-template-columns: auto; }
.max-of-sums > div { display: flex; }
.max-of-sums > div > i { flex: none; width: calc(var(--value) * 1px); }
.max-of-sums > b { container-type: inline-size; }
.max-of-sums > b > output { --result: round(100cqw / 1px); }
Limits
- values of 0 or more, no gap between the items, and items that do not flex
- 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 |
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.