我有两个有桌子的div。当我应用第一个div的css
类表时,第二个div表会受到影响。
.tbl-generic th:nth-child(-n+2),td:nth-child(-n+2) {
background-color:red;
width:25px;
}
.tbl-generic th:nth-last-child(-n+3),td:nth-last-child(-n+3) {
background-color:red;
width:25px;
}

<div>
<table class="tbl-generic">
<tr>
<td style="width:55px;">TEST1</td>
<td style="width:55px;">TEST2</td>
<td>TEST3</td>
<td>TEST4</td>
<td>TEST5</td>
<td>TEST6</td>
<td>TEST7</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td style="width:55px;">A</td>
<td style="width:55px;">B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
<td>G</td>
</tr>
</table>
</div>
&#13;
这是jsFiddle
答案 0 :(得分:1)
您在css中忘记了.tbl-generic
.tbl-generic th:nth-child(-n+2), .tbl-generic td:nth-child(-n+2) {
background-color:red;
width:25px;
}
.tbl-generic th:nth-last-child(-n+3), .tbl-generic td:nth-last-child(-n+3) {
background-color:red;
width:25px;
}
<div>
<table class="tbl-generic">
<tr>
<td style="width:55px;">TEST1</td>
<td style="width:55px;">TEST2</td>
<td>TEST3</td>
<td>TEST4</td>
<td>TEST5</td>
<td>TEST6</td>
<td>TEST7</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td style="width:55px;">A</td>
<td style="width:55px;">B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
<td>G</td>
</tr>
</table>
</div>
答案 1 :(得分:0)
这是因为你的CSS:
.tbl-generic th:nth-child(-n+2),td:nth-child(-n+2) {
background-color:red;
width:25px;
}
.tbl-generic th:nth-last-child(-n+3),td:nth-last-child(-n+3) {
background-color:red;
width:25px;
}
这应该是:
.tbl-generic th:nth-child(-n+2),
.tbl-generic td:nth-child(-n+2) {
background-color:red;
width:25px;
}
.tbl-generic th:nth-last-child(-n+3),
.tbl-generic td:nth-last-child(-n+3) {
background-color:red;
width:25px;
}
否则,所有td's
都会获得样式。
更新了您的FIDDLE
答案 2 :(得分:0)
您正在将css应用于所有td
而不是第一个div中的问题
检查
.tbl-generic th:nth-child(-n+2),.tbl-generic td:nth-child(-n+2) {
background-color:red;
width:25px;
}
.tbl-generic th:nth-last-child(-n+3),.tbl-generic td:nth-last-child(-n+3) {
background-color:red;
width:25px;
}
&#13;
<div>
<table class="tbl-generic">
<tr>
<td style="width:55px;">TEST1</td>
<td style="width:55px;">TEST2</td>
<td>TEST3</td>
<td>TEST4</td>
<td>TEST5</td>
<td>TEST6</td>
<td>TEST7</td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td style="width:55px;">A</td>
<td style="width:55px;">B</td>
<td>C</td>
<td>D</td>
<td>E</td>
<td>F</td>
<td>G</td>
</tr>
</table>
</div>
&#13;
答案 3 :(得分:0)
试试这个
div:first-of-type .tbl-generic{
}