Get started
Install Trousers and the adapter for your bundler. Trousers extracts CSS at build time and ships class names alongside your JavaScript.
Install
npm install trousers @trousers/react
npm install -D esbuild @trousers/esbuild
Configure esbuild
import { build } from 'esbuild';
import trousers from '@trousers/esbuild';
await build({
entryPoints: ['src/app.tsx'],
bundle: true,
outdir: 'public',
jsx: 'automatic',
jsxImportSource: '@trousers/react',
plugins: [trousers()],
});
Load the emitted stylesheet alongside the JavaScript entry.
<link rel="stylesheet" href="/app.css" />
<script src="/app.js" defer></script>
For TypeScript, follow the JSX configuration guide. With the standard React JSX runtime, use classnames.
Write a style
import { css } from 'trousers';
const button = css({ padding: '8px 12px', borderRadius: 6 })
.modifier('primary', { backgroundColor: '#f95b5b' });
export function SaveButton({ primary }: { primary: boolean }) {
return <button css={button} $primary={primary}>Save</button>;
}
Pass values computed at runtime, such as a measured width, through the style prop. The compiled style objects stay static.
Styling guide · Bundlers and SSR · Next.js example · Motivation