简体   繁体   English

将元素选择器应用于 class

[英]Apply element selector to class

I am trying to style an element that has a class exactly like a h3 element.我正在尝试对具有 class 的元素进行样式设置,就像 h3 元素一样。 Is there anything like an @apply directive in CSS? CSS 中是否有类似@apply指令的内容? Because I don't want to add the class to the selector of h3, because the CSS is only conditionally loaded.因为我不想将 class 添加到 h3 的选择器中,因为 CSS 只是有条件地加载。

This is what I imagine:这是我想象的:

h1 {
    font-size: 30px;
    /* ... */
}

/* somewhere else */
.my-class {
    @apply h1;
}

Is that possible?那可能吗?

No, that is not possible.不,那是不可能的。 As you suggest only a selector list, as in正如您仅建议一个选择器列表,如

h3, .my-class {
  /* ... */
}

could be use.可以使用。

A preprocessor (such as SASS or LESS) could be used, but those will only result in the same syntax in the end.可以使用预处理器(例如 SASS 或 LESS),但最终只会产生相同的语法。

As you were told below, this is not possible, but if the H3 class name is "class1", you can do the following:如下所述,这是不可能的,但如果 H3 class 名称为“class1”,您可以执行以下操作:

 .class1 { padding: 3px; color: red; }.class1.example { padding: 8px; color: green; }
 <h3 class="class1">EXAMPLE</h3> <p class="class1 example">EXAMPLE 2</p>

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

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