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.

Pseudo elements ::before and ::after don't work when using object syntax

See original GitHub issue

Environment

System:

  • OS: macOS 10.14
  • CPU: (4) x64 Intel® Core™ i5-7360U CPU @ 2.30GHz
  • Memory: 69.29 MB / 8.00 GB
  • Shell: 5.3 - /bin/zsh

Binaries:

  • Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
  • Yarn: 1.9.4 - /usr/local/bin/yarn
  • npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
  • Watchman: 4.9.0 - /usr/local/bin/watchman

npmPackages:

  • styled-components: ^4.0.3 => 4.0.3

Steps to reproduce

Create a styled component in object syntax with a pseudo selector. ie:

const StyledDiv = styled.div({
  '::before': {
    content: '🚀'
  }
})

Expected Behavior

We should see a pseudo element inside the div.

Actual Behavior

A pseudo element doesn’t get added to the div

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
probablyupcommented, Nov 11, 2018

Does actually work, there’s just a nuance around how the content property needs to be set: https://codesandbox.io/s/lxm7q494kz

2reactions
reveltcommented, Mar 20, 2020

TLDR — wrap value with extra quotes:

BAD:

&&:after {
    content: ${(props) => `${props.cta ? props.cta : 'Click'}`};
}

GOOD:

&&:after {
    content: ${(props) => `"${props.cta ? props.cta : 'Click'}"`};
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

before - CSS: Cascading Style Sheets - MDN Web Docs
In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content...
Read more >
::before / ::after | CSS-Tricks - CSS-Tricks
The ::before and ::after pseudo-elements in CSS allows you to insert content onto a page without it needing to be in the HTML....
Read more >
[Fixed] CSS :after element is not working - Weekend Projects
A few ways to fix why :after element is not working. 1. We need to check if we are using the right HTML...
Read more >
Diving into the ::before and ::after Pseudo-Elements
In CSS, ::before and ::after are keywords you can add to a selector to create pseudo-elements. The pseudo-elements are inserted into the element ......
Read more >
CSS pseudo-elements a Ridiculously Simple Explanation
Did you notice the use of the content property, with the ::before and ::after pseudo-elements? Objects that are inserted using the content ...
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