问题
Is there a way in CSS to modify the parent element, given the first child?
In my case, this would be to format a table cell differently if the first child is a link.
<td>Hello</td>
<td><a href="go.html">Go here!</a></td>
<style>
td { padding: 5px; } /* Normal cell */
?? { padding: 0px; } /* Cell with link as first child */
</style>
回答1:
No, but you can think of a different approach and do something like:
td { padding: 5px; } /* Normal cell */
td a { margin: -5px; } /* Cell with link as first child */
There are many other ways to do the similar, of course.
CSS4 should offer a :has
pseudo class which would do exactly what you wanted, but we need to wait for it.
来源:https://stackoverflow.com/questions/25747188/css-modify-parent-based-on-first-child