Select first child in TailwindCSS
/* On the parent element */
<div class="[&>*:first-child]:bg-red-200">
/* OR on children elements */
<div class="first:bg-red-200">Line-1</div>
There are two ways to get the first child in TailwindCSS. There is a solution based on the parent element and a solution based on the children elements.
The first pseudo class - children based solution
You can style an element if it’s the first child using the first
modifier. This is the equivalent of the CSS pseudo class :first-child
which represents the first element among a group of sibling elements. In other words, in this case, each child element must have the appropriate classes.
<div class="mt-10 w-40">
<div class="p-2 border border-slate-400 border-t-0 first:border-t first:rounded-t-md last:rounded-b-md">Line-1</div>
<div class="p-2 border border-slate-400 border-t-0 first:border-t first:rounded-t-md last:rounded-b-md">Line-2</div>
<div class="p-2 border border-slate-400 border-t-0 first:border-t first:rounded-t-md last:rounded-b-md">Line-3</div>
<div class="p-2 border border-slate-400 border-t-0 first:border-t first:rounded-t-md last:rounded-b-md">Line-4</div>
<div class="p-2 border border-slate-400 border-t-0 first:border-t first:rounded-t-md last:rounded-b-md">Line-5</div>
<div class="p-2 border border-slate-400 border-t-0 first:border-t first:rounded-t-md last:rounded-b-md">Line-6</div>
</div>
The result is:
Aribtrary variants - parent based solution
You can also achieve the previous result by using arbitrary variants [&>*:first-child]
. In this case the parent element will get the corresponding classes. The resulting code is smaller than the previous solution.
<div class="mt-10 w-40 [&>*]:p-2 [&>*]:border [&>*]:border-slate-400 [&>*]:border-t-0 [&>*:first-child]:border-t [&>*:first-child]:rounded-t-md [&>*:last-child]:rounded-b-md">
<div>Line-1</div>
<div>Line-2</div>
<div>Line-3</div>
<div>Line-4</div>
<div>Line-5</div>
<div>Line-6</div>
</div>
The output result is the same.
- 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