简体   繁体   English

CSS-选择多个元素的约定

[英]CSS - Conventions For Selecting Multiple Elements

I have a few CSS commands that change a lot of similar elements but it just looks ugly, I was wondering if there was a better way (conventionally) to select them. 我有一些CSS命令可以更改很多相似的元素,但是看起来很难看,我想知道是否有更好的方法(传统上)来选择它们。 All documentation suggests that I am correct so I have turned to the community again. 所有文档都表明我是正确的,因此我再次求助于社区。

Code: 码:

.customRadioNPS .generic_radio_1, .customRadioNPS .generic_radio_2{
  background:#33CC66;
  height: 40px;
  width: 40px;
}
.customRadioNPS .generic_radio_3, .customRadioNPS .generic_radio_4, .customRadioNPS     .generic_radio_5, .customRadioNPS .generic_radio_6, .customRadioNPS     .generic_radio_7, .customRadioNPS .generic_radio_8{
  background:#FFFF99;
  height: 40px;
  width: 40px;
}
.customRadioNPS .generic_radio_9,.customRadioNPS  .generic_radio_10{
  background:#FF3333;
  height: 40px; 
  width: 40px;
}
.customRadioNPS .generic_roption_1, .customRadioNPS .generic_roption_2{
  background:#33CC66;
}
.customRadioNPS .generic_roption_3, .customRadioNPS .generic_roption_4, customRadioNPS .generic_roption_6, .customRadioNPS .generic_roption_7, .customRadioNPS .generic_roption_8{
  background:#FFFF99;
}
.customRadioNPS .generic_roption_9, .customRadioNPS .generic_roption_10{
  background:#FF3333;
}
.customRadioNPS .generic_roption_5{
  border-right: 2px dashed gray;
  background:#FFFF99;
}

You can see that they are all within the customRadioNPS class but I am not sure if there was a better way so I did not perhaps, have to keep on naming .customRadioNPS? 您可以看到它们都在customRadioNPS类中,但是我不确定是否有更好的方法,所以我也许不必继续命名.customRadioNPS?

You cannot simplify the CSS selectors used as such. 您不能简化这样使用的CSS选择器。 But depending on the markup (about which you do not say anything), it may be possible to refer to the same elements using simpler selectors. 但是根据标记(您什么都不说),可以使用更简单的选择器来引用相同的元素。

For example, if classes like generic_radio_1 are only used, and will only be used, for the specific elements that you wish to refer to, then there is no need to use the selector .customRadioNPS before class selectors like .generic_radio_1 (except in the unlikely case that you have other rules that apply to those elements, too, and you need selectors with higher specificity to override them). 例如,如果对于您要引用的特定元素仅使用且仅将使用generic_radio_1类的类,则无需在.customRadioNPS类的类选择器之前使用选择器.generic_radio_1 (除非不太可能)如果您还有其他适用于这些元素的规则,并且您需要具有更高特异性的选择器来覆盖它们)。

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

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