Select the nth child in TailwindCSS
Question:
How to select the nth child in TailwindCSS? Answer:
<table class="w-80">
<tbody class="[&>*:nth-child(odd)]:bg-teal-200">
<tr><td>Line - 1</td></tr>
<tr><td>Line - 2</td></tr>
<tr><td>Line - 3</td></tr>
<tr><td>Line - 4</td></tr>
<tr><td>Line - 5</td></tr>
</tbody>
</table>
Description:
To access the nth
child using TailwindCSS you can use the arbitrary variants, that are format strings that represent the selector, wrapped in square brackets. You can write custom selector modifiers directly in your HTML. Just like arbitrary values let you use custom values with your utility classes, arbitrary variants let you write custom selector modifiers directly in your HTML:
For example to create a zebra styled table you can use the arbitrary variant: [&>*:nth-child(odd)]
Where:
- The ampersand
&
is used to reference the parent selector. - The greater than symbol
>
is used to select direct children of an element. - The asterisk
*
is used to select all descendants of an element.
So the following code:
<table class="w-80">
<tbody class="[&>*:nth-child(odd)]:bg-teal-200">
<tr><td>Line - 1</td></tr>
<tr><td>Line - 2</td></tr>
<tr><td>Line - 3</td></tr>
<tr><td>Line - 4</td></tr>
<tr><td>Line - 5</td></tr>
</tbody>
</table>
Results the following output:
Reference:
TailwindCSS pseudo clas reference
Share "How to select the nth child in TailwindCSS?"
Related snippets:
- Create blinking background with TailwindCSS
- Create a neon effect with TailwindCSS
- Rotate element with TailwindCSS
- Change SVG icon color with TailwindCSS
- Using :not in TailwindCSS
- Specify exact width value in TailwindCSS
- Set element width over 100% using Tailwind CSS
- Stretch children to fill horizontally
- Create a fixed navbar with TailwindCSS
- Use font from local files in TailwindCSS
- Make an element invisible in mobile size in Tailwind CSS
- Create a 20/80 grid with Tailwind CSS
- Select the nth child in TailwindCSS
- Add class to child when hovering on parent in TailwindCSS
- Fill the full height of the screen in TailwindCSS
- Create transition with TailwindCSS
- Create horizontal rule with text using TailwindCSS
- Set background image with TailwindCSS
- How to use CSS variables in TailwindCSS
- Center an absolute element in TailwindCSS
- Remove arrows from number input in TailwindCSS
- How to use !important in TailwindCSS
- Access child elements in TailwindCSS
- Select first child in TailwindCSS
- Create semi transparent background in TailwindCSS
- How to use calc() in TailwindCSS
- Add text shadow with Tailwind CSS
Tags:
select, choose, nth, odd, even, child, tailwind Technical term:
Select the nth child in TailwindCSS