简体   繁体   English

CSS中将多个具有相同属性选择器的选择器合并为一个选择器

[英]Combine multiple selectors with the same attribute selector into one selector in CSS

Is it possible in CSS/SASS/LESS to combine multiple selectors with the same attribute into one selector without repeating the attribute?在 CSS/SASS/LESS 中是否可以将具有相同属性的多个选择器组合成一个选择器而不重复该属性?

For example:例如:

div.foo[data="123"] {
  color: red;
}

p.bar[data="123"] {
  color: red;
}

p.bar[data="123"], div.foo[data="123"]{
  color: red;
}

Combine them somehow?以某种方式组合它们? Such as this invalid scenario:比如这个无效的场景:

(div.foo, p.bar)[data = "123"] {
  color: red;
}

You could do it like that:你可以这样做:

div.foo,
p.bar {
    &[data="123"] {
        color: red;
    } 
}
  • first step:第一步:
    combine your main selectors结合你的主要选择器
  • second step:第二步:
    adds the data selector添加数据选择器

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

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