← Back to React Course | Chapter 11: Styling in React | Lesson 2 of 8

Styled Components

styled-components is like writing your CSS directly onto a sticky note attached to each component, instead of keeping styles in a separate binder across the room.

Writing CSS Directly in JavaScript

styled-components lets you define a component's styling using a tagged template literal of real CSS syntax, attached directly to a styled.tagname call. The result, like StyledButton, is a genuine React component you render just like any other.

Note: Name styled components with a clear prefix or suffix, like StyledButton, to distinguish them at a glance from plain components.

Warning: It's an npm package — the special styled.div`` syntax needs the library's Babel/runtime processing and won't work standalone in this CDN-only sandbox.

Example: Writing CSS Directly in JavaScript

markup
// Run in your local React project (npm install required)
import styled from 'styled-components';

const StyledButton = styled.button`
  background: blue;
  color: white;
  padding: 8px 16px;
`;

function App() {
  return <StyledButton>Click me</StyledButton>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Using Props for Dynamic Styling

Inside the template literal, you can embed a function that receives the component's props and returns a CSS value, letting the same styled component render differently based on what props it's given, without writing separate CSS classes.

Note: Keep the prop-based logic simple inside the template literal — for complex conditional styling, consider computing a value beforehand and passing just that.

Warning: A prop like primary passed to a styled component also gets forwarded to the underlying DOM element by default in older versions, which can cause React 'unknown prop' warnings — check your styled-components version's behavior.

Example: Using Props for Dynamic Styling

markup
// Run in your local React project (npm install required)
import styled from 'styled-components';

const StyledButton = styled.button`
  background: ${props => props.primary ? 'blue' : 'gray'};
  color: white;
`;

function App() {
  return <StyledButton primary>Primary Button</StyledButton>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Nesting and Pseudo-Selectors

Unlike inline styles, styled-components' template literals support the FULL power of real CSS, including nested selectors and pseudo-classes like &:hover, using the '&' symbol to refer back to the component itself.

Note: This nesting support is one of styled-components' biggest advantages over plain inline React styles, which can't express hover/focus states at all.

Warning: The '&' must refer to the component's own root element — nesting other unrelated selectors deeply can make styles harder to trace back to their component.

Example: Nesting and Pseudo-Selectors

markup
// Run in your local React project (npm install required)
import styled from 'styled-components';

const StyledButton = styled.button`
  background: blue;
  &:hover {
    background: darkblue;
  }
`;

function App() {
  return <StyledButton>Hover me</StyledButton>;
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Common Mistakes
  1. Defining a styled component inside another component's render function, recreating it (and losing any internal state) on every render.
  2. Forgetting that styled.div``... creates an actual React component, usable just like any other, with its own props.
  3. Not realizing template-literal CSS supports full nesting and pseudo-selectors (like &:hover), not just flat properties.
Chapter Summary
  • styled-components lets you write actual CSS inside JavaScript template literals, attached to a specific component.
  • styled.div... (or styled.button, styled.p, etc.) returns a new React component with those styles baked in.
  • Props can be used inside the template literal to make styles conditional/dynamic.
  • Styled components must be defined at module scope, not recreated inside another component's render.
Browser Support

Requires npm install styled-components — not available via CDN in this sandbox; works with React 16.8+.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.