⚠️ This lesson is retired and might contain outdated information.

Style the body element with styled-components and "injectGlobal"

Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 2 years ago

In this lesson, we see how we can apply styles globally with the "injectGlobal" helper method in styled-components. This method allows us to apply styles on the body element, which cannot be wrapped in a styled-component.

[00:00] Here is a simple React app that works with styled components. We've got three styled components here which I used to output a Person component. We're currently loading some CSS from App.css file.

[00:15] If I command the import App, you can see the font and margins all messed up, because the styles are targeting the body element. We can't really wrap this in a styled component directly.

[00:25] I'll also import injectGlobal from styled components. I can then, here, type injectGlobal with back-ticks and paste my CSS in there. Here we go. We're back.

[00:40] I can now completely remove the App.css import and all our CSS is being handled by styled components. The Global style have been injected in the styled tag in the document head.

Dean
Dean
~ 7 years ago

Hey Simon, Have you been able to test with Jest, react components using styled-components that are exported from a dedicated "styledComponents.js" file. I love styled-components, but hitting a roadblock with testing.

Markdown supported.
Become a member to join the discussionEnroll Today