-
Notifications
You must be signed in to change notification settings - Fork 756
Description
This is a proposal for adding parameterized selectors and inline expressions to CSS for runtime computation
- Introduction
CSS is currently a declarative styling language, but lacks runtime mechanisms for logic, parameterization, and computation based on computed element values.
Today, developers rely on:
Preprocessors (Sass, LESS, Stylus) for variables and conditional logic (compile-time only).
CSS Houdini or JavaScript for runtime computations.
This proposal introduces parametric CSS rules to allow runtime-safe computations directly within CSS.
- Syntax
2.1 Parametric Selector
.selector(param1, param2, ...) { ... }
Parameters are bound to intrinsic element properties.
Example reserved keywords:
w → element width
h → element height
c → computed text color
bg → computed background color
2.2 Expressions
Expressions may contain:
Arithmetic: +, -, *, /
Comparisons: ===, !=, <, >, <=, >=
Conditional ternary: condition ? value1 : value2
- Examples
.card(h) {
height: h * 2;
background: h === 100 ? red : blue;
}
.title(w) {
font-size: w > 500 ? 2rem : 1rem;
}
.alert(c) {
border-color: c === red ? black : c;
}
-
Processing Model
-
Browser computes element’s intrinsic properties (w, h, etc.).
-
Parametric rules are resolved after style computation.
-
On layout changes (resize, reflow), parametric rules are re-evaluated.
-
If parameters are unavailable, expressions resolve to auto.
- Security and Performance
No arbitrary JavaScript execution is allowed.
Only safe arithmetic/conditional expressions are permitted.
Evaluations are sandboxed and optimized for reflow events.
- Relation to Other Features
Custom Properties: parametric CSS extends runtime flexibility beyond variables.
Container Queries: parametric rules complement container-based styling.
Houdini: advanced logic should still use Houdini Paint/Properties API.
Preprocessors: unlike Sass/LESS, evaluation happens at runtime, not compile time.
- Future Work
Allow user-defined parameters (.card(@ratio)).
Allow referencing other computed properties (.card(sibling.w)).
Integration with Houdini for custom bound values.