question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

First / Last child for margin not working?

See original GitHub issue

I’ve been playing around with pseudo class, but it’s seems margin won’t be affected by pseudo class?

e.g

# working
first:border

# not working
first:mx-0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Dealerpriestcommented, Nov 7, 2022

You want to select the direct children with the > combinator in addition to using the :nth-child psuedo class. The nth-child psuedo class doesn’t automatically point at the children but rather asks if the currently selected object is the nth child. Perhaps a bit confusing as the word “child” is part of the psuedo class 😛 So. In your case you need to put & > :nth-child(3) inside the square brackets. Like so:

<div class="text-red-400 last:text-blue-500 [&>:nth-child(3)]:font-extrabold">
...

this will first target all direct children of the div tag with the > and then match the one which is the 3rd child

playground link: https://play.tailwindcss.com/1tRLpoO1zC

Additional Update: also be aware that if you want to use the last-child psuedo class, the name is actually :last-child. Not last, which is tailwind’s short utility name for it. [&>:last-child]:text-blue-400 https://play.tailwindcss.com/9pGwUY7QKe

2reactions
mehdad-hussaincommented, Jul 11, 2022

@robbyrice I am trying to use these two selector first and last but not working , could you please help me ? Tailwind Playground link

Read more comments on GitHub >

github_iconTop Results From Across the Web

First / Last child for margin not working in the Next.js ...
you could try using the space-y utitlity class.
Read more >
last-child - CSS: Cascading Style Sheets - MDN Web Docs
The :last-child CSS pseudo-class represents the last element among a group of sibling elements.
Read more >
Everything You Need To Know About CSS Margins
See the Pen Margins: margin on first and last child doesn't collapse if the parent has a border by Rachel Andrew. Once again,...
Read more >
Remove Margins for First/Last Elements - CSS-Tricks
It can sometimes be desirable to remove the top or left margin from the first element in a container. Likewise, the right or...
Read more >
CSS | :not(:last-child):after Selector - GeeksforGeeks
:after This is a great selector to add content (or sometimes, even block-level elements) after the selected elements (Here the first inner-div ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found