Bottom Line:
No, because <td>
and <th>
can not be siblings since they are not proper children of a <table>
and even if your source markup has them that way - the browser will adjust the markup and overrule your styles.
Long explanation:
Looking at a more JS related SO question on the subject, the browser automatically will inject <thead>
and <tbody>
around your <th>
and <tr>
(subsequently <td>
) elements. <thead>
and <tbody>
are valid child elements of <table>
- <th>
and <tr>
are not.
As a result, finding the siblings of <th>
will only return other th tags, since they technically live in a <thead>
- the <td>
are in a <tr>
in <tbody>
Take a look at these examples:
Example 1
Codepen with straight <th>
and <tr>
elements
.isSystem + .row { background:red }
<table>
<th class="isSystem">Table Heading</th>
<tr class="row">
<td>Table Item</td>
</tr>
</table>
<div class="isSystem">Div Heading</div>
<div class="row">Div Item</div>
In this example, you would expect the table row to be red... The div
elements in the example do this but the <tr>
doesn't
Example 2
Codepen with proper <thead>
and <tbody>
elements
In example 2, wrapping the table with the correct thead
and tbody
elements, you can acheive this:
.isSystem + .rows tr { background:red; }
<table>
<thead class="isSystem"><th>Heading</th></thead>
<tbody class="rows">
<tr class="row"><td>Item</td></tr>
</tbody>
</table>
Unfortunately if your items are dynamically generated and you can not apply your classes in this way, then your only option will be using JS to target your elements as others have already mentioned. However, I would do what's possible to create proper semantic markup first.