# Tailwind CSS 4 URL: /lynx/getting-started/styling/tailwind-css-4 Source: https://github.com/daangn/seed-design/blob/dev/docs/content/lynx/getting-started/styling/tailwind-css-4.mdx Guides PostCSS configuration and compatibility limitations for using Tailwind CSS v4 in Lynx. [Lynx official guide](https://lynxjs.org/rspeedy/styling#using-tailwind-css) covers integration of v3 and `@lynx-js/tailwind-preset`. That preset is not compatible with v4. [Rsbuild itself supports v4](https://rsbuild.rs/guide/styling/tailwindcss). The following configuration uses Crispy theme and Lynx post-processing without that preset. Existing v3 projects can maintain the [Tailwind CSS 3](/lynx/getting-started/styling/tailwind-css-3) configuration. Tailwind CSS v4 can be used with `@seed-design/tailwind4-theme` and Lynx PostCSS post-processing. The following is a configuration verified for color, typography, spacing, corners, and class replacement by status of basic Crispy utilities, and root theme switching. This does not mean full Tailwind v4 features or compatibility across all Lynx Engines. ## Requirements and Installation This guide is based on **Rsbuild 2 and Rspack 2 configuration introduced in Rspeedy 0.15 or later**. | Package | Condition | | ------------------------------------- | ------------------------------------------------------- | | `@lynx-js/rspeedy` | `0.15.0` or later, Rsbuild 2 and Rspack 2 based version | | `tailwindcss`, `@tailwindcss/postcss` | `4.x` | | `postcss` | `8.x` | Use ReactLynx and React plugin versions compatible with your chosen Rspeedy. Refer to the [Rspeedy upgrade guide](https://lynxjs.org/rspeedy/upgrade.html) for Node requirements and existing configuration changes. This criterion is the build environment to which the configuration below will be applied, and does not mean Tailwind 4 itself only works on Rsbuild 2. - npm: npm install @seed-design/lynx-css @seed-design/tailwind4-theme tailwindcss@4 @tailwindcss/postcss@4 postcss@8 - pnpm: pnpm add @seed-design/lynx-css @seed-design/tailwind4-theme tailwindcss@4 @tailwindcss/postcss@4 postcss@8 - yarn: yarn add @seed-design/lynx-css @seed-design/tailwind4-theme tailwindcss@4 @tailwindcss/postcss@4 postcss@8 - bun: bun add @seed-design/lynx-css @seed-design/tailwind4-theme tailwindcss@4 @tailwindcss/postcss@4 postcss@8 You don't need to pin to a specific Tailwind patch version. However, not all 4.x version Lynx compatibility has been verified, so verify the CSS post-processing and actual screens below after updating. Crispy tokens are provided via `@seed-design/lynx-css/base.css` and do not import web `@seed-design/css`. ## postcss.config.js Transform the generated CSS in `OnceExit` after Tailwind processing. This file is **Lynx app only** and should not be applied to web apps. ```js title="postcss.config.js" import tailwindcss from "@tailwindcss/postcss"; function lynxTailwindCompatibility() { return { postcssPlugin: "lynx-tailwind-compatibility", OnceExit(root) { root.walkRules((rule) => { // Transform only Tailwind theme variable declarations. // Find combinations of :root and :host regardless of whitespace and selector order. const selectors = rule.selectors; if ( selectors.length === 2 && selectors.includes(":root") && selectors.includes(":host") ) { rule.selector = ":root"; } }); // Do not reconstruct layer order. Expand content at declared positions. const layers = []; root.walkAtRules("layer", (rule) => { layers.push(rule); }); for (const rule of layers.reverse()) { if (rule.nodes) rule.replaceWith(rule.nodes); else rule.remove(); } }, }; } export default { plugins: [tailwindcss(), lynxTailwindCompatibility()], }; ``` Tailwind v4 outputs `@theme` variables to `:root, :host`. `:host` is a selector for web Shadow DOM. In the verified Lynx compiler, the combined rule was removed with `Unsupported selector ":host,:root" was removed during template encode` warning, causing theme variables to disappear as well. Therefore, we preserve `:root` and variable declarations, excluding only `:host`. This code is not a tool that converts `:host(...)` or arbitrary Shadow DOM CSS into Lynx CSS. ## CSS import ```css title="src/styles/global.css" @import "@seed-design/lynx-css/base.css"; @import "tailwindcss/theme.css" layer(theme); @import "@seed-design/tailwind4-theme"; @import "tailwindcss/utilities.css" layer(utilities); /* Traverses the src directory relative to this CSS file. */ @source "../"; ``` Import this CSS in the app entry. ```ts title="src/index.tsx" import "./styles/global.css"; ``` `@import "tailwindcss"` also includes web Preflight, so we load theme and utilities separately as shown above. Adjust the `@source` path to match the CSS file location. Classes must be written as full strings, like `selected ? "bg-bg-brand-solid" : "bg-bg-neutral-weak"`, so they can be detected at build time. ## Limitations and Items to Check - This configuration does not use `@lynx-js/tailwind-preset`. Rsbuild's v4 support alone does not guarantee Lynx CSS compatibility. - Expanding `@layer` removes the priority between layers and the layer order of `!important`. The selector priority and declaration order of the remaining CSS applies instead. In particular, re-check the result if you use Crispy recipes and utilities together, or add external layered CSS. This post-processing is not a general cascade layer polyfill. - Use the Crispy theme on the root ``. Overriding the theme in a sub-area can cause the color of the Tailwind v4 alias variable and a direct Crispy token reference to differ. See the [theming guide](/lynx/getting-started/styling/theming#using-with-tailwind-css). - Other Tailwind output such as `@property`, `@supports`, web-only selectors, units, and CSS functions is not transformed by this plugin. Check additional utilities such as shadow, ring, gradient, and transform on the target Engine when you use them. - Do not judge screen compatibility from a successful build alone. Verify color, text, spacing, state changes, and device theme switching on an actual host. ## Upgrading from Tailwind CSS 3 First, check whether your project's build tools meet the [requirements](#requirements-and-installation) above. ### 1. Replace Packages - npm: npm uninstall @seed-design/tailwind3-plugin npm install @seed-design/lynx-css @seed-design/tailwind4-theme tailwindcss@4 npm install -D @tailwindcss/postcss@4 postcss@8 - pnpm: pnpm remove @seed-design/tailwind3-plugin pnpm add @seed-design/lynx-css @seed-design/tailwind4-theme tailwindcss@4 pnpm add -D @tailwindcss/postcss@4 postcss@8 - yarn: yarn remove @seed-design/tailwind3-plugin yarn add @seed-design/lynx-css @seed-design/tailwind4-theme tailwindcss@4 yarn add --dev @tailwindcss/postcss@4 postcss@8 - bun: bun remove @seed-design/tailwind3-plugin bun add @seed-design/lynx-css @seed-design/tailwind4-theme tailwindcss@4 bun add --dev @tailwindcss/postcss@4 postcss@8 Use a version of `@seed-design/lynx-css` that matches the peer range of the theme package. If you're using `@lynx-js/tailwind-preset`, remove it from the configuration and dependencies. ### 2. Replace PostCSS and CSS - Replace the existing PostCSS `tailwindcss` plugin with the [PostCSS configuration](#postcssconfigjs) above. Also apply the Lynx post-processing. - Replace the `@tailwind` directives with the [CSS import](#css-import) above. It does not include web Preflight. - Match the `@source` path to your app's source location, and import the global CSS in the app entry. ### 3. Migrate Existing Configuration v4 does not automatically read `tailwind.config.ts`. If you only used the Crispy plugin and `content`, the theme import and `@source` above replace it. If you have custom configuration, migrate it to `@theme`, `@utility`, and `@plugin`, then remove the existing configuration file. For utility changes outside of Crispy, see the [Tailwind v4 upgrade guide](https://tailwindcss.com/docs/upgrade-guide). ### 4. Check the Screen Verify color, typography, spacing, state changes, and root theme switching in both the dev server and the production bundle. Also check that styles are reflected without a server restart when a new class is added. ## Color Crispy color tokens are used with Tailwind's `bg-`, `text-`, and `border-` prefixes. ```tsx // Background color // Text color Default text Brand text Error text // Border color ``` ## Typography Crispy typography tokens are provided as component classes. ```tsx Smallest text Default body Bold body Large heading Largest heading ``` ## Layout Use Tailwind's Flexbox utilities on native `` elements. ```tsx // Flex row (HStack) A B // Flex column (VStack) Item 1 Item 2 ``` ## Lynx Platform Notes - `overflow: scroll` is not supported. Use a `` element if scrolling is needed. - The default value of `position` is `relative` (unlike the web's `static`). - `` + `` are used for virtualized lists. `item-key` is required. - The CSS `initial`, `inherit`, and `unset` keywords are not supported. - The `inset` shorthand is not usable as of Lynx 3.7. Write the `top`, `right`, `bottom`, `left` longhand directly.