CSS custom properties for WordPress theme development
CSS custom properties (CSS variables) are the base of a design system in a modern WordPress theme. A value is defined once (a color, a font size, a spacing step) and used everywhere, so changing the brand color is one line instead of a hundred. And theme.json generates them for you: every palette color, font size and spacing step becomes a variable like --wp--preset--color--primary, which the editor, the block settings and your stylesheet all share.
Problem: Colors, spacing and font sizes are hard-coded throughout the theme stylesheet, global design changes are tedious, and the theme is out of sync with the editor settings.
Solution: Define the design tokens in theme.json, use the generated --wp--preset--* variables in the CSS, and add your own variables for what theme.json does not cover (shadows, radii, transitions).
The tokens in theme.json
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 2,
"settings": {
"color": {
"palette": [
{ "slug": "primary", "color": "#2563eb", "name": "Primary" },
{ "slug": "secondary", "color": "#7c3aed", "name": "Secondary" },
{ "slug": "neutral", "color": "#6b7280", "name": "Neutral" },
{ "slug": "surface", "color": "#f9fafb", "name": "Surface" }
]
},
"typography": {
"fontSizes": [
{ "slug": "small", "size": "0.875rem", "name": "Small" },
{ "slug": "medium", "size": "1rem", "name": "Medium" },
{ "slug": "large", "size": "1.5rem", "name": "Large" },
{ "slug": "x-large", "size": "2rem", "name": "Extra Large" }
]
},
"spacing": {
"spacingSizes": [
{ "slug": "40", "size": "1rem", "name": "1" },
{ "slug": "50", "size": "2rem", "name": "2" },
{ "slug": "60", "size": "4rem", "name": "3" }
]
}
}
}
WordPress turns them into --wp--preset--color--primary, --wp--preset--font-size--large, --wp--preset--spacing--50: the pattern is --wp--preset--{type}--{slug}. Use lowercase slugs with hyphens. The spacing presets (spacingSizes) need WordPress 6.1 or newer.
Use them in the stylesheet
:root {
/* Own tokens for what theme.json does not have */
--ha-shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
--ha-shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
--ha-radius: 0.5rem;
--ha-transition: 0.2s ease;
--ha-card-bg: var(--wp--preset--color--surface);
}
.site-header {
background-color: var(--wp--preset--color--primary);
padding: var(--wp--preset--spacing--50);
}
.card {
background: var(--ha-card-bg);
border-radius: var(--ha-radius);
box-shadow: var(--ha-shadow-md);
padding: var(--wp--preset--spacing--50);
transition: box-shadow var(--ha-transition);
}
.card:hover {
box-shadow: var(--ha-shadow-sm);
}
/* A section with dark cards: the variable is inherited by all children */
.section--dark {
--ha-card-bg: #1f2937;
}
@media ( prefers-color-scheme: dark ) {
:root {
--ha-card-bg: #1f2937;
--ha-shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.4);
}
}
The card does not use the preset directly but its own --ha-card-bg, which points to the preset by default. Then a section or the dark mode changes one variable of the component, and the WordPress presets stay as they are: WordPress prints them on body, so overriding them on :root would not even work.
var() takes a fallback as the second argument: var( --ha-accent, #2563eb ).
Change them from JavaScript
Variables can be changed at runtime, for example for a theme switcher:
document.documentElement.style.setProperty( '--ha-card-bg', '#fff7ed' );
NOTE: To check which variables WordPress generated, look at the inline <style id="global-styles-inline-css"> in the page <head>, or at the “Computed” tab of the browser inspector for the body element.