Before and After pseudo classes used with styled-components
Asked Answered
W

6

85

What is the proper way to apply :before and :after pseudo classes to styled components?

I know that you can use

&:hover {}

to apply the :hover pseudo class to a styled-component.

Does this work for All pseudo elements like before and after?

I have tried using the &:before and &:after strategy with some rather complex examples and i'm not sure if my attempts are not working because i've got something wrong with my example or it just doesn't work like that.

Does someone have some insight on this? Thank you.

Wish answered 24/8, 2017 at 21:50 Comment(2)
Was this your issue? https://mcmap.net/q/246507/-how-to-render-pseudo-before-content-dynamically-in-styled-componentChrysostom
It’s been so long now I don’t recall!Wish
T
180

Pseudo-selectors in styled-components work just like they do in CSS. (or rather, Sass) Whatever isn't working is likely a problem in your specific code, but that's hard to debug without seeing the actual code!

Here is an example of how to use a simple :after:

const UnicornAfter = styled.div`
  &:after {
    content: " 🦄";
  }
`;

<UnicornAfter>I am a</UnicornAfter> // renders: "I am a 🦄"

If you post your code I'll likely be able to help debug your specific issue!

Troth answered 24/8, 2017 at 22:31 Comment(5)
That's not necessary. Thank you for clarifying! I just needed to know for sure that it was something on my end... =) I wasn't sure that &:before and &:after was working. I couldn't find a definitive answer via google... so... Thanks!Wish
No worries, happy to help!Troth
@Troth Hi, mxstbr! I'm having a problem rendering Pseudo-before content dynamically. am I doing wrong? or is it something styled-components doesn't support. #46339534Impede
Can we apply after and before to any specific child (ie :first-child)?Polyglot
If you've come here using object syntax instead of template string, and you want an empty content :before element, you have to include quotations within quotations like content: '""',Ese
P
12

This will print the triangle at middle of the div.

const LoginBackground = styled.div`
  & {
    width: 30%;
    height: 75%;
    padding: 0.5em;
    background-color: #f8d05d;
    margin: 0 auto;
    position: relative;
  }
  &:after {
    border-right: solid 20px transparent;
    border-left: solid 20px transparent;
    border-top: solid 20px #f8d05d;
    transform: translateX(-50%);
    position: absolute;
    z-index: -1;
    content: "";
    top: 100%;
    left: 50%;
    height: 0;
    width: 0;
  }
`;
Perfoliate answered 30/6, 2018 at 23:12 Comment(0)
L
5

Adding to @mxstbr answer

Note that when you want to render before based on props, don't forget to wrap it with double quotes (or single quotes) , example:

const Button = styled.button`
&:before {
content:"${(props)=>props.theme==='dark'?'dark theme':'white theme'}";
}

because

content:${(props)=>props.theme==='dark'?'dark theme':'white theme'};

will not work

Lavery answered 30/10, 2022 at 2:55 Comment(0)
C
4

This is good and simple answer:

https://mcmap.net/q/239490/-before-and-after-pseudo-classes-used-with-styled-components by mxstbr

but for elements requiring more complex logic I prefer this approach:

const Figure = styled.div`
  ${Square}:before, 
  ${Square}:after,
  ${Square} div:before, 
  ${Square} div:after {
    background-color: white;
    position: absolute;
    content: "";
    display: block;
    -webkit-transition: all 0.4s ease-in-out;
    transition: all 0.4s ease-in-out;
  }
`;
Cortex answered 7/10, 2020 at 10:4 Comment(1)
i can't get this to work with @emotion/babel-plugin 11.3.0 github.com/emotion-js/emotion/issues/2354Norsworthy
C
3

As an object (note the double quotes):

const Div = styled.div({
  '&:before': {
    content: '"a string"',
  },
})
Cyanate answered 18/9, 2021 at 19:42 Comment(0)
T
-31
Can try like this.
It works perfectly fine

var setValue="abc";
var elementstyle = '<style>YourClass:before { right:' + abc + 'px;}</style>'
 $(".YourClass").append(setValue);

 var rightMarginForNotificationPanelConnectorStyle = '<style>.authenticated-page.dx-authenticated .dx-notification .dx-notification-dialog.dx-content-frame:before { right:' + rightMarginForNotificationPanelConnectorWithBadge + 'px;}</style>'
                        $(".authenticated-page.dx-authenticated .dx-notification .dx-notification-dialog.dx-content-frame").append(rightMarginForNotificationPanelConnectorStyle);
Tendon answered 15/4, 2019 at 10:2 Comment(2)
The question was about styled-components and you are answering using plain javascript. Moreover your solution is not a good practice at all (using jquery, appending to the dom, etc). Also, consider improving the format of your answer: you put everything in a code sectionWithin
there is no point in using react if you are actually manipulating the dom using vanilla jsLavery

© 2022 - 2024 — McMap. All rights reserved.