简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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