
Data-Driven Styling with the Typed CSS attr Function
For years, the CSS attr() function was trapped inside a single narrow niche: pulling string values from HTML attributes exclusively for the content property of ::before and ::after pseudo-elements. Whenever a component needed to bind a progress percentage, grid column count, or custom accent color from markup into CSS layout or paint properties, authors had to write verbose inline style="--accent: ..." attributes or rely on JavaScript observers to mirror data attributes into custom properties.
The upgraded typed attr() syntax from CSS Values and Units Level 5—supported in modern Chromium engines since 2025 and steadily adopted across the 2026 web platform—lifts that limitation entirely. By passing a type(<syntax>) descriptor and an optional fallback value directly inside attr(), stylesheets can parse HTML attributes into real <color>, <length>, <number>, <integer>, and <percentage> values on any standard or custom CSS property. Because type parsing and validation happen natively inside the CSS parser, invalid attribute strings safely fall back to the declared default without breaking layout rules or exposing inline-style injection vectors.
Example
<div class="stat-badge" data-accent="#0f766e" data-scale="1.15" data-gap="0.75rem">
<span>Latency</span>
<strong>18ms</strong>
</div>
.stat-badge {
--badge-accent: attr(data-accent type(<color>), #334155);
--badge-scale: attr(data-scale type(<number>), 1);
--badge-gap: attr(data-gap type(<length>), 0.5rem);
display: inline-flex;
align-items: center;
gap: var(--badge-gap);
padding: 0.5rem 0.875rem;
border: 1px solid var(--badge-accent);
color: var(--badge-accent);
font-size: calc(1rem * var(--badge-scale));
}
Implementation
<section class="quota-board" aria-label="Resource quotas">
<article
class="quota-card"
data-color="#2563eb"
data-progress="72%"
data-columns="3"
>
<header class="quota-card__header">
<h2 class="quota-card__title">Object Storage</h2>
<span class="quota-card__value">72%</span>
</header>
<div
class="quota-meter"
role="progressbar"
aria-label="Object Storage usage"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="72"
data-fill="72%"
data-tone="#2563eb"
></div>
</article>
<article
class="quota-card"
data-color="#d97706"
data-progress="88%"
data-columns="3"
>
<header class="quota-card__header">
<h2 class="quota-card__title">Bandwidth Pool</h2>
<span class="quota-card__value">88%</span>
</header>
<div
class="quota-meter"
role="progressbar"
aria-label="Bandwidth Pool usage"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="88"
data-fill="88%"
data-tone="#d97706"
></div>
</article>
</section>
/* ==========================================================================
Typed CSS attr() Component System with Progressive Enhancement
========================================================================== */
.quota-board {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1.25rem;
}
.quota-card {
/* Safe baseline values for browsers without typed attr() support */
--card-accent: #1e293b;
--card-progress: 0%;
border: 1px solid color-mix(in srgb, var(--card-accent) 25%, transparent);
border-radius: 0.75rem;
padding: 1.25rem;
background: #ffffff;
}
.quota-card__header {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 0.75rem;
}
.quota-card__title {
margin: 0;
font-size: 1rem;
color: #0f172a;
}
.quota-card__value {
font-weight: 700;
color: var(--card-accent);
}
.quota-meter {
--meter-fill: 0%;
--meter-tone: #1e293b;
position: relative;
height: 0.625rem;
border-radius: 999px;
background: #e2e8f0;
overflow: hidden;
}
.quota-meter::after {
content: "";
display: block;
width: var(--meter-fill);
height: 100%;
border-radius: inherit;
background: var(--meter-tone);
transition: width 240ms ease;
}
/* Upgrade directly from HTML data-* attributes when typed attr() is supported */
@supports (color: attr(data-tone type(<color>), #000000)) {
.quota-card {
--card-accent: attr(data-color type(<color>), #1e293b);
--card-progress: attr(data-progress type(<percentage>), 0%);
}
.quota-meter {
--meter-fill: attr(data-fill type(<percentage>), 0%);
--meter-tone: attr(data-tone type(<color>), #1e293b);
}
}
@media (prefers-reduced-motion: reduce) {
.quota-meter::after {
transition: none;
}
}
Main Point: The typed CSS attr function parses HTML attributes into validated colors, lengths, and numbers directly in stylesheets, replacing brittle inline styles and JavaScript attribute observers.
Credits
Photo by Jonny James on Unsplash