I have a scenario where I want to target all tables (or any element) in the page / a container except those elements inside a special container or class (for example .exempt-table) .
I tried it using :not()
selector to target "All tables except those inside .exempt-table
" as follows:
.container :not(.exempt-table) table {
border: 5px solid red;
}
Here is the complete example:
div {
border: 1px solid blue;
padding: 5px;
}
div::before {
content: 'DIV';
}
.container :not(.exempt-table) table {
border: 5px solid red;
}
<div class="container">
<div>
<h1>General Table. Table1</h1>
<table>
<tr>
<td>COl1</td>
<td>COl1</td>
<td></td>
</tr>
<tr>
<td>COl1</td>
<td>COl1</td>
<td></td>
</tr>
</table>
</div>
<div class="exempt-table">
<h1>Special Table. Table2</h1>
<table>
<tr>
<td>COl1</td>
<td>COl1</td>
<td></td>
</tr>
<tr>
<td>COl1</td>
<td>COl1</td>
<td></td>
</tr>
</table>
</div>
<div>
<h1>NESTED: General Table. Table3</h1>
<div>
<table>
<tr>
<td>COl1</td>
<td>COl1</td>
<td></td>
</tr>
<tr>
<td>COl1</td>
<td>COl1</td>
<td></td>
</tr>
</table>
</div>
</div>
<div class="exempt-table">
<h1>NESTED: Special Table. Table4</h1>
<div >
<table>
<tr>
<td>COl1</td>
<td>COl1</td>
<td></td>
</tr>
<tr>
<td>COl1</td>
<td>COl1</td>
<td></td>
</tr>
</table>
</div>
</div>
</div>
Expectation is, border
(red) should not be applied to Table2, Table4 because these tables are inside .exempt-table
. But, the border applied to all tables except Table2 because Table2 is direct child if .container
where as Table4 is just descendant.
27th Nov:
Updated the above example to address the issue with > table
.