How to render pseudo before content dynamically in styled-component

前端 未结 3 1713
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-20 05:57

I\'m having a trouble of rendering pseudo before content dynamically in styled-components.

Am I doing something wrong?

I have no problem when I render pseudo bef

相关标签:
3条回答
  • 2021-02-20 06:43

    That is because content value must be within quotes in css

    Example:

    const Block = styled.div`
        &:before {
            display: absolute;
            top:0;
            content: '${props => props.before}'
        }
    `
    

    Please note that I am leveraging ES6 new features wherein a single statement function there is no need to use curly braces {} and return.

    Codepen: https://codepen.io/metju/pen/zEKeOm

    0 讨论(0)
  • 2021-02-20 06:45

    styled-components works the same as in sass, nothing different

    const Text = styled.span`
        &:before {
           content: ${props => !!props.before ? props.before : "                                                                     
    0 讨论(0)
  • What I did is use the css helper function for this:

    const Text = styled.span`
      &:before {
         ${props => !!props.before ?
          css`content: props.before;` : css`content: '                                                                     
    0 讨论(0)
提交回复
热议问题