框架CSS变量(CSS Variables)
最后更新: | 浏览次数: 1033|前端开发基础
响应式变量系统
与 ZMCMS 4.8.0601 版本同步发布(2026/6/19更新)。
响应式变量系统基于 CSS Custom Properties(CSS 自定义属性)实现,通过在不同断点下自动切换变量值,减少重复的媒体查询声明,使样式开发更加统一、简洁且易于维护。
在文本、间距、尺寸等大多数场景中,仅需定义响应式变量即可完成适配,无需重复编写相同的选择器和样式规则,从而有效降低代码量并提升后期维护效率
破坏性变更
4.8.0601 版本对部分变量名称进行了统一调整。
升级旧版本项目时,请检查并替换以下变量名称:
| 旧变量 | 新变量 |
|---|---|
| --title-font-family | --heading-font-family |
| --title-font-size | --heading-font-size |
H1-H6 全局响应式变量
以下变量用于控制 H1-H6 标题元素的默认样式。
这些变量属于全局变量,无需添加任何 Class,设置后将自动应用于所有标题元素。
| 变量名称 | 默认值 | 说明 |
|---|---|---|
| --heading-font-family | inherit | 字体名称 |
| --heading-size | inherit | 字体大小 |
| --heading-weight | 500 | 字体粗细 |
| --heading-color | inherit | 字体颜色 |
| --heading-line-height | 1.45 | 行高 |
| --heading-margin | 0 0 0.5rem | 外边距 |
.ui-vars 响应式变量
.ui-vars 用于控制容器内正文内容的显示样式,包括字体、颜色、背景、行高等属性。
只有在当前元素或其父级元素添加 .ui-vars 类后,以下变量才会生效。
<div class="ui-vars">
...
</div>
body 标签,系统组件默认已启用 .ui-vars 功能,因此页面和系统组件正文可直接使用以下变量。
| 变量名称 | 默认值 | 说明 |
|---|---|---|
| --body-font-family | inherit | 字体名称 |
| --body-size | inherit | 字体大小 |
| --body-weight | 400 | 字体粗细 |
| --body-color | inherit | 字体颜色 |
| --body-line-height | 1.45 | 行高 |
| --body-margin | 0 0 0.5rem | 外边距 |
响应式断点变量
除字体名称变量外,所有响应式变量均支持断点后缀。
| 后缀 | 断点 |
|---|---|
| -md | min-width: 776px |
| -lg | min-width: 992px |
| -xl | min-width: 1200px |
例如:
.content-main{
--body-size:14px;
--body-size-md:15px;
--body-size-lg:16px;
--body-size-xl:18px;
}
系统将在对应断点下自动切换变量值。
使用示例
HTML
<div class="ui-vars content-main">
<h1>Heading 1</h1>
<p>This is content.</p>
<h2>Heading 2</h2>
<p>This is content 2.</p>
</div>
CSS
.content-main{
--body-color:#333;
--body-size:15px;
/* ≥992px */
--body-size-lg:16px;
--heading-size:24px;
/* ≥992px */
--heading-size-lg:28px;
--heading-weight:600;
--heading-color:#111;
}
效果
- 默认正文字体:15px
- 992px 以上正文字体:16px
- 默认标题字体:24px
- 992px 以上标题字体:28px
工作原理
响应式变量系统会在不同断点下自动映射当前变量值。
例如:
--body-size
--body-size-md
--body-size-lg
--body-size-xl
最终统一映射为:
--body-size-current
组件内部只需读取:
font-size:var(--body-size-current);
即可自动获得当前断点对应的值。
这种方式可以避免在多个断点中重复编写如下代码:
@media(min-width:992px){
.content-main p{
font-size:16px;
}
}
从而提升样式复用性和维护效率。
全局变量
| 变量名称 | 默认值 | 说明 |
|---|---|---|
| --body-bg | transparent | 页面背景颜色 |
| --link-color | #444 | 超链接颜色 |
| --link-hover-color | #0a58ca | 超链接激活时颜色 |
| --input-checked-color | #0d6efd | chcekcbox,radio选中时颜色 |
| --menu-level-ident | 16px | 菜单每层缩进值 |
容器变量
| 变量名称 | 默认值 | 说明 |
|---|---|---|
| --max-width | 1920px | 页面最大显示宽度 |
| --container-width | 1450px | .container-xl 最大宽度 |
| --gutter-x | 1.5rem | 列之间的水平间距 |
| --gutter-y | 0 | 列之间的垂直间距 |
说明
--max-width用于限制页面在超大屏幕下的最大显示宽度。 当浏览器宽度超过该值时,页面内容将保持居中显示,不再继续拉伸。.container-fluid同样遵循此规则。--container-width用于定义.container-xl的最大显示宽度。 与传统栅格系统不同,.container-xl不再依赖断点切换宽度, 而是统一使用该变量控制最大宽度。--gutter-x和--gutter-y用于控制栅格列之间的间距, 所有基于 Row / Col 的布局组件均会自动继承该设置。
示例
:root{
--max-width:1920px;
--container-width:1450px;
--gutter-x:2rem;
--gutter-y:1rem;
}
上述配置表示:
- 页面最大显示宽度为 1920px
- .container-xl 最大宽度为 1450px
- 列水平间距为 2rem
- 列垂直间距为 1rem
Table变量
| 变量名 | 默认值 | 变量说明 |
|---|---|---|
| --table-font-size | var(--body-font-size, 1rem) | 表格字体大小 |
| --table-line-height | var(--body-line-height, 1.45) | 表格行高 |
| --table-bg | #fff | 表格整体背景颜色 |
| --table-head-bg | #f7f8fa | 表头背景颜色 |
| --table-striped-bg | inherit | 斑马纹行背景颜色,继承父元素背景 |
| --table-color | var(--body-color) | 表格正文文字颜色 |
| --table-head-color | #222 | 表头文字颜色 |
| --table-border-color | #e5e5e5 | 表格边框颜色 |
| --table-border-width | 1px | 表格边框宽度 |
| --table-border-style | solid | 表格边框样式 |
| --table-cell-padding-y | .5rem | 单元格上下内边距 |
| --table-cell-padding-x | .5rem | 单元格左右内边距 |
| --table-text-align | left | 表格内容水平对齐方式 |
| --table-head-align | left | 表头内容水平对齐方式 |
| --table-vertical-align | middle | 单元格垂直对齐方式 |
| --table-radius | 0 | 表格圆角大小 |
| --table-hover-bg | #f5f7fb | 鼠标悬停行背景颜色 |
| --table-min-width | 476px | 表格最小宽度,小于该宽度时建议横向滚动显示 |
表单相关
| 变量名称 | 默认值 | 说明 |
|---|---|---|
| --form-color | inherit | 表单文字颜色 |
| --form-font-size | inherit | 表单文字大小 |
| --form-border-color | #ced4da | 表单边框颜色 |
| --form-bg | #fff | 表单背景 |
--form-valid-border-color |
#198754 |
表单验证通过边框色 |
--form-invalid-border-color |
#dc3545 |
表单验证未通过边框色 |
| --form-label-color | inherit | label标签文字颜色 |
| --form-label-font-size | inherit | label标签文字大小 |
| --form-label-font-weight | 400 | label标签文字粗细 |
| --placeholder-color |
#6c757d
|
|
| --placeholder-font-size | 0.875rem | |
| --placeholder-font-weight | 400 |
面包屑(.breadcrumb)
| 变量名称 | 默认值 | 说明 |
|---|---|---|
| --breadcrumb-divider | / | 面包屑分隔符 |
| --breadcrumb-item-padding-x | 0.5rem | 面包屑间隔 |
购物车
| 变量名称 | 默认值 | 说明 |
| --cart-side-width | 450 | 购物车宽度 |
Widget TOC / Widget Collapse组件
| 变量名称 | 默认值 | 说明 |
| --white-space | normal | 内容是否换行 |
| --indent | 0.875rem | 层级缩进距离 |
| --link-color | inherit | 链接颜色 |
| --hover-color | inherit | 链接悬停颜色 |
| --hover-bg-color | #f8f8f8 | 链接悬停背景色 |
| --active-color | --main-color | 激活时的链接颜色 |
| --active-bg | #f8f8f8 | 激活时的链接背景颜色 |
| --drop-con | \f10b | 下拉按钮代码 |
其它变量
| 变量名称 | 默认值 | 说明 |
|---|---|---|
| --overlay-color | transparent | banner图遮罩颜色 |
| --border-radius | 0 | 用于button,select上的圆角 |
| --offcanvas-bg | #fff | offcanvas组件背景颜色 |
| --param-width | 150px | 产品参数名称宽度 |
| --param-padding-y | 8px | 产品参数垂直间距 |
| --param-padding-x | 8px | 产品参数水平间距 |
Swiper变量
| 变量名称 | 默认值 | 说明 |
|---|---|---|
| --swiper-navigation-sides-offset | swiper的left,right | |
| --swiper-button-prev-content | swiper左字体图标 | |
| --swiper-button-next-content | swiper右字体图标 | |
| --swiper-navigation-color | swiper字体图标颜色 | |
| --swiper-navigation-size | swiper字体图标大小 | |
| --swiper-pagination-bullet-inactive-color | swiper标识背景颜色 | |
| --swiper-theme-color | 当前swiper标识背景颜色 | |
| --swiper-pagination-bullet-horizontal-gap | swiper标识图标距离 | |
| --swiper-pagination-bullet-opacity | swiper标识图标透明度 | |
| --swiper-pagination-bullet-width | swiper标识图标宽度 | |
| --swiper-pagination-bullet-height | swiper标识图标宽度 | |
| --swiper-pagination-bullet-border-radius | swiper标识图标弧度 |