Course contentsShow
← React
Lesson 1927 of 2,39644. Styling ApproachesFree lesson

Global Styles and CSS Reset

Using `createGlobalStyle` for app-wide styles, resets, and font imports that exist outside component trees.

Global Styles and CSS Reset

You'll learn how to apply app-wide styles—like resets, fonts, and base element styling—that sit outside your component tree using createGlobalStyle.

The Problem: Component Styles Aren't Enough

With styled-components, you've been creating scoped styles for individual components. But what about styles that affect everything—like removing default browser margins, setting a base font, or importing web fonts? These don't belong to any single component; they're foundational to your entire app.

Enter createGlobalStyle

styled-components provides createGlobalStyle, which creates a special component that injects global styles into the document's <head>. Unlike normal styled components, it doesn't render visible elements—it only adds styles.

import { createGlobalStyle } from 'styled-components';

const GlobalStyles = createGlobalStyle`
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }

  body {
    font-family: 'Inter', sans-serif;
    line-height: 1.6;
    color: #333;
  }

  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap');
`;

// Then in your App component:
function App() {
  return (
    <>
      <GlobalStyles />
      {/* Rest of your app */}
    </>
  );
}

You render <GlobalStyles /> once, typically at the root of your app. It doesn't matter where in the component tree—it always injects global styles.

Common Use Cases

  • CSS resets (normalize browser inconsistencies)
  • Base typography (default fonts, sizes, line-height)
  • Font imports from Google Fonts or other CDNs
  • Global animations or utility classes

Key Takeaway: Use createGlobalStyle to define app-wide styles like resets and font imports that need to exist outside individual component scopes—render it once at your app's root.