How can I apply styles to multiple classes at once?

CssCss Selectors

Css Problem Overview


I want two classes with different names to have the same property in CSS. I don't want to repeat the code.

.abc {
   margin-left:20px;
}  
.xyz {
   margin-left:20px;
}

<a class="abc">Lorem</a>
<a class="xyz">Ipsum</a>

Since both classes are doing the same thing, I should be able to merge it into one. How can I do that?

Css Solutions


Solution 1 - Css

.abc, .xyz { margin-left: 20px; }

is what you are looking for.

Solution 2 - Css

You can have multiple CSS declarations for the same properties by separating them with commas:

.abc, .xyz {
   margin-left: 20px;
}

Solution 3 - Css

Don’t Repeat Your CSS

 a.abc, a.xyz{
    margin-left:20px;
 }

OR

 a{
    margin-left:20px;
 }

Solution 4 - Css

If you use as following, your code can be more effective than you wrote. You should add another feature.

.abc, .xyz {
margin-left:20px;
width: 100px;
height: 100px;
} 

OR

a.abc, a.xyz {
margin-left:20px;
width: 100px;
height: 100px;
} 

OR

a {
margin-left:20px;
width: 100px;
height: 100px;
} 

Solution 5 - Css

just seperate the class name with a comma.

.a,.b{
your styles
}

Solution 6 - Css

And if you have set these classes with their parents before, you should set them with parents (repeat each time) again as below:

parent .abc, parent .xyz

Solution 7 - Css

Using CSS pseudo-classes :is (previously :any and :matches) and :where, you can use comma to match multiple classes on any level.

At the root level, :is(.abc, .xyz) and .abc, .xyz function almost identically. However, :is allows matching only a part of the selector without copying the whole selector multiple times.

For example, if you want to match .root .abc .child and .root .xyz .child, you can write code like this:

.root :is(.abc, .xyz) .child {
   margin-left: 20px;
}  

<div class="root">
  <a class="abc">
    <span class="child">Lorem</span>
  </a>
  <a class="xyz">
    <span class="child">Ipsum</span>
  </a>
</div>

The difference between :is and :where is that :where is ignored when calculating specificity of the selector:

  • specificity of .root :is(.abc, .xyz) .child = specificity of .root .abc .child
  • specificity of .root :where(.abc, .xyz) .child = specificity of .root .child

Both pseudo-classes accept a forgiving selector list, meaning that if one selector fails to be parsed (due to unsupported syntax, either too new, too old or just incorrect), the other selectors would still work. This makes :is useful even at the root level, because it allows combining selectors using modern CSS features without fear that one selector will break the rest.

P.S. This question answers a more difficult variation of the question, but Google returns this page on too many queries, so I think this information will be relevant here.

Solution 8 - Css

There is also a way to use @extend to achieve this with SCSS.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsatView Question on Stackoverflow
Solution 1 - CssJuraj BlahunkaView Answer on Stackoverflow
Solution 2 - CsszombatView Answer on Stackoverflow
Solution 3 - CssNitekursView Answer on Stackoverflow
Solution 4 - CssFatih Kaan AÇIKGÖZView Answer on Stackoverflow
Solution 5 - CssAlphonse PrakashView Answer on Stackoverflow
Solution 6 - CssShahin HsynzdeView Answer on Stackoverflow
Solution 7 - CssAthariView Answer on Stackoverflow
Solution 8 - CssgceView Answer on Stackoverflow