Skip to content
AITroveRead. Build. Understand.
Make this comfortable

CSS custom properties: share tokens without losing fallbacks

Last updated: 5 Oct 20265 min read
tutorial
BeginnerBy AITrove Editorial

Custom properties hold token sequences and normally inherit. The var() function substitutes a property value when the declaration is computed.

How the rule works

A receipt surface has one border color across summary cards and form fields. Scope the token to that surface so another product section can use a different palette without a global collision. The fallback keeps the border visible if the token is absent, but it does not rescue a value that is present yet invalid for the destination property. A token that changes color may affect contrast in every component that reads it. Keep state colors paired with their backgrounds, document what owns each token, and check the computed value when a component looks unexpectedly transparent.

css
.receipt-surface { --receipt-border: #b7c4d1; }
.receipt-surface .receipt-card {
  border: 1px solid var(--receipt-border, #8b9bad);
  padding: 1.25rem;
}

Cost and verification

The browser substitutes the token during style calculation; no script is required. Shared tokens reduce duplicated literals, though a large unscoped token system can make simple changes harder to trace. The fallback in this sample is an alternate border color, not a full theme. Test a card inside and outside the token scope, then inspect dark and forced-color environments. A custom property does not automatically type-check like a standard property unless explicitly registered.

Common Mistakes

  • Do not assume var() fallback applies after an invalid substituted value.
  • Do not put every token on the document root without an owner.
  • Do not change foreground colors without reviewing their paired surfaces.

Connected lessons

Extend this decision

Related component decisions

css
foundations
web
Storage details