简体   繁体   English

CSS 属性值来自 class name

[英]CSS property value from class name

It is possible to pass parameters for CSS in class name? class name 可以给CSS传参数吗? For example:例如:

.mrg-t-X {
   margin-top: Xpx;
}
<span class="mrg-t-10">Test</span>

In this example X should be 10.在此示例中,X 应为 10。

No it isn't.不,不是。 The closest we have to this is the attr() function , but that only works within the content property:我们最接近的是attr()函数,但它只适用于content属性:

 figure::before { content: attr(data-before) ', '; } figure::after { content: attr(data-after) '!'; }
 <figure data-before="Hello" data-after="world"></figure>

Perhaps one day this will be expanded so that we can use it elsewhere, but for now this isn't possible.也许有一天它会被扩展,以便我们可以在其他地方使用它,但目前这是不可能的。

Currently as I'm sure you're aware if you want to be able to use the .mrg-tX class, you'll need to define separate style rules for each X you wish to allow:目前,我确定您知道如果您希望能够使用.mrg-tX类,您需要为您希望允许的每个X定义单独的样式规则:

.mrg-t-1 { ... }
.mrg-t-2 { ... }
.mrg-t-3 { ... }
...

Nowdays you can use CSS variable inside a style attribute instead generating a specific class:现在,您可以在样式属性中使用CSS 变量,而不是生成特定的类:

Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific values to be reused throughout a document.自定义属性(有时称为 CSS 变量或级联变量)是由 CSS 作者定义的实体,其中包含要在整个文档中重复使用的特定值。 They are set using custom property notation (eg, --main-color: black; ) and are accessed using the var() function (eg, color: var(--main-color); ).它们使用自定义属性符号(例如, --main-color: black; )设置并使用var()函数访问(例如, color: var(--main-color); )。

Complex websites have very large amounts of CSS, often with a lot of repeated values.复杂的网站有大量的 CSS,通常有很多重复的值。 For example, the same color might be used in hundreds of different places, requiring global search and replace if that color needs to change.例如,可能在数百个不同的地方使用相同的颜色,如果该颜色需要更改,则需要进行全局搜索和替换。 Custom properties allow a value to be stored in one place , then referenced in multiple other places.自定义属性允许将值存储在一个地方,然后在其他多个地方引用。 An additional benefit is semantic identifiers.另一个好处是语义标识符。 For example, --main-text-color is easier to understand than #00ff00 , especially if this same color is also used in other contexts.例如, --main-text-color#00ff00更容易理解,特别是如果在其他上下文中也使用相同的颜色。

Custom properties are subject to the cascade and inherit their value from their parent.自定义属性受级联影响并从其父级继承其值。

example例子

 span { display: block; margin-top: var(--mt); } html { background: repeating-linear-gradient(to bottom, transparent, 10px, lightgrey 10px, lightgrey 20px);} /* see 10px steps */
 <span style="--mt:50px">one</span> <span style="--mt:85px">two</span> <span style="--mt:110px;">three</span>

Maybe you are looking for SCSS or LESS.也许您正在寻找 SCSS 或 LESS。 It have mixins, variables, etc, and it autocompile real and long css.它有 mixins、变量等,它会自动编译真正的和长的 css。 It was maded to this purposes and write less and less css with the same result.它是为此目的而编写的,并且编写的 css 越来越少,结果相同。

http://sass-lang.com/guide http://sass-lang.com/guide

http://lesscss.org/ http://lesscss.org/

 @size: 10px;
 .class { font-size: @size;  }

Good luck!祝你好运!

不,您的代码是错误的,但您可以在标签内编写 css

*<span style="margin-top:Xpx;">*

It isn't possible to directly pass parameters using just CSS but you're not the first person to ask - check out this question which looks at CSS and JavaScript options and also this might be helpful regarding attribute selection.仅使用 CSS 无法直接传递参数,但您不是第一个提出问题的人 - 查看此问题该问题查看 CSS 和 JavaScript 选项,也可能对属性选择有所帮助。

This will only help if you are looking at a few variables of margin-top but I don't know what context you're using this in. Depending on what you're using it for there might be better ways.这只会在您查看margin-top的几个变量时有所帮助,但我不知道您在什么上下文中使用它。根据您使用它的目的,可能有更好的方法。

The simplest way would probably be just to add the style inline to your span <span style="margin-top:10px"> but I try to stay away from inline CSS!最简单的方法可能只是将样式内联添加到您的 span <span style="margin-top:10px">但我尽量远离内联 CSS!

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

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