Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS‑in‑JS (Styled‑Components | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 43 of 50

·css

CSS‑in‑JS (Styled‑Components, Emotion)

9 mins

Course progress86%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Eliminates the need for separate CSS files.
  • Enables **dynamic styling** based on component props.
  • Server‑side rendering works with proper configuration.

CSS‑in‑JS moves styling inside JavaScript, allowing styles to be parameterised by component props and enabling automatic scoping.

Styled‑Components (React example)

import styled from 'styled-components';

const Button = styled.button`
  background:${props => props.primary ? '#0066cc' : '#ccc'};
  color:#fff; padding:0.5rem 1rem; border:none;
  border-radius:4px;
  &:hover { opacity:0.9; }
`;

Emotion (alternative API – css prop)

/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';

function Card({title, children}) {
  return (
    <section css={css`
      background:#fff; border:1px solid #e0e0e0; padding:1rem;
      box-shadow:${'0 2px 4px rgba(0,0,0,0.1)'};
    `}>
      <h2>{title}</h2>{children}
    </section>
  );
}

Advantages

  • Dynamic theming – directly read values from React context or props.
  • Scoped – automatically generates unique class names.
  • Server‑side rendering – libraries provide a <StyleSheet> component to inject critical CSS into the HTML.

Drawbacks

  • Larger bundle size (runtime CSS parser).
  • Requires a build step (Babel plugin) for the css prop syntax.
  • Harder to audit with pure‑CSS tools unless you extract the generated CSS.

Theming with a Provider

import { ThemeProvider } from 'styled-components';
const theme = { primary:'#0066cc', danger:'#c00' };

<ThemeProvider theme={theme}> <App/> </ThemeProvider>

Inside any styled component:

background:${props => props.theme.primary};

Performance Tip – shouldForwardProp

By default, every prop is passed to the DOM element, which can cause warnings. Filter them:

const Button = styled('button', { shouldForwardProp: prop => prop !== 'primary' })`
  background:${p=>p.primary?'#0066cc':'#ccc'};
`;

Accessibility Checklist

  • Preserve focus outlines – :focus-visible works the same inside styled components.
  • Ensure color contrast; you can compute contrast dynamically using a utility function.
  • When using styled-components, the generated stylesheet is placed after the main CSS, so make sure you don’t unintentionally override critical rules.

When Not to Use

  • Very small sites where a build step adds unnecessary complexity.
  • Projects that already rely heavily on static CSS and want to stay out of the JavaScript bundle for performance.
Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS Modules (Scoped CSS)Continue to lesson 44CSS Houdini – The Paint APIBack to CSS Basics to Advanced curriculum