
Thisparagraphisinitialblack.
:root{--text-color:16px;}p{颜色:蓝色;}p{颜色:var(--文本颜色);本例中p的颜色属性应用了var函数,但16px是无效的颜色属性值。所以找到了父元素,但是没有父元素,所以把这个颜色的值替换成默认的初始值。替换后,该属性恢复为默认属性。注意:虽然CSS属性/值对中的语法错误会导致该行被忽略。但是对于级联值,无效的替换(具有无效的自定义属性值)不会被忽略,从而导致值被继承。通过js获取的css变量值//从内联样式中获取变量element.style.getPropertyValue("--my-var");//从任意位置获取变量getComputedStyle(element).getPropertyValue("--my-var");//在内联样式中设置变量element.style.setProperty("--my-var",jsVar+4);var()和rgba()配合实现变量复用:root{--my-var:24,144,255;}.custom-bg-1{background-color:rgba(var(--my-var),0.5);}.custom-bg-2{background-color:rgba(var(--my-var),0.7);}vue实战:基于ColorPick通过颜色选择器切换热图的主题颜色iView组件,用户可以根据自己的喜好切换主题颜色。heat.scss:root{--heat-cell-bgc:24,144,255;}.custom-alpha-8-1{background-color:rgba(var(--heat-cell-bgc),0.125)!important;}.custom-alpha-8-2{background-color:rgba(var(--heat-cell-bgc),0.25)!important;}....custom-alpha-8-8{background-color:rgba(var(--heat-cell-bgc),1)!important;}colorFilter.vue