Estimated reading time: 7 minutes
In this exercise, you will practice creating a theme.json file for a WordPress block theme.
You will create presets and add some basic styling to the theme.
This lesson is suitable for those who want a quick recap of the global styles lesson.
For this full site editing lesson, you will need:
- A WordPress installation with some test content and the Gutenberg plugin.
- A block theme with at least one header and one footer template part.
If you created a theme with template parts in the previous exercise, you can continue using it for this exercise. Or, you can download the theme from GitHub here: Part two of the lesson files. Download the final code for the project created in this lesson.
Updated June 25, 2022.
With the help of the theme.json file, we are going to turn this:

into this:

Creating the theme.json file
Open your theme files in your code editor. In the root theme folder, If you haven’t already done so in the previous lessons, create a file called theme.json. The file will have three sections: settings, styles, and templateParts.
I recommend that you add one section at a time because one single error in the file can cause other settings to break.
First, you will add the default settings. Default settings apply to all blocks, but you can override them per block.
Start with the opening and closing brackets:
{
}
Inside these brackets, add the schema, version number (2) and the settings
section:
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version":2,
"settings": {
}
}
Color palette
Next, add the first colors to the color palette. Remember to encase the colors with brackets and separate them with a comma. There should not be a trailing comma after the last color.
This setting is the equivalent of the editor-color-palette
theme support.
The slug
will be used in the generated CSS class, and the name
is the visible name in the editor:
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version":2,
"settings": {
"color": {
"palette": [
{
"slug": "primary",
"color": "#111",
"name": "Primary"
},
{
"slug": "secondary",
"color": "#fefefe",
"name": "Secondary"
}
]
}
}
}
Add a few more colors that you can use in the design of the theme:
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version":2,
"settings": {
"color": {
"palette": [
{
"slug": "primary",
"color": "#111",
"name": "Primary"
},
{
"slug": "secondary",
"color": "#fefefe",
"name": "Secondary"
},
{
"slug": "tertiary",
"color": "#383838",
"name": "Tertiary"
},
{
"slug": "quaternary",
"color": "#00838f",
"name": "Quaternary"
},
{
"slug": "quinary",
"color": "#4fb3bf",
"name": "Quinary"
}
]
}
}
}
After the palette, inside colors, add a gradient using the custom color presets you just created.
The presets for quinary -light cyan and secondary -off white, are converted into these CSS custom properties:
--wp--preset--color--secondary
--wp--preset--color--quinary
Use the CSS custom properties to define the gradients:
"gradients": [
{
"slug": "quinary-to-secondary",
"gradient": "linear-gradient(135deg, var(--wp--preset--color--quinary), var(--wp--preset--color--secondary))",
"name": "Quinary to secondary"
}
]
This setting is the equivalent of the editor-gradient-presets
theme support.
Typography
The typography sub-section in theme.json covers font families, sizes, and line height.
Add typography
after color
(the code is truncated):
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version":2,
"settings": {
"color": { ... },
"typography": {
}
}
}
Font families
I am adding three sets of font families with this code example: system fonts, sans-serif, and serif.
The Name
property is the visible name in the editor. If the name
is not included, the fontFamily
value will be visible instead.
"typography": {
"fontFamilies": [
{
"fontFamily": "-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,Oxygen-Sans,Ubuntu,Cantarell,\"Helvetica Neue\",sans-serif",
"slug": "system-fonts",
"name": "System Fonts"
},
{
"fontFamily": "Geneva, Tahoma, Verdana, sans-serif",
"slug": "geneva-verdana"
},
{
"fontFamily": "Cambria, Georgia, serif",
"slug": "cambria-georgia"
}
]
}
Learn more in the lesson dedicated to font family and size.
Font sizes
This setting is the equivalent of the editor-font-sizes
theme support.
- The
slug
is used in the generated CSS class name. - The
size
property can be any CSS that is valid for font sizes, using for example em, rem or px units. - The
name
property is the visible name in the editor.
Adjust the font sizes that you would like to use in your theme. Basic example:
"fontSizes": [
{
"slug": "extra-small",
"size": "1rem",
"name": "Extra small"
},
{
"slug": "small",
"size": "1.125rem",
"name": "Small"
},
{
"slug": "medium",
"size": "1.25rem",
"name": "Medium"
},
{
"slug": "large",
"size": "1.5rem",
"name": "Large"
},
{
"slug": "extra-large",
"size": "2.5rem",
"name": "Extra large"
}
]
Line height
The global styles equivalent of add_theme_support( 'custom-line-height'
);
.
You can enable custom line heights by adding:
"lineHeight": true
– It is not possible to add a selection of line heights to choose from.
Padding & Margin
Inside settings, enable support for custom padding and margin. The units are optional, you can include them all or only select a few.
"spacing": {
"padding": true,
"margin": true,
"units": [ "px", "em", "rem", "vh", "vw", "%" ]
}
Layout
The layout is one of the most important settings in the global styles. In this setting, developers define the default width of the blocks and the width for wide blocks.
"layout": {
"contentSize": "840px",
"wideSize": "1100px"
}
View the complete code for the settings section
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 2,
"settings": {
"color": {
"palette": [
{
"slug": "primary",
"color": "#111",
"name": "Primary"
},
{
"slug": "secondary",
"color": "#fefefe",
"name": "Secondary"
},
{
"slug": "tertiary",
"color": "#383838",
"name": "Tertiary"
},
{
"slug": "quaternary",
"color": "#00838f",
"name": "Quaternary"
},
{
"slug": "quinary",
"color": "#4fb3bf",
"name": "Quinary"
}
],
"gradients": [
{
"slug": "quinary-to-secondary",
"gradient": "linear-gradient(135deg, var(--wp--preset--color--quinary), var(--wp--preset--color--secondary))",
"name": "Quinary to secondary"
}
]
},
"typography": {
"lineHeight": true,
"fontFamilies": [
{
"fontFamily": "-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,Oxygen-Sans,Ubuntu,Cantarell,\"Helvetica Neue\",sans-serif",
"slug": "system-fonts",
"name": "System Fonts"
},
{
"fontFamily": "Geneva, Tahoma, Verdana, sans-serif",
"slug": "geneva-verdana"
},
{
"fontFamily": "Cambria, Georgia, serif",
"slug": "cambria-georgia"
}
],
"fontSizes": [
{
"slug": "extra-small",
"size": "1rem",
"name": "Extra small"
},
{
"slug": "small",
"size": "1.125rem",
"name": "Small"
},
{
"slug": "medium",
"size": "1.25rem",
"name": "Medium"
},
{
"slug": "large",
"size": "1.5rem",
"name": "Large"
},
{
"slug": "extra-large",
"size": "2.5rem",
"name": "Extra large"
}
]
},
"spacing": {
"padding": true,
"margin": true,
"units": [ "px", "em", "rem", "vh", "vw", "%" ]
},
"layout": {
"contentSize": "840px",
"wideSize": "1100px"
}
}
}
Styling the website
Now that you have created some theme.json presets, you can use them to add styles to the website. At the top level, below settings
, add the styles
section.
The presets are defined in the settings, but the code in the example is truncated:
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 2,
"settings": { ... },
"styles": {
}
}
Set the body background color to a pleasant dark grey using the preset--wp--preset--color--primary
.
Set the text color to offwhite:
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 2,
"settings": { ... },
"styles": {
"color": {
"background": "var(--wp--preset--color--primary)",
"text": "var(--wp--preset--color--secondary)",
}
}
}
To change the body link color, you need to use elements.
Below color, add elements
and then select which element to target, in this case, link
:
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 2,
"settings": { ... },
"styles": {
"color": {
"background": "var(--wp--preset--color--primary)",
"text": "var(--wp--preset--color--secondary)",
},
"elements": {
"link": {}
}
}
}
You can add both typography settings and colors inside elements.link
, but I am going to show an example of how to add a text color:
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 2,
"settings": { ... },
"styles": {
"color": {
"background": "var(--wp--preset--color--dark-grey)",
"text": "var(--wp--preset--color--white)"
},
"elements": {
"link": {
"color": {
"text": "var(--wp--preset--color-quinary)",
}
}
}
}
}
For the fonts, I am selecting a default font size of 20px or 1.25 rem, which has the preset slug of “medium”, and the system fonts:
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 2,
"settings": { ... },
"styles": {
"color": { ... },
"typography": {
"fontSize": "var(--wp--preset--font-size--medium)",
"fontFamily": "var(--wp--preset--font-family--system-fonts)",
"lineHeight": "1.7"
}
}
}
And the resulting CSS output is:
body {
--wp--style--color--link: var(--wp--preset--color--quinary);
background-color: var(--wp--preset--color--primary);
color: var(--wp--preset--color--secondary);
font-family: var(--wp--preset--font-family--system-fonts);
font-size: var(--wp--preset--font-size--medium);
line-height: 1.7;
}
Assign content to the template part areas
Finally, add a new section called templateParts
at the bottom of theme.json.
The theme that we created in the previous lesson has two template parts: header and footer.
Assign the header.html template part file to the header area and footer.html to the footer area:
"templateParts": [
{
"name": "header",
"area": "header",
"title": "Header"
},
{
"name": "footer",
"area": "footer",
"title": "Footer"
}
]
Preview
With the styles added, preview the theme on the front to see the result.
