💡 云策文档标注

概述

本文档是 theme.json 中可用的样式属性参考,涵盖边框、颜色、尺寸、滤镜、阴影、间距、排版和 CSS 等属性,用于全局、元素或块级别的样式定义。

关键要点

  • 边框样式支持两种方法:统一设置所有边或针对特定边(如 top、right)分别设置颜色、样式和宽度。
  • 颜色属性包括文本颜色、背景颜色和链接颜色,可应用于块或元素。
  • 尺寸属性主要用于定义最小高度,如 minHeight。
  • 滤镜属性当前支持设置默认双色调滤镜(duotone)。
  • 阴影属性用于定义盒阴影(box-shadow)。
  • 间距属性包括块间距(blockGap)、边距(margin)和内边距(padding),可针对各边单独设置。
  • 排版属性涵盖字体、大小、样式、字重、字母间距、行高、文本装饰等。
  • CSS 属性允许直接在 theme.json 中编写自定义 CSS,用于块或元素。

代码示例

{
  "version": 2,
  "styles": {
    "border": {
      "color": "#000000",
      "style": "solid",
      "width": "1px"
    }
  }
}

注意事项

  • 使用前请参考 Applying Styles 文档以了解如何应用样式到主题。
  • 属性类型包括字符串或对象,对应 CSS 属性。
  • 示例代码基于 theme.json 版本 2,确保版本兼容性。

📄 原文内容

This is a reference to the available style properties that you can apply to the root element (global), individual elements, and individual blocks in theme.json. Please review the Applying Styles documentation to learn how to apply styles to your theme.

Border

There are two methods for working with the border style property. The first is to target all sides of a block or element with the properties shown in the table:

Property Type CSS Property
border.radius string, object border-radius
border.color string, object border-color
border.style string, object border-style
border.width string, object border-width

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"border": {
			"color": "#000000",
			"style": "solid",
			"width": "1px"
		}
	}
}

The second method is to specifically target the top, right, bottom, and left sides:

Property Type CSS Property
border.<side>.color string, object border-<side>-color
border.<side>.style string, object border-<side>-style
border.<side>.width string, object border-<side>-width

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"border": {
			"top": {
				"color": "#000000",
				"style": "solid",
				"width": "1px"
			}
		}
	}
}

Color

The color style property lets you define the default text, background, and link colors for a block or element:

Property Type CSS Property
color.text string, object color
color.background-color string, object background-color
color.link string, object color (applied to nested <a> elements)

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"blocks": {
			"core/group": {
				"color": {
					"text": "#000000",
					"background": "#ffffff",
					"link": "#777777"
				}
			}
		}
	}
}

Dimensions

The dimensions style property lets you define the minimum height for a block or element:

Property Type CSS Property
dimensions.minHeight string, object min-height

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"blocks": {
			"core/cover": {
				"dimensions": {
					"minHeight": "50vh"
				}
			}
		}
	}
}

Filter

The filter style property lets you define filters for a block or element. Currently, you can set a default duotone filter:

Property Type CSS Property
filter.duotone string, object filter

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"blocks": {
			"core/image": {
				"filter": {
					"duotone": "var(--wp--preset--duotone--default-filter)"
				}
			}
		}
	}
}

Shadow

The shadow style property lets you define the default box-shadow style for a block or element:

Property Type CSS Property
shadow string, object box-shadow

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"blocks": {
			"core/heading": {
				"shadow": "0 1px 2px 0 rgb(0 0 0 / 0.05)"
			}
		}
	}
}

Spacing

The spacing style property lets you define the default gap, margin, and padding for a block or element:

Property Type CSS Property
blockGap string, object margin-top, gap
margin.<side> string, object margin-<side>
padding.<side> string, object padding-<side>

You can define any or all of the sides (top, right, bottom, left) for the margin and padding style properties.

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"spacing": {
			"blockGap": "2rem",
			"margin": {
				"top": "2rem",
				"bottom": "2rem"
			},
			"padding": {
				"left": "2rem",
				"right": "2rem"
			}
		}
	}
}

Typography

The typography style property lets you define default font and text-related styles for a block or element:

Property Type CSS Property
fontFamily string, object font-family
fontSize string, object font-size
fontStyle string, object font-style
fontWeight string, object font-weight
letterSpacing string, object letter-spacing
lineHeight string, object line-height
textColumns string columns
textDecoration string, object text-decoration
writingMode string, object writing-mode

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"blocks": {
			"core/paragraph": {
				"typography": {
					"fontFamily": "Georgia, serif",
					"fontSize": "1.25rem",
					"fontStyle": "normal",
					"fontWeight": "500",
					"letterSpacing": "0",
					"lineHeight": "1.6",
					"textDecoration": "none"
				}
			}
		}
	}
}

CSS

The css property lets you write custom CSS directly in theme.json for a block or element:

Property Type CSS Property
css string

Example usage in theme.json:

{
	"version": 2,
	"styles": {
		"blocks": {
			"core/gallery": {
				"css": "--wp--style--gallery-gap-default: 1rem;"
			}
		}
	}
}

For an in-depth look at how to use the css style property, read Per-block CSS with theme.json on the WordPress Developer Blog.