CSS自定义属性(CSS变量)
来源:互联网 发布:桌面快速启动软件 编辑:程序博客网 时间:2024/06/05 05:06
除了sass,less等预编译器外,css自身也支持定义变量
html
<section> <i></i> <div> test test test test test test test test test test test test test test test test test test test test test </div></section><div id="test"> <div id="test__title">font-size:28px</div></div>
css
/*全局变量*/:root { --bgcolor: yellow; --sec-color:blue; --font-size:22px; --quanjv:1; /*[JS] getComputedStyle(document.body).getPropertyValue('--quanjv')//'1' */}section { background: var(--bgcolor);/*取--bgcolor的值*/ color:var(--sec-color,blue);/*取--sec-color的值,如果没有,则使用默认值(blue)*/ --multy:20; width: calc(var(--font-size) * var(--multy)*1.1);/*多个变量计算*/}section:after{ content: ''; display: block; clear: both;}section i { width: 100px; height: 100px; background: currentColor;/*使用默认颜色,取color的值,没定义取父元素的color*/ display: inline-block; float: left; border-radius: 50%; shape-outside: circle(); margin: 10px;}/*局部变量*/section div { word-break: break-all; font-size:var(--font-size,14px);/*找不到该变量,不在变量的作用域,使用默认值14px*/}#test__title{ font-size:var(--font-size,10px);/*使用局部变量,在下方*/}#test{ --font-size:28px;/*定义局部变量*/ --jubu:2; /*[JS] getComputedStyle(document.body).getPropertyValue('--quanjv') = '' ,无法获取; getComputedStyle(document.getElementById('test')).getPropertyValue('--jubu') = "2" */}@supports(--a:0){ /*支持*/}@supports(not(--a:0)){ /*不支持*/}
还可以JS操作
//改变变量document.documentElement.style.setProperty('--sec-color','red');//JS操作//获得变量getComputedStyle(document.body).getPropertyValue('--quanjv');//1//获取支持性if(window.CSS&&window.CSS.supports&&window.CSS.supports('--a',0)){ console.log('support') }else{ console.error('not support') }
0 0
- CSS自定义属性(CSS变量)
- CSS-颜色属性+颜色函数+自定义变量
- CSS自定义属性Expression
- 学习CSS自定义属性
- css 自定义属性(@apply var)
- CSS变量(css variable)
- 自定义checkbox大小(注:用CSS的ZOOM属性 )
- 自定义checkbox大小(注:用CSS的ZOOM属性 )
- css属性~(积少成多)
- css(float属性)
- css (overflow属性)
- 有关Css自定义变量的介绍
- 【css属性之cursor】 光标自定义
- CSS 自定义属性 — 基础篇
- CSS 变量
- css 变量
- CSS变量
- CSS学习笔记-CSS属性(五)
- linux常用命令之包含字符串的杀死
- CEF会遇到的问题
- Nginx 出现 413 Request Entity Too Large错误解决方法
- 小萌新jason的第一篇博客
- 知识库--The Connector Interface
- CSS自定义属性(CSS变量)
- 负载能力——影响负载能力思维导图整理
- FZU 1055 赋值问题【】
- springmvc的拦截器配置
- HDU 1847 Good Luck in CET-4 Everybody!(sg入门题)
- hdoj 1851 A Simple Game(组合博弈, sg函数)
- uCOS-II任务管理之改变任务优先级 [转载]
- ajax的简单应用
- MakeDown最基本用法,亲测有效