# Tailwind CSS

How to use `material` beside Tailwind CSS 4: one line orders the cascade layers, and a theme block turns the Material
tokens into utilities. The examples were run with Tailwind CSS 4.3.5 and Vite 8.4.

## Order the layers

Tailwind's utilities are in the layer `utilities` and its reset, Preflight, in `base`. material's per-component
stylesheets are in `mtrl`. Put `mtrl` between the two, before any other CSS of the page:

```css
/* app.css */
@layer theme, base, mtrl, components, utilities;
@import "tailwindcss";
```

Import this file first, then the components' stylesheets:

```javascript fragment
import './app.css';
import 'material/styles/base';
import 'material/styles/typography';
import 'material/styles/button';
```

The order does two things:

- **Preflight stays under the components.** If `mtrl` comes first, Preflight resets the factories: a filled button loses
  its background and its corners.
- **Utilities win over the components.** `createButton({ text: 'Save', class: 'rounded-none' })` has square corners.

Use the [per-component stylesheets](https://md3.io/develop/stylesheets.md). `material/styles`, the sheet with every component, is in no layer:
its rules win over the utilities.

## Web Components and frameworks

A web component, and a React, Vue, Svelte or Solid component, draws in its shadow root, where neither Preflight nor the
utilities reach. A utility on the element styles its host, so it can place it:

```html
<m-button class="mt-4">Save</m-button>
```

To change how the component looks, set its custom properties or its tokens ([Theming](https://md3.io/styles/theming.md)).

## Tokens as utilities

`@theme inline` makes a utility of a token and keeps the token's `var()` in it, so the utility follows the theme, dark
mode and contrast. List the roles you use:

```css
@theme inline {
  --color-primary: var(--mtrl-sys-color-primary);
  --color-on-primary: var(--mtrl-sys-color-on-primary);
  --color-primary-container: var(--mtrl-sys-color-primary-container);
  --color-on-primary-container: var(--mtrl-sys-color-on-primary-container);
  --color-surface: var(--mtrl-sys-color-surface);
  --color-on-surface: var(--mtrl-sys-color-on-surface);

  --radius-corner-xl: var(--mtrl-sys-shape-corner-extra-large);

  --font-brand: var(--mtrl-ref-typeface-brand);
  --text-title-large: var(--mtrl-sys-typescale-title-large-font-size);
  --text-title-large--line-height: var(--mtrl-sys-typescale-title-large-line-height);
  --text-title-large--letter-spacing: var(--mtrl-sys-typescale-title-large-letter-spacing);
  --text-title-large--font-weight: var(--mtrl-sys-typescale-title-large-font-weight);
}
```

```html
<div class="bg-primary-container text-on-primary-container rounded-corner-xl p-6 text-title-large font-brand">
  Material roles, Tailwind spacing
</div>
```

`bg-primary/50` works too. The type tokens need `material/styles/typography`. The names are yours: `rounded-corner-xl`
leaves Tailwind's own `rounded-xl` as it is.

## Dark mode

Tailwind's `dark:` follows the system. material's themes follow `data-theme-mode`, and the baseline follows the system
when no theme is set ([Dark mode](https://md3.io/styles/theming.md#dark-mode)). This variant follows both:

```css
@custom-variant dark {
  &:where([data-theme-mode="dark"], [data-theme-mode="dark"] *) { @slot; }
  @media (prefers-color-scheme: dark) {
    &:where(:root:not([data-theme]) *) { @slot; }
  }
}
```

The color utilities above need no `dark:`: their roles change with the mode.

## Sass

Tailwind CSS 4 [is not designed to be used with CSS preprocessors](https://tailwindcss.com/docs/compatibility). Compile
material's Sass ([configuration](https://md3.io/styles/theming.md#sass-configuration)) to a stylesheet of its own, and import it beside
`app.css`.
