Was ist Typisierter CSS attr()-Generator?
The typed CSS attr() function (Chrome 133+) reads an HTML attribute value and uses it in any CSS property, with explicit type casting. Unlike the traditional attr() which only works on the content property of ::before and ::after pseudo-elements and always returns a string, the typed version accepts a type parameter: attr(data-value type(<type>), <fallback>). Supported types include string, number, px, rem, em, percentage, color, deg, s, ms, url, and more. This enables direct data-attribute-driven styling without JavaScript interpolation.
Kurze Antwort
Use typed CSS attr(data-* type(<type>), <fallback>) to read HTML attribute values into any CSS property with type casting. Chrome 133+ only. Supports number, px, rem, em, percentage, color, deg, s, ms, url types. Always provide a fallback value and layer a hardcoded fallback declaration first for cross-browser compatibility.
Einschränkungen
- Typed attr() requires Chrome 133+ or Edge 133+. Safari and Firefox do not yet support it as of May 2026. The function is effectively Chromium-only, so all non-Chromium browsers will fall back to the initial value or the preceding hardcoded declaration.
- The attribute value must match the expected type format exactly. A type(px) attribute expecting "300" breaks on "300px", "300 px", or non-numeric strings. There is no built-in sanitization or lenient parsing — the type system enforces strict value formatting.
- Type conversion failures and parse errors behave differently: a type mismatch triggers the fallback value, but a completely invalid type argument (such as type(foobar)) is a CSS parse error that invalidates the entire declaration. Use only the documented type keywords to avoid invalidating styles.
So nutzt du dieses Tool
- Choose an HTML attribute to read — typically a data-* attribute such as data-size, data-color, or data-speed on the target element.
- Select the CSS property that will receive the attribute value — any CSS property that accepts the chosen type.
- Pick the type for the attr() function that matches your CSS property and expected value format.
- Set a fallback value for when the attribute is missing, empty, or the value cannot be parsed into the specified type.
Wofür du es nutzen kannst
- Build a data-driven progress bar where the width is set by a data-percent attribute using attr(data-percent type(%), 0%) instead of an inline style.
- Create a configurable animation system where data-duration and data-delay HTML attributes control animation timing via attr(data-duration type(s), 300ms).
- Design a component color system where data-accent reads a hex color from the HTML and applies it to background, border, or text color through attr(data-accent type(color), #333).