CSS 如何一次将样式应用于多个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2099252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:28:29  来源:igfitidea点击:

How can I apply styles to multiple classes at once?

csscss-selectors

提问by sat

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

我希望两个名称不同的类在 CSS 中具有相同的属性。我不想重复代码。

.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?

由于两个类都在做同样的事情,我应该能够将它合并为一个。我怎样才能做到这一点?

回答by Juraj Blahunka

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

is what you are looking for.

就是你要找的。

回答by zombat

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

您可以通过用逗号分隔来为相同的属性创建多个 CSS 声明:

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

回答by Nitekurs

Don't Repeat Your CSS

不要重复你的 CSS

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

OR

或者

 a{
    margin-left:20px;
 }

回答by Fatih Kaan A?IKG?Z

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;
}