css - 仅将样式应用于 td 标签的第一级

标签 css css-selectors

有没有办法将类的样式仅应用于一级 td 标签?

<style>.MyClass td {border: solid 1px red;}</style>

<table class="MyClass">
  <tr>
    <td>
      THIS SHOULD HAVE RED BORDERS
    </td>
    <td>
      THIS SHOULD HAVE RED BORDERS
      <table><tr><td>THIS SHOULD NOT HAVE ANY</td></tr></table>
    </td>
  </tr>
</table>

最佳答案

Is there a way to apply a Class' style to only ONE level of td tags?

*:

.MyClass>tbody>tr>td { border: solid 1px red; }

但是! ‘>’直接子选择器在 IE6 中不起作用。如果您需要支持该浏览器(唉,您可能会这样做),您所能做的就是单独选择内部元素并取消设置样式:

.MyClass td { border: solid 1px red; }
.MyClass td td { border: none; }

*请注意,第一个示例引用了在您的 HTML 中找不到的 tbody 元素。它应该已经在您的 HTML 中,但浏览器通常可以将其省略...他们只是在幕后添加它。

关于css - 仅将样式应用于 td 标签的第一级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/613284/

相关文章:

jQuery 导航标题

html - 到目前为止我发现闪烁的文字不起作用

javascript - 我想使用 css 进行叠加,但它不起作用

html - 无序列表需要 CSS 帮助

css - TR 中的最后一个 TD 和倒数第二个 TD (CSS/LESS)

html - .class :nth-child(even) not working?

html - 第一个 child 和列表中的列表

javascript - 使用JS通过数学运算修改样式元素

html - Bootstrap 4 : Placing a image between two card blocks

html - 如何在CSS中为不同的标题(h1,h2,...)设置样式?