CSS var() 函数 自定义元素属性插入css样式中

 2022-05-29    507  

CSS var() 函数,自定义元素属性插入css样式中

实例

CSS var() 函数 自定义元素属性插入css样式中

首先定义一个名为 "--main-bg-color" 的自定义属性,然后使用 var() 函数在样式表中插入该自定义属性的值:

:root {
  --main-bg-color: coral; 
}
#div1 {
  background-color: var(--main-bg-color); 
}
#div2 {
  background-color: var(--main-bg-color);
}

定义和用法

var() 函数用于插入自定义属性的值,而不是另一个属性的值的任何部分。

CSS 语法

var(custom-property-name, value)

描述
custom-property-name 必需。自定义属性的名称(必须以两个破折号开头)。
value 可选。回退值(在自定义属性无效时使用)。


var() 函数

Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!

Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!

Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!

  •  标签:  
  • css
  •  

原文链接:https://77isp.com/post/1016.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。