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 --result on as 1e+06, which counter-reset rejects, so the counter prints 0
  • no margins, padding or borders on the items or the query containers: they add to the widths read, and 100cqw reads the content box
  • each query container stretched across its track (justify-items and justify-self at their defaults): aligned to a side or the center, it shrinks to its content, none, and reads 0
  • no other registration of --result on the page: a later @property --result that doesn’t inherit leaves the read right and the counter at 0

Browser support

ChromeSafariFirefox
Example14026158

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.