css - 仅影响 parent

标签 css

让我们开门见山,我创建这个例子是为了更好地表达我的观点:

Demo Here

HTML: 表1

<table class="testClass">
    <tr>
        <td>Inner table
            <table>
                <tr>
                    <td>Hello</td>
                    <td>Testing testing</td>
                    <td>Bye</td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br />
<br />
<br />Table 2
<table class="testClass">
    <tr>
        <td colspan="3">stuff</td>
    </tr>
    <tr>
        <td>Left</td>
        <td>Middle</td>
        <td>Right</td>
    </tr>
</table>

CSS:

table {
    border: 2px solid red;
    width: 100%;
}
td {
    border: 2px solid blue;
}
/* Relative CSS */
 .testClass tr:last-child td:nth-child(1) {
    width: 15px;
}
.testClass tr:last-child td:nth-child(2) {
    width: auto;
}
.testClass tr:last-child td:nth-child(3) {
    width: 15px;
}

所以我们有 2 个表,都具有相同的类。 表 1 中有一个表,而 表 2 没有。

我发现的问题是使用我创建的 CSS 我无法阻止 .testClass 的样式影响子表(内表)。我在想 :not() 可以使用,但我无法找到使用它的解决方案,但我觉得这不应该那么难。

是否可以只影响从父表到子表的样式中的父表?

注意:只能更改 CSS,不能更改 HTML。可以使用CSS3!

我希望这是有道理的,如果我需要说得更清楚,请发表评论。

最佳答案

选择第一级 child 并应用它。

   .testClass > tbody > tr:last-child > td:nth-child(1) {
      width: 15px;
   }
  .testClass > tbody > tr:last-child > td:nth-child(2) {
     width: auto;
   }
   .testClass > tbody > tr:last-child > td:nth-child(3) {
      width: 15px;
   }

DEMO

关于css - 仅影响 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25565765/

相关文章:

html - 如何强制 margin-right 0?

html - 强制 div 进入左列

Javascript 和 CSS : how to get the value of the style sheet file?

html - Bootstrap 中的输入组类不允许同一行上的任何其他元素

html - 避免水平滚动 HTML 网页

html - 过渡属性背景图像不适用于 Firefox(使用前缀)

css - 如何让 liveserver 渲染 django 模板?

jquery - 自定义具有 4 个不同范围的 jQuery UI slider

jquery - css3 动画与 scaleX 和 scaleY?

html - 使用 CSS 在列之间放置间隙