【问题标题】:How to exclude a css class for a tag in the same div/ tr? [duplicate]如何为同一 div/tr 中的标签排除 css 类? [复制]
【发布时间】:2018-08-30 14:11:58
【问题描述】:

我有一个div如下:

<tr class="section">
   <td>some content here</td>
   <td class="info">content 2 here</td>
</tr>

css:

.section{
   //style here
 }

.info{
   //another style here
}

在这种情况下,我不想在 info 中应用 section 样式。

怎么做?

【问题讨论】:

  • HTML 无效。 td 只能是 tr 的子级。
  • 它是无效的,但如果我们认为它们跨度......你不能,你只需要恢复样式但不是全部(显示,不透明度等)
  • 此外,您无法阻止样式继承 - 您只能覆盖来自父元素的不需要的样式。
  • 问及回答here

标签: html css css-selectors


【解决方案1】:

您可以通过以下方式使用:not 选择器:.section *:not(.info)

(我稍微更改了您的 HTML 以使其有效)

.section{
   color: red;
 }

.section *:not(.info){
   color: green;
}
<div class="section">
   <span>some content here</span>
   <span class="info">content 2 here</span>
</div>

【讨论】:

  • 谢谢@Johannes,这很好,但是 section 类是静态的,这里不应该改变任何东西,因为“content 2”我只是想要排除“部分”样式。
  • 为什么你也重新打开了这个问题......当我们关闭你回答的问题时你不喜欢......
  • 所谓的“重复”是从 2009 年开始的,在接受的答案下有一条评论(有 9 个赞)说这个答案已经过时了
  • @Johannes 这并没有减少重复。这个问题还有 10 个其他答案。
【解决方案2】:

只有在使用 HTML 表格时才应使用 td,而您可以使用 div 或更语义化的标签...check here

在css中你可以像这样覆盖你的父类..

.section .info{
   //overwrite style here
 }

【讨论】:

  • 是的,我在 HTML 表格中使用了这些 td。
  • 谢谢@Renzo
猜你喜欢
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 2018-06-23
  • 2013-01-13
  • 2019-08-11
  • 2018-12-04
相关资源
最近更新 更多