The attr() function in CSS now supports types
amitmerchant.com
amitmerchant.com
Browser bugs are so cute. Just sitting there for decades, comments accreted over time. Tasks spinning for eternity. These are truly the cicadas of bugs.
min-width: attr(foo) /* implicitly requires type to be length */
color: attr(bar) /* implicitly requires type to be color */
Unless you wanted to use a shorthand property like border, but restrict the attribute to only one part of it? border: 1px attr(bdrcolor type(<color>)) /* needs explicit type to prevent specifying border-style */
But that would be easier and cleaner to specify separately? border-width: 1px
border-color: attr(bdrcolor) /* implicitly requires type to be color */
In what scenarios does adding the explicit type information help, rather than having the parser infer the type of the attr() based on the context of the call?CSS variables are another thing to consider perhaps. You can specify their type explicitly as well (albeit using a more verbose syntax):
@property --logo-color {
syntax: "<color>";
inherits: false;
initial-value: attr(logo-color);
} - Variables cascade and cause restyle of whole sub-tree.
- Attributes often are already on the html element, e.g. title, aria, data attributes, so dont need inline styles
- Not mixing data with presentation
Just to name a few