此处列出了可在 theme.json 中应用于根元素(全局)、单个元素以及各个区块的可用样式属性参考。请查阅 应用样式 文档,了解如何为您的主题应用样式。
边框
有两种方法可用于设置 border 样式属性。第一种方法是使用表格中所示的属性来设置区块或元素的所有边缘的样式:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
border.radius | 字符串,对象 | border-radius |
border.color | 字符串,对象 | border-color |
border.style | 字符串,对象 | border-style |
border.width | 字符串,对象 | border-width |
在 theme.json 中的示例用法:
{
"version": 2,
"styles": {
"border": {
"color": "#000000",
"style": "solid",
"width": "1px"
}
}
}第二种方法是分别设置 top、right、bottom 和 left 四个方向的边框样式:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
border.<side>.color | 字符串,对象 | border-<side>-color |
border.<side>.style | 字符串,对象 | border-<side>-style |
border.<side>.width | 字符串,对象 | border-<side>-width |
在 theme.json 中的示例用法:
{
"version": 2,
"styles": {
"border": {
"top": {
"color": "#000000",
"style": "solid",
"width": "1px"
}
}
}
}颜色
color 样式属性可用于为区块或元素定义默认的文本色、背景色以及链接色:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
color.text | 字符串,对象 | color |
color.background-color | 字符串,对象 | background-color |
color.link | 字符串,对象 | color(应用于嵌套的 <a> 元素) |
在 theme.json 中的示例用法:
{
"version": 2,
"styles": {
"blocks": {
"core/group": {
"color": {
"text": "#000000",
"background": "#ffffff",
"link": "#777777"
}
}
}
}
}尺寸
dimensions 样式属性可用于为区块或元素定义最小高度:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
dimensions.minHeight | 字符串,对象 | min-height |
在 theme.json 中的示例用法:
{
"version": 2,
"styles": {
"blocks": {
"core/cover": {
"dimensions": {
"minHeight": "50vh"
}
}
}
}
}滤镜
filter 样式属性可用于为区块或元素定义滤镜效果。目前,您可以设置默认的双色调滤镜:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
filter.duotone | 字符串,对象 | filter |
在 theme.json 中的示例用法:
{
"version": 2,
"styles": {
"blocks": {
"core/image": {
"filter": {
"duotone": "var(--wp--preset--duotone--default-filter)"
}
}
}
}
}阴影
shadow 样式属性可用于为区块或元素定义默认的盒阴影样式:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
shadow | 字符串,对象 | box-shadow |
在 theme.json 中的示例用法:
{
"version": 2,
"styles": {
"blocks": {
"core/heading": {
"shadow": "0 1px 2px 0 rgb(0 0 0 / 0.05)"
}
}
}
}间距
spacing 样式属性可用于为区块或元素定义默认的间隙、外边距以及内边距:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
blockGap | 字符串,对象 | margin-top、gap |
margin.<side> | 字符串,对象 | margin-<side> |
padding.<side> | 字符串,对象 | padding-<side> |
对于 margin 和 padding 样式属性,您可以分别为 top、right、bottom、left 这四个方向设置样式。
在 theme.json 中的示例用法:
{
"version": 2,
"styles": {
"spacing": {
"blockGap": "2rem",
"margin": {
"top": "2rem",
"bottom": "2rem"
},
"padding": {
"left": "2rem",
"right": "2rem"
}
}
}
}排版
typography 样式属性可用于为区块或元素定义默认的字体以及与文本相关的样式:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
fontFamily | 字符串,对象 | font-family |
fontSize | 字符串,对象 | font-size |
fontStyle | 字符串,对象 | font-style |
fontWeight | 字符串,对象 | font-weight |
letterSpacing | 字符串,对象 | letter-spacing |
lineHeight | 字符串,对象 | line-height |
textColumns | 字符串 | columns |
textDecoration | 字符串,对象 | text-decoration |
writingMode | 字符串,对象 | writing-mode |
在 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
css 属性允许您在 theme.json 中直接为区块或元素编写自定义 CSS 代码:
| 属性 | 类型 | CSS 属性 |
|---|---|---|
css | 字符串 | — |
在 theme.json 中的示例用法:
{
"version": 2,
"styles": {
"blocks": {
"core/gallery": {
"css": "--wp--style--gallery-gap-default: 1rem;"
}
}
}
}如需深入了解如何使用 css 样式属性,请阅读 WordPress 开发者博客上的文章 使用 theme.json 实现区块级 CSS。