maxIndex()
Where the largest number is: its position, from 0.
In CSS, each item is its value × 1000 + 1000 − its place (sibling-index()) px wide: an auto column is as wide as the widest, the largest value and, on a tie, the first; a query container reads the width, and 999 minus the width mod 1000 is the place, from 0.
Example
<div class="max-index">
<i style="--value: 3"></i>
<i style="--value: 2"></i>
<i style="--value: 1"></i>
<i style="--value: 1"></i>
<i style="--value: 6"></i>
<i style="--value: 2"></i>
<i style="--value: 4"></i>
<b><output></output></b>
</div>
@property --result { syntax: '<integer>'; inherits: true; initial-value: 0; }
.max-index output::after { counter-reset: result var(--result); content: counter(result); }
.max-index { display: inline-grid; width: max-content; grid-template-columns: auto; }
.max-index > i { width: calc((var(--value) * 1000 + 1000 - sibling-index()) * 1px); }
.max-index > b { container-type: inline-size; }
.max-index > b > output { --result: calc(999 - mod(round(100cqw / 1px), 1000)); }
greatestIndex() is the same on numbers; d3-array’s also takes a comparator, to pick an item of a list by one of its fields, which this example can’t take.
dCSS Example
With dCSS, an element whose class is named after the function (dCSS’s docs).
<link rel="stylesheet" href="dcss.css">
<link rel="stylesheet" href="families/array.css">
<link rel="stylesheet" href="families/format.css">
<figure class="dcss-max-index">
<ol>
<li style="--value: 3"></li>
<li style="--value: 2"></li>
<li style="--value: 1"></li>
<li style="--value: 1"></li>
<li style="--value: 6"></li>
<li style="--value: 2"></li>
<li style="--value: 4"></li>
</ol>
<div>
<output class="dcss-format-fixed" style="--value: var(--dcss-max-index); --precision: 0"></output>
</div>
</figure>
Limits
These are the Example’s; the dCSS Example’s are in dCSS’s docs.
- whole-number values of 0 or more
- fewer than 1000 items, and each item’s width, the encoded number, under about 16 million px
- the items first among their parent’s children:
sibling-index()counts from its first child - a list of one number or more: with none, the read gives a number where d3 gives −1
- 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 - 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.2 | 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.