New
You're viewing older docs for version 7.0. View latest docs

Styling and CSS

There are many ways to include CSS in a web application, and correspondingly there are many ways to include CSS in Storybook. Usually, it is best to try and replicate what your application does with styling in Storybook’s configuration. To make this easier, we recommend using @storybook/addon-styling.

Importing CSS files

Storybook is pre-configured to recognize imports for CSS files. To add global CSS for all your stories, import it in .storybook/preview.js.

If your component files import their CSS files, this will work too. The noticeable exception to this is if you're using CSS processor tools like Sass or Postcss.

CSS processors

If you're using Vite as your builder, you're covered! Vite supports Sass and PostCSS out-of-the-box 🎉

However, if you're using Webpack and want to use Sass and PostCss, you'll need some extra configuration. We recommend installing @storybook/addon-styling to help you configure these tools. Or if you'd prefer, you can customize Storybook's webpack configuration yourself to include the appropriate loader(s).

CSS-in-JS

CSS-in-JS libraries are designed to use basic JavaScript, and they often work in Storybook without any extra configuration. Some libraries expect components to render in a specific rendering “context” (for example, to provide themes), which can be accomplished with @storybook/addon-styling's withThemeFromJSXProvider decorator.

Adding webfonts

If you need webfonts to be available, you may need to add some code to the .storybook/preview-head.html file. We recommend including any assets with your Storybook if possible, in which case you likely want to configure the static file location.

Troubleshooting

Styles aren't being applied with Angular

The latest Angular releases introduced significant changes in configuring and styling projects. If you're working with an Angular version greater than 13 and your styles aren't being applied, you may need to check your angular.json ad adjust the builder configuration to import your CSS:

Loading...

Additionally, if you need Storybook-specific styles that are separate from your application, you can configure the styles with Storybook's custom builder, which will override the application's styles:

Loading...

NX component libraries not loading styles

If you're working with Storybook and Nx libraries, you can extend your project's configuration (i.e., project.json) and provide the application's styles.

For earlier Nx versions (before 14.1.8), your configuration would look like this:

Loading...

Starting with version 14.1.8, Nx uses the Storybook builder directly, which means any configuration supplied to the builder also applies to the NX setup. If you're working with a library, you'll need to configure the styling options ( e.g., preprocessors) inside the build-storybook options configuration object. For example:

Loading...

When Nx runs, it will load Storybook's configuration and styling based on storybook's browserTarget.

Was this page helpful?

Markdown accepted ([link text](url), _italic_, **bold**, etc). Your anonymous feedback will be posted publicly on GitHub.

✍️ Edit on GitHub – PRs welcome!
Storybook
Join the community
6,556 developers and counting
WhyWhy StorybookComponent-driven UI
DocsGuidesTutorialsChangelogTelemetryStatus
CommunityIntegrationsGet involvedBlog
ShowcaseExploreProjectsComponent glossary
Open source software
Storybook

Maintained by
Chromatic
Special thanks to Netlify and CircleCI