Is it possible to combine nth-child and hover in tailwind?
<tr key={i} className='[&>*:nth-child(even)]:bg-[white] hover:bg-[#6b7280] [&>*:nth-child(odd)]:bg-[#eee] hover:bg-[#6b7280]'>
This is what i currently have, but now hover does not work, without the even rule, only the odd columns get changed on hover …
EDIT: new version of the code:
{dataProtokoll.map((k, i) => {
return (
<tr key={i} className='[&>*:nth-child(odd)]:bg-[#eee] [&>*:nth-child(even)]:bg-[white] group' onClick={() => { i === selectedRowProtokoll ? setSelectedRowProtokoll(-1) : setSelectedRowProtokoll(i) }}>
<>
{Object.values(k).map(value => <td className={clsx('text-left border px-2.5 py-2 border-solid border-[rgb(160_160_160)] group-hover:bg-[#6b7280]', { 'bg-[#6b7280]': i === selectedRowProtokoll })}>{value}</td>)}
</>
</tr>
);
})}
2
Answers
You are applying
background-color
to the<tr>
on hover. The inner elements (presumably<td>
elements) have their own background colors. The<td>
elements’ background colors draw over the<tr>
element’s background color, so it is never shown. Instead, you could consider changing the background colors of all the<td>
elements like:Or:
Just use these classes first:style , second:, odd:, even:, last: you can use it in every child class no matter the index they will be applied to the specific div