πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Styled Components

React Styling🟒 Free Lesson

Advertisement

Styled Components

CSS-in-JS, dynamic styles, theming, global styles, and server-side rendering.

Overview

Styled-components lets you write actual CSS in JavaScript with component-level scoping.

Key Concepts

  • Template Literals β€” Write CSS in tagged template literals
  • Dynamic Props β€” Pass props to change styles dynamically
  • Theme Provider β€” Share theme across all styled components
  • Global Styles β€” Inject global CSS reset or base styles
  • SSR Support β€” Server-side rendering with style collection

Code Examples

import styled, { ThemeProvider, createGlobalStyle } from 'styled-components';

const Button = styled.button`
  background: ${props => props.primary ? '#007bff' : 'white'};
  color: ${props => props.primary ? 'white' : '#007bff'};
  padding: 10px 20px;
  border: 2px solid #007bff;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  }
`;

const GlobalStyle = createGlobalStyle`
  body { margin: 0; font-family: sans-serif; }
`;

function App() {
  return (
    <ThemeProvider theme={{ primary: '#007bff', secondary: '#6c757d' }}>
      <GlobalStyle />
      <Button primary>Primary</Button>
      <Button>Secondary</Button>
    </ThemeProvider>
  );
}

Practice

Create a themed component library with dark mode support using styled-components.

⭐

Premium Content

Styled Components

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert React Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement