备忘录吕吕没有备忘录十新建_一份备忘单,可帮助您记住CSS自定义属性

news/2024/7/3 1:53:38

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

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 dat.gui controller library 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.

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

Thanks for reading!

谢谢阅读!

翻译自: https://www.freecodecamp.org/news/css-customs-properties-cheatsheet-c86778541f7d/

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


http://lihuaxi.xjx100.cn/news/238361.html

相关文章

透明度和隐私:通过区块链赋予人们权力

想知道更多区块链技术知识,请百度【链客区块链技术问答社区】 链客,有问必答!区块链首次以比特币等加密货币的形式应用,已经证明了它对金融界的巨大影响。也许用不了多久,它的影响就会遍及全球。 区块链是一个安全的数…

Spark集群启动时worker节点启不起来

在spark集群中使用命令: sbin/start-all.sh 启动集群时报错: starting org.apache.spark.deploy.master.Master, logging to /home/yxk/cluster/spark/logs/spark-yxk-org.apache.spark.deploy.master.Master-1-linux.out yxklinuxs password: linux:…

Gulp快速入门教程

Gulp是基于流的前端自动化的构建工具,虽说如今是webpack盛行的时代,但是gulp和webpack整合效果更美味的,鱼与熊掌都可兼得哦!本文只介绍下Gulp的基本使用和一些常用的Gulp插件,废话不多说,一起来看看吧。 g…

ssh框架实现数据库_自顶向下介绍SSH及其如何实现安全的数据共享

ssh框架实现数据库by Sam Ollason通过萨姆奥拉森(Sam Ollason) This article will take a high-level and top-down approach to explain how SSH works and how it is used for securely communicating with remote computers.本文将采用一种自上而下的高级方法来解释SSH的工…

并非所有区块链都生来平等:找到正确的共识算法

现在知道更多区块链技术,请百度【链客区块链技术问答社区】 链客,有问必答!!关于共识算法的信息很难找到,即使它们构成了区块链技术的主干。这些算法对于确保分布式分类账平稳运行至关重要,没有它们&#x…

微服务项目的整合与测试

实验目的 掌握微服务项目的整合使用 掌握Swagger-UI的简单使用 练习内容 1、微服务项目整合 1.1、项目预览 1.1.1、在 https://github.com/shi469391tou/microservice-mallmanagement.git 地址下载,并导入Myeclipse中; 1.1.2、查看项目的结构 1.2、…

单点登录与权限管理本质:session和cookie介绍

本篇开始写「单点登录与权限管理」系列的第一部分:单点登录与权限管理本质,这部分主要介绍相关的知识概念、抽象的处理过程、常见的实现框架。通过这部分的介绍,能够对单点登录与权限管理有整体上的了解,对其相关概念、处理流程、…

如何在区块链中创建块

想知道更多关于区块链技术知识,请百度【链客区块链技术问答社区】 链客,有问必答!!这里我们讨论的是区块链。区块链上所定义的协议不仅是要交易数据而且还要交易数据的价值。目前的主要方式是在互联网出现之前发明的,它…