> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Tailwind CSS

Style Lynx elements with [Tailwind CSS](https://tailwindcss.com/) utility classes. New apps get **Tailwind CSS 4**, set up the way Angular CLI sets it up. Apps made before keep **Tailwind CSS 3** with Lynx's preset, which still works.

## Add Tailwind

`npm create angular-lynx` asks whether to add Tailwind, and adds it unless you say no. To add it to an existing app, run the schematic:

```bash
ng generate @blotch/angular-lynx:add-tailwind
```

It sets up Tailwind 4, as `ng new --style=tailwind` does:

- Installs `tailwindcss` and `@tailwindcss/postcss` as dev dependencies.
- Writes `.postcssrc.json`, which runs `@tailwindcss/postcss`.
- **Replaces** `src/styles.css` with Tailwind's imports and Lynx's own utilities. Copy out any global styles you want to keep first.

An app that already depends on Tailwind 3 keeps it. Pass `--tailwind-version=3` or `4` to choose.

### Set it up by hand

1. Install Tailwind 4:

   ```bash
   npm install -D tailwindcss @tailwindcss/postcss
   ```

2. Run its PostCSS plugin:

   ```json title=".postcssrc.json"
   {
     "plugins": {
       "@tailwindcss/postcss": {}
     }
   }
   ```

3. Import Tailwind in your global stylesheet, then Lynx's own utilities:

   ```css title="src/styles.css"
   @layer theme, base, components, utilities;
   @import 'tailwindcss/theme.css' layer(theme);
   @import 'tailwindcss/utilities.css' layer(utilities);
   @import '@blotch/angular-lynx/tailwind.css';
   ```

This imports Tailwind without **Preflight**, its reset for HTML elements such as `h1` and `button`. A Lynx app has none of them, and the web preview would apply the reset where the device doesn't. The last import adds the [Lynx utilities and variants](#lynx-utilities-and-variants).

:::tip From `ng new --style=tailwind`
Running `ng add @blotch/angular-lynx` in an app made that way keeps its Tailwind 4 and its `.postcssrc.json`, and replaces `@import 'tailwindcss';` with the imports above.
:::

## Use utility classes

```typescript title="src/app/counter.ts"
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-counter',
  imports: [LYNX_ELEMENTS],
  template: `
    <view class="flex flex-col items-center gap-3 p-4">
      <text class="text-4xl font-bold text-slate-900">{{ count() }}</text>
      <view
        class="flex w-full justify-center rounded-lg bg-blue-600 p-4"
        (bindtap)="increment()"
      >
        <text class="font-semibold text-white">Add one</text>
      </view>
    </view>
  `,
})
export class Counter {
  readonly count = signal(0);

  increment(): void {
    this.count.update((n) => n + 1);
  }
}
```

- **Put text classes on the `<text>`.** `text-white` on the blue `<view>` wouldn't color its label, because only `font-size` inherits on Lynx by default. See [Inheritance](/guide/styling/inheritance.md).
- **Rows need `flex flex-row`.** Elements stack in a column by default, and `flex-row` alone does nothing.
- **Sizes run 12.5% smaller than on the web.** Tailwind's scale uses `rem`, which is 14px on Lynx. For an exact size, use an arbitrary `px` value such as `w-[44px]`.
  - With the [UI components' theme](/dolan/theming.md), `1rem` is 16px, so sizes match the web.


## Lynx utilities and variants

`@blotch/angular-lynx/tailwind.css` adds what Lynx's Tailwind 3 preset adds for Lynx's own CSS. `ng add` imports it. Tailwind writes CSS only for the classes you use, so the rest cost nothing.

| Class                                    | CSS                                                 |
| ---------------------------------------- | --------------------------------------------------- |
| `linear`                                 | `display: linear`, Lynx's default layout            |
| `display-relative`                       | `display: relative`                                 |
| `normal`, `ltr`, `rtl`, `lynx-rtl`       | `direction`                                         |
| `solo-translate-{x,y,z}-*`               | `transform: translateX(…)`, one function            |
| `solo-rotate-*`, `solo-rotate-{x,y,z}-*` | `transform: rotate(…)`                              |
| `solo-scale-*`, `solo-scale-{x,y}-*`     | `transform: scale(…)`, where `solo-scale-50` halves |
| `solo-skew-*`, `solo-skew-{x,y}-*`       | `transform: skew(…)`                                |

The `solo-*` classes each set the whole `transform` with one function, without Tailwind's variables. Lynx before 3.4 can't animate a variable, so use them to animate a transform there. Two on one element don't combine, and neither do they with `translate-*` or `rotate-*`. Each takes a negative (`-solo-rotate-45`) and an arbitrary value (`solo-translate-x-[10px]`).

### State variants

Tailwind's `data-*` and `aria-*` variants work, Tailwind 3's and 4's, with `group-*`, `peer-*` and `in-*`: `data-[state=open]:bg-primary`, `aria-expanded:rotate-90`, `group-data-[state=open]:text-foreground`. Lynx matches no attribute selector, so the build turns each into a class the runtime gives the element while its attribute matches (see [Attribute selectors](/guide/styling/index.md#attribute-selectors)).

```html
<view
  class="bg-muted data-[state=open]:bg-primary"
  [attr.data-state]="open() ? 'open' : 'closed'"
></view>
```

The `ui-*` variants style a class instead of an attribute. Give the element a `ui-*` class for its state, and style it with the variant of the same name:

```html
<view class="group" [class.ui-open]="open()">
  <view class="bg-muted parent-ui-open:bg-primary"></view>
  <text class="text-muted-foreground group-ui-open:text-foreground">
    Details
  </text>
</view>
```

- **`ui-<state>:`** applies while the element has the `ui-<state>` class. The states are `active`, `disabled`, `readonly`, `checked`, `selected`, `indeterminate`, `invalid`, `initial`, `open`, `closed`, `leaving`, `entering`, `animating`, `busy`, `focused`, `complete`, `filled` and `dragging`, plus `ui-side-{left,right,top,bottom}` and `ui-align-{start,end,center}`.
- **`parent-ui-<state>:`** applies while the element's direct parent has it.
- **Tailwind's `group-*`, `peer-*` and `in-*`** combine with them: `group-ui-open:` under a `group ui-open` ancestor, `peer-ui-checked:` after a `peer ui-checked` sibling, and named groups such as `group-ui-open/menu:`.
- **For a state of your own**, add a variant in `src/styles.css`, after the import:

  ```css title="src/styles.css"
  @custom-variant ui-expanded (&.ui-expanded);
  ```

## How Tailwind 4 works on Lynx

Tailwind 4 writes CSS for current browsers: `oklch()` colors, cascade layers, `color-mix()` for opacity modifiers, `padding-inline` for `px-*`, and the `translate`, `rotate` and `scale` properties. Lynx drops all of these, while the web preview, which runs in a browser, shows them.

So the build rewrites the CSS Tailwind writes into CSS with the same meaning that Lynx reads, for the device and the web preview alike:

| Tailwind 4 writes                  | Lynx gets                                     |
| ---------------------------------- | --------------------------------------------- |
| `oklch(63.7% 0.237 25.331)`        | `rgb(251, 44, 54)`                            |
| `color-mix(in oklab, #000 50%, …)` | `rgba(0, 0, 0, 0.5)`                          |
| `padding-inline: …`                | `padding-left` and `padding-right`            |
| `translate`, `rotate`, `scale`     | one `transform` they all compose into         |
| `opacity: 50%`                     | `opacity: 0.5`                                |
| `@layer`, nesting, `@property`     | plain rules, with the variables' start values |
| `:where(.group).ui-open .x`        | `.group.ui-open .x`                           |

It also leaves out the properties and values Lynx doesn't have, such as `text-transform`, so the web preview doesn't show styles the device won't. There's nothing to configure: the build does this for every stylesheet Tailwind 4 compiles.

## Dark mode

`dark:` classes apply while the app is drawn dark, with Tailwind 3 and 4:

```html
<view class="bg-white dark:bg-zinc-900">
  <text class="text-zinc-900 dark:text-white">Hello in either theme</text>
</view>
```

- **They switch while the app is open**, when the phone changes appearance or the app calls `setColorScheme()`. See [Dark mode](/guide/dark-mode.md).
- **Keep Tailwind's own `dark` variant.** It writes a `prefers-color-scheme` query, which the build turns into rules under `page.dark`. A `@custom-variant dark` copied from a web app, such as `(&:where(.dark, .dark *))`, writes selectors Lynx can't match. Delete it.
- **With the [UI components](/dolan/index.md), you rarely need `dark:`.** Their classes, such as `bg-background` and `text-foreground`, switch by themselves.

## Where Tailwind finds classes

Tailwind 4 scans your project's files for classes itself, `.html` templates and `.ts` files alike, and skips what `.gitignore` lists. To scan a folder outside the project, such as a library in your workspace, add it with [`@source`](https://tailwindcss.com/docs/detecting-classes-in-source-files):

```css title="src/styles.css"
@source '../../libs/ui/src';
```

## Mix with component styles

Tailwind classes come from the global stylesheet, so they combine with a component's own `styles`:

```typescript title="src/app/badge.ts"
import { Component } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-badge',
  imports: [LYNX_ELEMENTS],
  template: `
    <view class="app-badge self-start rounded-full bg-slate-800 px-3 py-1">
      <text class="text-xs text-white">New</text>
    </view>
  `,
  styles: `
    .app-badge {
      border-width: 1px;
      border-style: solid;
      border-color: #dd0031;
    }
  `,
})
export class Badge {}
```

The component's own rules are scoped to its template, as on the web. See [Component styles](/guide/styling/component-styles.md).

## What doesn't work

These are the gaps you'll notice first:

- **The `hover:` and `focus:` variants** don't work on a phone: Lynx has no `:hover` or `:focus`. `sm:`, `md:` and the other breakpoints do, against the page's size (see [Adaptive layouts](/guide/adaptive-layouts.md#media-rules)), and so does `dark:`: see [Dark mode](#dark-mode).
- **`inline` and `inline-block`** have no effect. Lynx has no flow layout.
- **`overflow-auto` and `overflow-scroll`** don't scroll a `<view>`. Use `<scroll-view>`.
- **`uppercase`, `lowercase` and `capitalize`** do nothing. Lynx has no `text-transform`.
- **Two filters on one element**, such as `blur-sm grayscale`, don't combine. Lynx takes one filter function.
- **`ring-offset-*` doesn't widen a ring**, and **`drop-shadow-<color>`** doesn't color a drop shadow.

[CSS on Lynx](/guide/styling/index.md) lists every CSS difference.

## Tailwind 3

Apps made before Tailwind 4 support use Tailwind 3 with **`@lynx-js/tailwind-preset`**, which limits Tailwind's utilities to the CSS Lynx supports and adds the [Lynx utilities and variants](#lynx-utilities-and-variants). They keep working: the build runs Tailwind 3 when it finds a `tailwind.config.ts`.

```typescript title="tailwind.config.ts"
import type { Config } from 'tailwindcss';
import preset from '@lynx-js/tailwind-preset';

const config: Config = {
  content: ['./src/**/*.{html,ts}'],
  presets: [preset],
};

export default config;
```

```css title="src/styles.css"
@tailwind base;
@tailwind utilities;
```

To set Tailwind 3 up in a new app, run `ng generate @blotch/angular-lynx:add-tailwind --tailwind-version=3`. Apps made with an older version have `content: ['./src/**/*.ts']`, which misses `.html` templates; `ng update @blotch/angular-lynx` adds `.html` to it.

### Move to Tailwind 4

1. Run `ng generate @blotch/angular-lynx:add-tailwind --tailwind-version=4`. It installs Tailwind 4, writes `.postcssrc.json` and replaces `src/styles.css`.
2. Move your config's `theme.extend` into [`@theme`](https://tailwindcss.com/docs/theme) in `src/styles.css`, then delete `tailwind.config.ts` and uninstall `@lynx-js/tailwind-preset`.
3. Rename the utilities Tailwind 4 renamed, such as `shadow-sm` to `shadow-xs`. See Tailwind's [upgrade guide](https://tailwindcss.com/docs/upgrade-guide#renamed-utilities).
4. If you use the [UI components](/dolan/index.md), import the theme in `src/styles.css`, after Tailwind's imports. Step 1 replaced the file, and Tailwind 4 takes the theme's classes from its `tailwind.css`:

   ```css title="src/styles.css"
   @layer theme, base, components, utilities;
   @import 'tailwindcss/theme.css' layer(theme);
   @import 'tailwindcss/utilities.css' layer(utilities);
   @import '@blotch/angular-lynx/tailwind.css';
   @import './styles/default.css';
   @import './styles/dark.css';
   @import './styles/tailwind.css';
   ```

   Keep them above the `@theme` from step 2: CSS ignores an `@import` that comes after other rules. The preset's `ui-*` and `solo-*` classes keep working through `@blotch/angular-lynx/tailwind.css`. For a state you added with `createLynxPreset()`'s `uiVariants` option, add a `@custom-variant` (see [State variants](#state-variants)). Then run `npx dolan update`. It copies `tailwind.css` into the theme and deletes the Tailwind 3 plugin, `tailwind-plugin.ts`, with its test. It asks first about a file you edited.

Tailwind's own `npx @tailwindcss/upgrade` can't read a config that uses the Lynx preset, so remove the preset from `presets` before running it.

## Prettier

[`prettier-plugin-tailwindcss`](https://github.com/tailwindlabs/prettier-plugin-tailwindcss) sorts classes against your stylesheet. With Tailwind 4, point it there:

```json title=".prettierrc"
{
  "plugins": ["prettier-plugin-tailwindcss"],
  "tailwindStylesheet": "./src/styles.css"
}
```

## UI components

The [UI components](/dolan/index.md) add semantic color utilities such as `bg-background` and `text-foreground`. With Tailwind 4 they come from the theme's `tailwind.css`, which you import after Tailwind; with Tailwind 3, from its Tailwind plugin. They take opacity modifiers, such as `bg-primary/50`, and switch colors when the `page` has a `dark` class. See [Theming](/dolan/theming.md) and [Dark mode](/guide/dark-mode.md).

## Related

- [CSS on Lynx](/guide/styling/index.md)
- [Dark mode](/guide/dark-mode.md)
- [Safe area](/guide/safe-area.md): the `*-safe` padding utilities
- [`add-tailwind` schematic](/api/schematics.md#add-tailwind)
