I am having a difficult time trying to style a td
element which has a div
that has a class called ‘check’ as its child element. I am trying to style it from div
element which is the parent of the table
.
This is my selector in the below example:
<div class="[&>table>tbody>tr>td:has-[ .check]]:bg-gray-100">
<table>
<thead>...</thead>
<tbody>
<tr>
<td><div class="check">...</div></td>
</tr>
</tbody>
</table>
</div>
I am not that fluent with tailwind. What is it that I am doing wrong and how can I fix it
I’ve tried achieving this using the has-
modifier
https://tailwindcss.com/docs/hover-focus-and-other-states#styling-based-on-descendants
2
Answers
Updated Answer
While you cannot style a parent depending on the children by
:has-[.CLASS]
, you can do so by javascript.Orginal Answer
You cannot select a class with the
has-{modifier}
, you can only select an element by either a pseudo-class or by the tag name.As per TailwindCSS
However, you can achive that without
has-*
simply by selecting the element with.check
class like the following:I think
has-[>table>tbody>tr>td>.check]:bg-gray-100
is what you need.Tailwind Playground
Update
To target the
td
parent of.check
.<td class="has-[>.check]:bg-gray-100"><div class="check">...</div></td>
Tailwind Playground