Skip to content

[css-values-5] Introduce nth-item() #11103

@tabatkins

Description

@tabatkins

Back in #5009 (comment) I summarized the various "conditional" things we might want to add. We've now added the if() function, and the big one left from that summary list is nth-item() (called nth-value() in that comment, but we should be consistent with random-item()'s naming).

This is a fairly trivial addition functionality-wise, and it has two good arguments for including it:

  1. As argued in the thread, it gives a complementary syntax for conditionals, letting you centralize the actual condition to one spot on your page and just use it to set an integer that'll select things from the list. That way, you don't have to repeat your conditions all over your page if you have a lot of things that'll use the same set of tests.
  2. If you can do random-item() to select a random item from a list, surely you should be able to do nth-item() to select a non-random item from a list, that's just something authors will expect. ^_^

Syntax would just be:

nth-item( <integer> , [ <declaration-value>? ]# )

And it'll select the nth argument according to the first argument, yielding the guaranteed-invalid value if the index goes past the end of the list.

Maybe the first arg can have an optional clamp keyword, which would make the index clamp to the largest valid index instead of being invalid, so authors can use the final argument as the "default" value.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Friday afternoon

    Status

    Tuesday Afternoon

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions