Day 278 - Range Selection using Modern CSS (combining @function, if(), sibling-index())

Day 278 - Range Selection using Modern CSS (combining @function, if(), sibling-index())

🎉 An Awesome CSS Link a Day - Day 278 🎉

Range Selection using Modern CSS (combining @function, if(), sibling-index())

#AnAwesomeCSSLinkADay #YouDontMessWithCSS #CSS

Don't tell me anymore that CSS is not a programming language.

Today we turn a classic pattern into a dynamic trick, selecting an index range with pure CSS logic. By combining if(), sign(), sibling-index(), and custom properties, you can make ranges adjustable at runtime without touching your selectors.

📐 Theory

The goal is to emulate the behavior of (.container div:nth-child(n + X):nth-child(-n + Y)) using variables and math so you can update X and Y on the fly.

  1. Start from the range idea, the element is selected when its index i satisfies X ≤ i ≤ Y.
  2. Encode the condition with math, use (i − X + 1) × (Y − i + 1): positive values mean inside the range, zero or negative means outside.
  3. Convert positivity to a boolean-like signal with (sign()), giving 1 inside, −1 outside, then check it with (if(style(--g: 1): ...)).
  4. Drive the whole thing with custom properties, declare (--X) and (--Y) on the container so you can tweak them per scope or at runtime.
  5. Use (@property) to type your guard variable, for example (syntax: "<integer>") to make the math predictable.
  6. Package it as a reusable function, for example (@function --range(--X: 1, --Y: 1, --_g <integer>: 0)) that returns 1 when the element is in range.
  7. Keep support in mind, this technique relies on cutting edge features (functions, conditionals, sibling-index), so it is currently Chrome only, perfect for experiments and progressive enhancement.

🚀 Today’s Link

A concise exploration of how to replicate a range style like (nth-child(n + X):nth-child(-n + Y)) with CSS math and conditionals, plus a neat function wrapper. It also shows how to compare the classic selector approach with the variable-driven version side by side.

🔗 Range Selection using Modern CSS

💫 Giveaways

  • A math-driven guard using (sign()) that flips to 1 only inside the range.
  • Variable powered ranges with (--X) and (--Y), no selector rewriting required.
  • A reusable function form, so you can write (background: if(style(--g: --range(3, 8)): red;)).
  • A clear mental model for translating predicates into CSS, multiply two terms that must both be non negative to stay “in range.”
  • Chrome only today, but a great playground for progressive enhancement and future ready patterns.

❓ Why This Rocks

  • Updates ranges by changing variables, faster iteration and fewer selector cascades.
  • Encourages component level tuning, set (--X) and (--Y) per container for instant customization.
  • Reduces duplication, one rule can cover many ranges without generating extra CSS.
  • Bridges classic selectors and modern CSS logic, a gentle step toward function based styling.

🗣️ Join the Conversation

  • Where would dynamic ranges simplify your styles the most, grids, tables, carousels, timelines?
  • Would you wrap this into a design token or keep it as a local component variable?
  • How would you progressively enhance this pattern for non Chrome browsers in your product?
  • What other selector predicates could you translate into math plus if() next?

Let me know your feelings and your experience about this!

To view or add a comment, sign in

More articles by Emiliano Pisu

Others also viewed

Explore content categories