【问题标题】:How to combine the two CSS rules with a single selector [duplicate]如何将两个 CSS 规则与单个选择器结合使用 [重复]
【发布时间】:2013-09-29 15:57:54
【问题描述】:

我有两个带有不同选择器的 CSS 规则,我想将它们组合起来。两条规则具有相同的 CSS 属性。第一个选择器以th 元素为目标,其他选择器以td 元素为目标:

.MyClass th:nth-child(n+6):nth-child(-n+9){
    text-align: right;
}

.MyClass td:nth-child(n+6):nth-child(-n+9){
    text-align: right;
}

我想这两个规则可以结合起来,但是怎么做呢?

编辑

好吧,我知道我的回答是错误的。我的问题有点简单!

我已经习惯了用这样的逗号将它们放在一起:

.MyClass th:nth-child(n+6):nth-child(-n+9),
.MyClass td:nth-child(n+6):nth-child(-n+9){
    text-align: right;
}

但我真正想知道的是如何只做一个:nth-child(n+6):nth-child(-n+9) 的选择器。

【问题讨论】:

  • 通过将它们彼此相邻并用逗号分隔?
  • 天哪,我讨厌答案如此简单,而且我一直在放 cmets。
  • 把它作为答案然后... ;-)
  • 现在有点太晚了 :D 我不会得到它的功劳,多么粗鲁。
  • 或者实际为网站做出贡献并将其标记为重复。

标签: html css css-selectors


【解决方案1】:

你试过了吗

.MyClass th:nth-child(n+6):nth-child(-n+9), 
.MyClass td:nth-child(n+6):nth-child(-n+9) {
    text-align: right;
}

【讨论】:

  • 这行得通,谢谢。但我希望有一个更短的版本!我会改变我的问题。
  • @Timo002 不要更改,发布另一个......
【解决方案2】:

使用逗号, 分隔符,这是一个示例

.class1, .class2 {
   text-align: right;
}

【讨论】:

  • 我不知道为什么在这里投反对票?
猜你喜欢
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
相关资源
最近更新 更多