Skip to content
tezvyn:
Menu

Emotion: Component-Scoped Styles in JavaScript

Source: emotion.shMediumHow cards are made

Emotion lets you write CSS directly in your JavaScript, keeping styles and logic colocated. It's ideal for dynamic, component-scoped styles in React that avoid global conflicts.

Why it exists

Traditional CSS lives in global files, leading to naming collisions, dead code, and difficulty managing styles tied to a component's state. CSS-in-JS libraries like Emotion were created to scope styles directly to components and leverage JavaScript's power for dynamic styling.

The mental model

Think of Emotion as bringing your CSS stylesheet directly into your component file. Instead of referencing an external class name like className="primary-button", you define the styles for that button right where you use it, using JavaScript variables, functions, and props to make them dynamic.

How it works

Emotion parses your JavaScript-written CSS (either as template literals or objects) at runtime or build time. It generates unique, scoped class names (e.g., css-1q2w3e4) and injects the corresponding CSS rules into the document's <head>. This ensures styles don't leak and only the CSS for rendered components is loaded. It supports two main patterns: the css prop for applying styles directly to an element, and the styled factory for creating reusable styled components (e.g., styled.button).

When to use it

Use Emotion in component-based applications (especially React) where you need dynamic styling based on component state or props. It's excellent for building design systems with theming, as components can easily consume theme values. It also simplifies server-side rendering (SSR) of styles, often with zero configuration in modern React frameworks.

When not to use it

For static sites with simple, non-dynamic styling, Emotion can be overkill; traditional CSS or a utility-first framework might be simpler and have less runtime overhead. If your team is not comfortable with JavaScript or prefers a strict separation of concerns between JS, HTML, and CSS, it might not be a good cultural fit.

One canonical example

A common use case is creating a themed button component. Using the styled API, you can define a component that accepts a primary prop. If the prop is true, it pulls a color from a theme object; otherwise, it uses a default. For example: const Button = styled.button(props => ({ backgroundColor: props.primary ? props.theme.colors.primary : 'grey' }));. This creates a <Button primary /> component that is self-contained and reusable across an application.

Interview question

How does Emotion primarily ensure that component styles are scoped and avoid global conflicts?

  • a.By enforcing strict naming conventions for CSS classes to prevent accidental overrides.
  • b.By generating unique, component-specific class names and injecting their rules into the document's head.Correct
  • c.By requiring developers to define all styles using inline style attributes on HTML elements.
  • d.By compiling all component styles into a single, optimized CSS file during the build process.
Why?

Emotion prevents style conflicts by generating unique, scoped class names for each component's styles and injecting these rules into the document's <head>. While inline styles (option C) can prevent conflicts, Emotion's core mechanism involves generated class names, not exclusively inline styles.

Just read this? Test yourself on what you have been reading.

Read the original → emotion.sh

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles