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.

Injecting styles in body without global

See original GitHub issue

Version

2.0.0-13

I have a situation where I am applying styling to a page.

Can I directly inject css styles to the body in this page without applying them through injectGlobal?

Something like the following:

const Cover = styled(CoverUnstyled)`
  body {
    height: 100%
  }
`;

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:28 (15 by maintainers)

github_iconTop GitHub Comments

28reactions
thysultancommented, Jul 6, 2017

@chhuang Making use of the inverted & pattern you could do.

body:not(&) { overflow: hidden; }

Which can essentially be used as an inline global styling escape hatch 😃

25reactions
wmertenscommented, Aug 3, 2017

@geelen @mxstbr I think there is a good case here for having a react component that does the actual injecting (rendering null), removing the CSS when it is unmounted.

instead of

injectGlobal`...`

you’d do

const GlobalStyle = styled.global`
… (including prop interpolation)
`
...
return (
  <App>
    <GlobalStyle darkBg={this.props.dark} />
    ...
  </App>
)

The only differences with regular styled components would be:

  • renders null
  • does not wrap the css with an own class

This way:

  • Global styles can be easily modified via props
  • It works without issues in SSR
  • It’s obvious what is happening
Read more comments on GitHub >

github_iconTop Results From Across the Web

html - How can I insert CSS styles inside <body> independent ...
So, theoretically, the users will first see an ugly html page without styles, then styles will be loaded and they'd see the page...
Read more >
Styling Vue components with CSS - Learn web development
Vue has three common approaches to styling apps: External CSS files. Global styles in Single File Components ( .vue files). Component-scoped ...
Read more >
Style the body element with styled-components ... - Egghead.io
In this lesson, we see how we can apply styles globally with the "injectGlobal" helper method in styled-components. This method allows us to...
Read more >
style-loader - webpack
Automatically injects styles into the DOM using multiple <style></style> . It is default behaviour. ... import styles from "./styles.css"; const divElement = ...
Read more >
Adding Custom Styles - Tailwind CSS
Tailwind has been designed from the ground up to be extensible and customizable, so that no matter what you're building you never feel...
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