Home > OS >  Apply element selector to class
Apply element selector to class

Time:09-02

I am trying to style an element that has a class exactly like a h3 element. Is there anything like an @apply directive in CSS? Because I don't want to add the class to the selector of h3, because the CSS is only conditionally loaded.

This is what I imagine:

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

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

Is that possible?

CodePudding user response:

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.

CodePudding user response:

As you were told below, this is not possible, but if the H3 class name is "class1", you can do the following:

.class1 {
padding: 3px;
color: red;
}

.class1.example {
padding: 8px;
color: green;
}
<h3 >EXAMPLE</h3>
<p >EXAMPLE 2</p>

  • Related