#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.
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.
- Start from the range idea, the element is selected when its index i satisfies X ≤ i ≤ Y.
- Encode the condition with math, use (i − X + 1) × (Y − i + 1): positive values mean inside the range, zero or negative means outside.
- Convert positivity to a boolean-like signal with (sign()), giving 1 inside, −1 outside, then check it with (if(style(--g: 1): ...)).
- Drive the whole thing with custom properties, declare (--X) and (--Y) on the container so you can tweak them per scope or at runtime.
- Use (@property) to type your guard variable, for example (syntax: "<integer>") to make the math predictable.
- 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.
- 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.
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.
- 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.
- 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.
- 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!
Codepen Example 2: https://codepen.io/t_afif/pen/azdmQNr
Codepen Example 1: https://codepen.io/t_afif/pen/PwZGyeP
Today's link: https://css-tip.com/range-selection/