- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 引入
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 定位
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 平移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z 索引
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪切
- CSS - 书写模式
- CSS - Unicode 双向
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - Justify Items
- CSS - Justify Self
- CSS - 制表符大小
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块级尺寸
- CSS - 最小块级尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移量
- CSS - 口音色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - 重要
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2D 变换
- CSS - 3D 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - 自定义属性
自定义属性是 CSS 中的变量,用于在样式表中存储和重用值。它们与其他编程语言中的变量相同。
在 CSS 中声明自定义属性
以下代码展示了如何在 CSS 中声明自定义属性。
:root {
--primary-color: #3498db;
--font-size-large: 1.5rem;
}
body{
background-color: var(--primary-color);
font-size: var(--font-size-large)
}
在 CSS 中使用自定义属性时,需要考虑以下几点。
- 您可以在样式表中的任何选择器中定义自定义属性,但如果您在**:root**选择器中定义自定义属性,则它将在整个样式表中具有作用域。
- var函数用于将变量值应用于任何元素上的 CSS 属性。
- CSS 不允许在媒体查询或容器查询中使用自定义属性,也不能使用它们来设置 CSS 属性或选择器的名称。
- 自定义属性区分大小写。
CSS 自定义属性示例
以下代码展示了在 CSS 中使用自定义属性的示例。在这里,您可以看到我们将颜色 '#0044cc' 定义为 blue,因此开发人员可以通过使用变量 blue 来重复使用此颜色。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
:root {
--main-bg-color: #f0f0f0;
--main-text-color: #333;
--primary-font: 'Arial', sans-serif;
--padding: 10px;
--blue: #0044cc;
--header-text: #fff;
--container-bg: #fff;
}
body {
background-color: var(--main-bg-color);
color: var(--main-text-color);
font-family: var(--primary-font);
padding: var(--padding);
}
header {
background-color: var(--blue);
color: var(--header-text);
padding: var(--padding);
}
.container {
background-color: var(--container-bg);
padding: var(--padding);
}
</style>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
<div class="container">
<p>
This is a container with a background color
defined using variables.
</p>
</div>
</body>
</html>
自定义属性回退值
我们可以为自定义属性定义回退值,以确保您的 CSS 代码仍然有效并在变量未定义的情况下也能工作。
回退值不用于使 CSS 自定义属性在旧版浏览器中工作。它们仅在支持 CSS 自定义属性的浏览器中用作备份,以防变量未定义或具有无效值。
示例
在下面的示例中,我们只为白色定义了颜色阴影,但也在使用黑色变量。由于我们为黑色变量定义了回退值,因此不会出现任何错误。
<!DOCTYPE html>
<html>
<head>
<style>
:root {
--white-color: #f0f0f0;/* Shade for white */
/* Custom Property for black is not defined */
}
.box {
text-align: center;
padding: 20px;
background-color: var(--white-color, white);
color: var(--black-color, black);
}
.box1, .box2 {
display: inline-block;
background-color: var(--black-color, black);
color: var(--white-color, white);
width: 100px;
height: 50px;
}
</style>
</head>
<body>
<div class="box">
<h2>Tutorialspoint</h2>
<p> How to code a website using HTML and CSS </p>
<div class="box1"> HTML </div>
<div class="box2"> CSS </div>
</div>
</body>
</html>
自定义属性的继承
在 CSS 中,自定义属性默认从父元素继承到子元素。在父元素上声明的任何变量都将可用于其所有后代,除非被覆盖。
.container {
--main-bg-color: black;
--text-color: white;
}
.child {
/* Use inherited value from parent for background color*/
background-color: var(--main-bg-color);
/* Overrides the parent’s value for text color*/
--text-color: lightgrey;
color: var(--main-bg-color);
}
示例
以下示例演示了使用 CSS 自定义属性以及继承。
<!DOCTYPE html>
<html>
<head>
<style>
:root {
--white-color: #f0f0f0;
--black-color: rgb(0, 0, 21);
}
.box {
--box-background: var(--white-color);
background-color: var(--box-background);
text-align: center;
padding: 20px;
}
.box1, .box2 {
display: inline-block;
background-color: var(--black-color);
/* Box Background is defined at parent box */
color: var(--box-background);
width: 100px;
height: 50px;
}
</style>
</head>
<body>
<div class="box">
<h2>Tutorialspoint</h2>
<p> How to code a website using HTML and CSS </p>
<div class="box1"> HTML </div>
<div class="box2"> CSS </div>
</div>
</body>
</html>
广告