Styled-components is not the only CSS-in-JS library. There are others, each with unique advantages. Let's explore two of the most important ones: Emotion and vanilla-extract.
Emotion is a CSS-in-JS library that offers two ways of using it:
@emotion/react)1/** @jsxImportSource @emotion/react */
2import { css } from '@emotion/react';
3
4function SpacePanel({ isActive }) {
5 return (
6 <div css={css`
7 background: #0d1137;
8 border: 2px solid ${isActive ? '#00e676' : '#3949ab'};
9 border-radius: 12px;
10 padding: 20px;
11 transition: border-color 0.3s ease;
12 `}>
13 <h2>Navigation Panel</h2>
14 </div>
15 );
16}@emotion/styled)1import styled from '@emotion/styled';
2
3const Card = styled.div`
4 background: #1a1a4e;
5 padding: 20px;
6 border-radius: 12px;
7`;
8
9// Or object syntax:
10const CardObj = styled.div({
11 background: '#1a1a4e',
12 padding: 20,
13 borderRadius: 12,
14});css propVanilla-extract is a completely different approach: styles are generated at compile time, not at runtime. Zero JavaScript in the client bundle!
1// styles.css.ts
2import { style, globalStyle } from '@vanilla-extract/css';
3
4export const panel = style({
5 background: '#0d1137',
6 border: '2px solid #3949ab',
7 borderRadius: 12,
8 padding: 20,
9 ':hover': {
10 borderColor: '#7c4dff',
11 },
12});
13
14export const title = style({
15 color: '#7c4dff',
16 fontSize: 24,
17});1// Component.tsx
2import { panel, title } from './styles.css.ts';
3
4function SpacePanel() {
5 return (
6 <div className={panel}>
7 <h2 className={title}>Panel</h2>
8 </div>
9 );
10}| Feature | Styled-components | Emotion | Vanilla-extract | |---------|-------------------|---------|-----------------| | Runtime | Yes | Yes | No (zero-runtime) | | Bundle | Larger | Medium | Minimal | | TypeScript | Good support | Good support | Native TS | | SSR | Requires configuration | Requires configuration | Seamless | | DX | Very good | Very good | Requires build |
The choice of CSS-in-JS library depends on several factors:
Choose styled-components when:
Choose Emotion when:
css prop for quickly adding inline-like styles with the full power of CSSChoose vanilla-extract when:
More and more projects are moving to zero-runtime solutions like vanilla-extract, Panda CSS, or Linaria. The reason? In large applications, runtime CSS-in-JS (styled-components, Emotion) can slow down rendering because it generates styles in JavaScript while the application is running.
Zero-runtime generates CSS classes at compile time -- it's like preparing a navigation map before launch instead of calculating the route in flight. The result: faster page loading and better Core Web Vitals scores.
1// Panda CSS -- another popular zero-runtime alternative
2import { css } from '../styled-system/css';
3
4function Panel() {
5 return (
6 <div className={css({
7 bg: 'slate.900',
8 p: '4',
9 rounded: 'xl',
10 border: '2px solid',
11 borderColor: 'indigo.700'
12 })}>
13 Panel content
14 </div>
15 );
16}Regardless of which library you choose, the key is to stick with one approach within a project and establish conventions with your team.