Is it possible to select a table cell based on the state of a previous cell using CSS? I have a feeling that this may not be achievable with CSS alone. Can someone confirm my suspicion?
Let's consider this example:
<table>
<tr>
<td>Row with checked input</td>
<td><input type=checkbox checked /></td>
<td>should show.</td>
</tr>
<tr>
<td>Row with unchecked input</td>
<td><input type=checkbox /></td>
<td>should be hidden.</td>
</tr>
</table>
Here is the CSS code that I attempted:
td:last-child { display:none; }
input:checked ~ td:last-child { display:initial; }
The first part of the selector can hide unchecked inputs, but the second part fails to make the input visible again. It seems like the ~
combinator struggles with targeting children of previous siblings. Is there a workaround in CSS for this issue, or should I consider a JavaScript solution?
Feel free to check out this JSFiddle link for live testing and experimentation.