Use theme.json Global Styles and Fluid Typography in WordPress Block Themes

theme.json global styles and fluid typography in WordPress block themes

theme.json is the single configuration of a block theme: it declares the design tokens (colors, font sizes, font families, spacing), decides which design controls the editor shows, and applies default styles to elements and blocks. WordPress turns it into CSS custom properties and styles that are the same in the editor and on the front end. Since WordPress 6.1 font sizes can be fluid: they scale smoothly between a minimum and a maximum with clamp(), without media queries.

Problem: A block theme defines font sizes in theme.json and again in style.css with media queries. Every change of the heading scale means two files, the editor preview does not match the front end, and the headings are too big on phones and too small on desktops.

Solution: Remove the font-size media queries from style.css and define the whole type scale in theme.json with fluid sizes, applied to the elements in styles.

The configuration

{
    "$schema": "https://schemas.wp.org/trunk/theme.json",
    "version": 2,
    "settings": {
        "typography": {
            "fluid": true,
            "fontFamilies": [
                {
                    "fontFamily": "'Inter', sans-serif",
                    "slug": "inter",
                    "name": "Inter",
                    "fontFace": [
                        {
                            "fontFamily": "Inter",
                            "fontStyle": "normal",
                            "fontWeight": "100 900",
                            "src": [ "file:./assets/fonts/inter-var.woff2" ],
                            "fontDisplay": "swap"
                        }
                    ]
                }
            ],
            "fontSizes": [
                { "slug": "small", "size": "0.875rem", "name": "Small", "fluid": false },
                { "slug": "medium", "size": "1rem", "name": "Medium", "fluid": false },
                { "slug": "large", "size": "1.25rem", "name": "Large", "fluid": { "min": "1.125rem", "max": "1.375rem" } },
                { "slug": "x-large", "size": "1.75rem", "name": "Extra large", "fluid": { "min": "1.375rem", "max": "2rem" } },
                { "slug": "xx-large", "size": "2.5rem", "name": "Huge", "fluid": { "min": "1.75rem", "max": "3rem" } }
            ]
        },
        "color": {
            "palette": [
                { "slug": "primary", "color": "#1a56db", "name": "Primary" },
                { "slug": "accent", "color": "#f59e0b", "name": "Accent" },
                { "slug": "base", "color": "#ffffff", "name": "Base" },
                { "slug": "contrast", "color": "#111827", "name": "Contrast" }
            ]
        },
        "spacing": {
            "spacingScale": { "operator": "*", "increment": 1.5, "steps": 7, "mediumStep": 1.5, "unit": "rem" },
            "units": [ "px", "rem", "em", "%" ]
        }
    },
    "styles": {
        "color": {
            "background": "var(--wp--preset--color--base)",
            "text": "var(--wp--preset--color--contrast)"
        },
        "typography": {
            "fontFamily": "var(--wp--preset--font-family--inter)",
            "fontSize": "var(--wp--preset--font-size--medium)",
            "lineHeight": "1.6"
        },
        "elements": {
            "h1": {
                "typography": { "fontSize": "var(--wp--preset--font-size--xx-large)", "fontWeight": "700", "lineHeight": "1.15" }
            },
            "h2": {
                "typography": { "fontSize": "var(--wp--preset--font-size--x-large)", "fontWeight": "600", "lineHeight": "1.25" }
            },
            "link": {
                "color": { "text": "var(--wp--preset--color--primary)" },
                ":hover": { "color": { "text": "var(--wp--preset--color--accent)" } }
            }
        }
    }
}

How fluid sizes work

"fluid": true in settings.typography turns fluid sizing on for all font sizes. A size with its own fluid object uses that min and max; a size without it gets limits calculated by WordPress from its size; a size with "fluid": false stays fixed. That is why the small and body sizes above are marked false: body text should not shrink on phones. Sizes below the minimum (about 14px by default) are not made fluid anyway.

WordPress writes the result into the presets, for example --wp--preset--font-size--x-large: clamp(1.375rem, …, 2rem), and the elements and the editor’s font size picker use them.

Other parts of the file

  • fontFace (WordPress 6.0+) loads the font files from the theme, so no @font-face in style.css and no Google request is needed;
  • spacingScale generates a series of spacing presets (--wp--preset--spacing--50 and so on) for padding, margin and gaps;
  • styles.elements styles headings, links and buttons everywhere, with the :hover, :focus states;
  • a child theme’s theme.json is merged with the parent’s, so it only needs the values it changes.

NOTE: WordPress caches the processed theme.json. If changes do not show up during development, set define( 'WP_DEVELOPMENT_MODE', 'theme' ); in wp-config.php (WordPress 6.3+), which turns this cache off, and remember that styles saved by users in the Site Editor override the theme file. The structure of a block theme itself is described in Full Site Editing in WordPress 5.9.