Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

target first-child css styled-components

I am using styled-components and want to target the first child of Text, but am unable to do so.

const Text = styled.p`
    font-size: 12px;
    &:first-child {
        margin-bottom: 20px;
    }
`;

... component

return(
   <div>
      <p>I am just regular text</p>
      <p>Me too</p>
      <Text>Hello Joe</Text> // this should have the margin bottom
      <Text>Goodbye</Text >
   </div>
)
like image 835
peter flanagan Avatar asked Jan 02 '19 15:01

peter flanagan


People also ask

How do you target first child in styled-components?

The styled component confuses with the first two native p tag (from my perspective) and that's the reason why the CSS is not applied. OR, you can do something like this: Adding a class name for the tag and giving CSS for that class.

How do I select the first child of a class in CSS?

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

How do you target first child in SCSS?

The :first-child selector allows you to target the first element immediately inside another element. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.

Is first child CSS?

The first-child is a pseudo class in CSS which represents the first element among a group of sibling elements. The :first-child Selector is used to target the first child element of it's parent for styling.


4 Answers

Finally, I got your issue. The styled component confuses with the first two native p tag (from my perspective) and that's the reason why the CSS is not applied.

I will use a workaround like this:

const Text = styled.p`
    font-size: 12px;
    color: blue;
    &:nth-child(3) {
        margin-bottom: 20px;
        color: red !important;
    }
`;

By doing this, you are selecting the third child (which include the first two p tag) for the CSS

OR, you can do something like this: Adding a class name for the tag and giving CSS for that class.

const Text = styled.p`
  font-size: 12px;
  color: blue;
  &.colors {
    margin-bottom: 20px;
    color: red !important;
  }
`;

 <div>
    <p>I am just regular text</p>
    <p>Me too</p>
    <Text className="colors">Hello Joe</Text>
    <Text>Goodbye</Text>
</div>

Here is the demo

Hope it helps :)

like image 50
Thinker Avatar answered Oct 04 '22 13:10

Thinker


Use like this

const Text = styled.p`
   font-size: 12px;
    > * {
      &:first-child {
         margin-bottom: 20px;
      }
    }
`;
like image 28
Dagar Avatar answered Oct 04 '22 15:10

Dagar


There shouldn't be a space between the & and the :first-child

&:first-child {
    margin-bottom: 20px;
}
like image 44
dan Avatar answered Oct 04 '22 13:10

dan


it's better to use :last-of-type on certain styled component instead of using :nth-child and it works perfectly

export default styled.div`
    :last-of-type {
        background: red;
    }`

const Text = styled.p`
    font-size: 12px;
    color: blue;
    &:nth-child(3) {
        margin-bottom: 20px;
        color: red !important;
    }
`;
like image 41
toxxiczny Avatar answered Oct 04 '22 14:10

toxxiczny