博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
备忘录吕吕没有备忘录十新建_一份备忘单,可帮助您记住CSS自定义属性
阅读量:2526 次
发布时间:2019-05-11

本文共 2216 字,大约阅读时间需要 7 分钟。

备忘录吕吕没有备忘录十新建

CSS custom properties, also known as CSS variables, represent custom properties that can be declared and be called in your CSS.

CSS定制属性,也称为CSS变量,表示可以在CSS中声明和调用的定制属性。

在CSS中声明自定义属性 (Declare a custom property in CSS)

To declare a Custom property in your CSS, you need to use the -- syntax:

要在CSS中声明Custom属性,您需要使用--语法:

:root { --colorPrimary: hsla(360, 100%, 74%, 0.6); }

Notice the :root pseudo-class selector — we can declare our variables globally using it. We can also declare them using other selectors, and they will then be scoped in those.

注意:root伪类选择器-我们可以使用它全局地声明变量。 我们还可以使用其他选择器声明它们,然后将它们限定在这些选择器中。

.theme-dark { --colorPrimary: hsla(360, 100%, 24%, 0.6); }

在CSS中使用自定义属性 (Use a custom property in CSS)

To use a CSS custom property in your CSS, you can use the var() function:

要在CSS中使用CSS自定义属性,可以使用var()函数:

:root { --colorPrimary: tomato; } .theme-dark { --colorPrimary: lime; } body { background-color: var(--colorPrimary); }

In this case, body will have a background colour of tomato, but a body.theme-dark of lime.

在这种情况下, body就会有一个背景颜色tomato ,但body.theme-darklime

使用无单位的自定义属性 (Use custom properties without units)

CSS custom properties can be declared without units if they are used with the calc() function.

如果将CSS自定义属性与calc()函数一起使用,则可以不使用单位声明它们。

:root { --spacing: 2; } .container {   padding: var(--spacing) px; /*Doesn't Work 😫*/   padding: calc(var(--spacing) * 1rem); /*Will output 2rem 😃*/ }

在JavaScript中使用自定义属性 (Use custom properties with JavaScript)

To get a custom property, we can use the following:

要获取自定义属性,我们可以使用以下代码:

getComputedStyle(element).getPropertyValue("--my-var"); // Or if inline element.style.getPropertyValue("--my-var");

To update the custom property value:

要更新定制属性值:

element.style.setProperty("--my-var", newVal);

Example of getting and replacing values:

获取和替换值的示例:

In the following example, we use the to change the value of --scenePerspective, --cubeRotateY, and --cubeRotateX custom properties. This method makes it easier to apply a new style, as you do not have to apply inline style on each DOM element.

在以下示例中,我们使用来更改--scenePerspective,--cubeRotateY和--cubeRotateX自定义属性的值。 此方法使您更容易应用新样式,因为您不必在每个DOM元素上应用内联样式。

Thanks for reading!

谢谢阅读!

翻译自:

备忘录吕吕没有备忘录十新建

转载地址:http://epuzd.baihongyu.com/

你可能感兴趣的文章
python学习笔记-day10-01-【 类的扩展: 重写父类,新式类与经典的区别】
查看>>
查看端口被占用情况
查看>>
浅谈css(块级元素、行级元素、盒子模型)
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
PHP开源搜索引擎
查看>>
12-FileZilla-响应:550 Permission denied
查看>>
ASP.NET MVC 3 扩展生成 HTML 的 Input 元素
查看>>
LeetCode 234. Palindrome Linked List
查看>>
编译HBase1.0.0-cdh5.4.2版本
查看>>
结构体指针
查看>>
迭代器
查看>>
Food HDU - 4292 (结点容量 拆点) Dinic
查看>>
Ubuntu安装Sun JDK及如何设置默认java JDK
查看>>
[经典算法] 排列组合-N元素集合的M元素子集
查看>>
Codeforces 279D The Minimum Number of Variables 状压dp
查看>>
打分排序系统漫谈2 - 点赞量?点赞率?! 置信区间!
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>