|
| 1 | +# Tailwind CSS v4 |
| 2 | + |
| 3 | +Analog supports Tailwind CSS v4 for both: |
| 4 | + |
| 5 | +- utility classes in templates |
| 6 | +- `@apply` inside Angular component styles |
| 7 | + |
| 8 | +The supported v3 `alpha` setup is: |
| 9 | + |
| 10 | +1. keep one root stylesheet such as `src/styles.css` |
| 11 | +2. put `@import 'tailwindcss';` in that stylesheet |
| 12 | +3. enable `@tailwindcss/vite` in `vite.config.ts` |
| 13 | +4. keep a `postcss.config.mjs` with `@tailwindcss/postcss` |
| 14 | +5. configure Analog with `tailwindCss.rootStylesheet` |
| 15 | + |
| 16 | +Generated apps already follow this shape. |
| 17 | + |
| 18 | +## Install |
| 19 | + |
| 20 | +```sh |
| 21 | +npm install -D tailwindcss @tailwindcss/vite @tailwindcss/postcss postcss |
| 22 | +``` |
| 23 | + |
| 24 | +## Vite Config |
| 25 | + |
| 26 | +```ts |
| 27 | +/// <reference types="vitest" /> |
| 28 | + |
| 29 | +import { resolve } from 'node:path'; |
| 30 | +import { defineConfig } from 'vite'; |
| 31 | +import analog from '@analogjs/platform'; |
| 32 | +import tailwindcss from '@tailwindcss/vite'; |
| 33 | + |
| 34 | +export default defineConfig(() => ({ |
| 35 | + plugins: [ |
| 36 | + analog({ |
| 37 | + vite: { |
| 38 | + tailwindCss: { |
| 39 | + rootStylesheet: resolve(__dirname, 'src/styles.css'), |
| 40 | + }, |
| 41 | + }, |
| 42 | + }), |
| 43 | + tailwindcss(), |
| 44 | + ], |
| 45 | +})); |
| 46 | +``` |
| 47 | + |
| 48 | +Use an absolute `rootStylesheet` path. Analog may serve component styles through virtual stylesheet ids during dev, so relative `@reference` paths are not reliable there. |
| 49 | + |
| 50 | +If you are using `@analogjs/vite-plugin-angular` directly instead of `@analogjs/platform`, the same Tailwind option lives on the Angular plugin itself: |
| 51 | + |
| 52 | +```ts |
| 53 | +import { resolve } from 'node:path'; |
| 54 | +import { defineConfig } from 'vite'; |
| 55 | +import angular from '@analogjs/vite-plugin-angular'; |
| 56 | +import tailwindcss from '@tailwindcss/vite'; |
| 57 | + |
| 58 | +export default defineConfig(() => ({ |
| 59 | + plugins: [ |
| 60 | + angular({ |
| 61 | + tailwindCss: { |
| 62 | + rootStylesheet: resolve(__dirname, 'src/styles.css'), |
| 63 | + }, |
| 64 | + }), |
| 65 | + tailwindcss(), |
| 66 | + ], |
| 67 | +})); |
| 68 | +``` |
| 69 | + |
| 70 | +## Root Stylesheet |
| 71 | + |
| 72 | +In `src/styles.css`: |
| 73 | + |
| 74 | +```css |
| 75 | +@import 'tailwindcss'; |
| 76 | +``` |
| 77 | + |
| 78 | +You can keep your theme, `@source`, plugins, and prefixes there as well: |
| 79 | + |
| 80 | +```css |
| 81 | +@import 'tailwindcss' prefix(tw); |
| 82 | + |
| 83 | +@source './src'; |
| 84 | + |
| 85 | +@theme { |
| 86 | + --color-primary: #3b82f6; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## PostCSS Config |
| 91 | + |
| 92 | +Create `postcss.config.mjs`: |
| 93 | + |
| 94 | +```js |
| 95 | +export default { |
| 96 | + plugins: { |
| 97 | + '@tailwindcss/postcss': {}, |
| 98 | + }, |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +Keep this even if dev already works with `@tailwindcss/vite`. Current Analog builds still rely on the PostCSS path for production CSS processing. |
| 103 | + |
| 104 | +## How Component Styles Work |
| 105 | + |
| 106 | +Angular compiles component styles in isolation. When a component stylesheet contains `@apply`, Tailwind still needs access to the root stylesheet that defines prefixes, theme values, and plugins. |
| 107 | + |
| 108 | +Analog handles that by: |
| 109 | + |
| 110 | +- detecting Tailwind usage in component CSS |
| 111 | +- injecting the correct `@reference` to the configured root stylesheet |
| 112 | +- externalizing component styles during dev when needed so they flow through Vite's CSS pipeline |
| 113 | +- preserving the build path through PostCSS for production |
| 114 | + |
| 115 | +That means you should not manually add `@reference` to every component stylesheet in the normal setup. |
| 116 | + |
| 117 | +## Plugin Order |
| 118 | + |
| 119 | +List `analog()` before `tailwindcss()` in your Vite config. That is now how the generators scaffold it. |
| 120 | + |
| 121 | +```ts |
| 122 | +plugins: [analog({ vite: { tailwindCss: { ... } } }), tailwindcss()]; |
| 123 | +``` |
| 124 | + |
| 125 | +This keeps the config aligned with the generated apps and the current documentation. |
| 126 | + |
| 127 | +## HMR |
| 128 | + |
| 129 | +Prefer `hmr` over `liveReload` when you need to configure Angular HMR explicitly. `liveReload` remains a compatibility alias. |
| 130 | + |
| 131 | +Tailwind support does not require you to enable HMR manually. The stylesheet pipeline is handled independently from whether Angular can produce a hot component update for a given edit. |
| 132 | + |
| 133 | +## Prefixes |
| 134 | + |
| 135 | +If your component styles use custom-prefixed utilities, configure `prefixes` so Analog knows which stylesheets need Tailwind `@reference` injection: |
| 136 | + |
| 137 | +```ts |
| 138 | +analog({ |
| 139 | + vite: { |
| 140 | + tailwindCss: { |
| 141 | + rootStylesheet: resolve(__dirname, 'src/styles.css'), |
| 142 | + prefixes: ['tw:'], |
| 143 | + }, |
| 144 | + }, |
| 145 | +}); |
| 146 | +``` |
| 147 | + |
| 148 | +Without `prefixes`, Analog falls back to its default Tailwind usage detection for component styles. |
| 149 | + |
| 150 | +## Generated Apps |
| 151 | + |
| 152 | +Current `create-analog` and Nx app scaffolds both generate: |
| 153 | + |
| 154 | +- `@import 'tailwindcss';` in `src/styles.css` |
| 155 | +- `@tailwindcss/vite` in `vite.config.ts` |
| 156 | +- `postcss.config.mjs` with `@tailwindcss/postcss` |
| 157 | + |
| 158 | +If you start from a generated app, keep that structure unless you have a specific reason to diverge from the supported path. |
| 159 | + |
| 160 | +## Related |
| 161 | + |
| 162 | +- [Using CSS Pre-processors](/docs/packages/vite-plugin-angular/css-preprocessors) |
| 163 | +- [create-analog](/docs/packages/create-analog/overview) |
0 commit comments