Learn how to use various styling libraries in your project, including Tailwind, styled-components, styled-jsx, and CSS Modules.
In this guide, learn about the different ways to include styling in your CMS React project. You can also refer to the styling example project for working examples of supported styling methods, including a number of major styling libraries. Below, learn more about each of the following methods:
Tailwind CSS is a framework designed to speed up the building process by providing utility classes for styling your markup without needing to write custom CSS. You can learn more about Tailwind CSS on HubSpot’s blog.To configure Tailwind for your components:
Add a PostCSS configuration file to your project subcomponent, then import the Tailwind configuration file and include Tailwind as a plugin along with any Tailwind-specific configuration.
import tailwind from "tailwindcss";import autoprefixer from "autoprefixer";import postcssNested from "postcss-nested";import tailwindConfig from "./tailwind.config.js";export default { plugins: [tailwind(tailwindConfig), postcssNested, autoprefixer()],};
Create a tailwind.css file in your styles directory. Then, import that file in your top-level component with the base Tailwind layer directives.
styled-components is a library that enables you to write CSS in JavaScript and inject styles dynamically. Learn more about the basics of styled-components.To use styled-components in your project:
Add the styled-components dependency to your package.json file in the JavaScript asset folder.
For each island you’re using, you must wrap each subtree in a registry to capture styles when rendering on the server. To make this easier, you may use the Wrapper prop on the Island component to wrap the contents without needing to edit the island components themselves. Note that this prop also lets you configure this once by replacing all instances of <Island /> with a <StyledIsland /> component that looks similar to the following:
import { Island } from "@hubspot/cms-components";import StyledComponentsRegistry from "./StyledComponentsRegistry?client";export default function StyledIsland(props) { return <Island {...props} Wrapper={StyledComponentsRegistry} />;}
Please note:As shown above, when using the Wrapper prop you must import the component with a ?client suffix to make sure it can be bundled for the client.
You can now import styled from 'styled-components' and use it to style your components.
import { useState, useId } from "react";import { styled } from "styled-components";const StyledButton = styled.button` padding: ${props => 10 + props.$count * 10}px;`;export default function InteractiveStyledComponent() { const id = useId(); const [count, setCount] = useState(0); return ( <StyledButton $count={count} onClick={() => setCount(prevCount => prevCount + 1)}> Current count is: {count} {id} </StyledButton> );}
styled-jsx is a library that enables you to write encapsulated and scope CSS to style your components. By setting styles at the individual component level, you can add, change, and delete styles for one component without impacting others.To use styled-jsx in your project:
Add the styled-jsx-components dependency to your package.json file in the JavaScript asset folder.
The styled-jsx registry component must also be wrapped on any <Island> usage to prevent hydration mismatches or missing styles on initial load. Note that styled-jsx's implementation depends on useId() hooks in a way that can cause mismatches if not properly configured. See the StyledJSXIsland.jsx example file for an easier pattern of replacing all direct <Island /> usage.
import { Island } from "@hubspot/cms-components";import StyledComponentsRegistry from "./StyledComponentsRegistry?client";export default function StyledIsland(props) { return <Island {...props} Wrapper={StyledComponentsRegistry} />;}
You can now use styled-jsx to style your components, including <style jsx>{`/_ CSS here _/`}</style> patterns. See example below, which is copied from the InteractiveStyledJSXComponent.jsx example file.
CSS Modules is a CSS file where all class names and animation names are scoped locally by default. You can use CSS Modules within any React component by importing a file ending in .module.css, which will return a CSS module object:
import classes from './example.module.css';export default function MyComponent() { return <div className={classes.red}>red text</div>;}
.red { color: red;}/* How to have global—non-namespaced—styles in CSS modules */:global(html) { border: 6px solid SteelBlue;}
When you import a CSS modules file from inside a React component:
A <style> tag will automatically be inserted into the page when the component is rendered server-side or when it’s dynamically rendered in the browser.
Those styles will automatically be namespaced so they don’t interfere with anything else on the page.
You can also import regular CSS files into your React components, but their selectors will not be automatically namespaced.
CMS React projects support any CSS-in-JS libraries that provide a server-side rendering API and don’t depend on a Babel plugin. The same registry patterns described above can be generalized for other libraries to emit CSS to include as part of the server-side render. You’ll also need to include the registry as a Wrapper on any <Island> component if there are styles within the island.
Last modified on February 10, 2026
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.