简体   繁体   English

取决于其类/ ID名称的动态CSS属性值

[英]Dynamic CSS property value that depends on its class/id name

I am wondering if there is a way to set a css property value dynamically that depends on its name. 我想知道是否有一种方法可以根据其名称动态设置css属性值。 Like for example: 例如:

.m-b-5 {
    margin-bottom: 5px;
}

Where 5px is referenced to its class/id name. 其中5px被引用为其类/ id名称。

With pure CSS no. 使用纯CSS号。 The only way with CSS is to create all the classes you'll use on the start: CSS的唯一方法是创建所有将在开始时使用的类:

.m-t-10{ margin-top: 10px }
.m-t-20{ margin-top: 20px }
.m-t-30{ margin-top: 30px }
/* etc margin */

.p-t-10{ padding-top: 10px }
.p-t-20{ padding-top: 20px }
.p-t-30{ padding-top: 30px }
/* etc padding */

Or you can use LESS/SCSS/some other CSS preprocessor to create a mixin/function which creates those classes. 或者,您可以使用LESS / SCSS /其他CSS预处理器来创建可创建这些类的mixin /函数。

Boostrap V4 included the same feature so check the source code to get the idea . Boostrap V4包含相同的功能,因此请检查源代码以了解其想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM