---
title: Tailwind
description: Use dark mode utilities with Astro Eclipse, with the class attribute and the Tailwind 4 custom variant.
---

Tailwind's `dark:` utilities need to know what "dark" means in your page. There are two setups. Both work with Astro Eclipse; pick one.

## Option 1: class attribute

Tell Astro Eclipse to toggle classes on `<html>` instead of setting `data-theme`:

```js astro.config.mjs
import eclipse from 'astro-eclipse'

export default defineConfig({
  integrations: [eclipse({ attribute: 'class' })],
})
```

With `themes: ['light', 'dark']` the element ends up with the class `light` or `dark`, and the other one is removed. Any other classes on `<html>` are left alone.

Then declare the variant in Tailwind 4:

```css src/styles/global.css
@import 'tailwindcss';

@custom-variant dark (&:where(.dark, .dark *));
```

```html
<body class="bg-white text-black dark:bg-black dark:text-white"></body>
```

### Different class names than the themes

When the class must not be the theme name (a `dark` theme that adds `theme-dark`, or several classes at once), map it with `values`. Storage and the API keep the theme names.

```js astro.config.mjs
eclipse({
  attribute: 'class',
  themes: ['light', 'dark'],
  values: { dark: 'dark theme-night' },
})
```

The theme `dark` then puts the classes `dark` and `theme-night` on `<html>`, and the classes of every other theme are removed first. A theme without an entry uses its own name. The same option works with `data-theme`: `{ dark: 'theme-dark' }` writes `data-theme="theme-dark"`.

## Option 2: keep data-theme

If you would rather keep the default attribute, point the variant at it. No `attribute` option is needed:

```js astro.config.mjs
export default defineConfig({
  integrations: [eclipse()],
})
```

```css src/styles/global.css
@import 'tailwindcss';

@custom-variant dark (&:where([data-theme='dark'], [data-theme='dark'] *));
```

## Which config to choose

| Setup     | Astro Eclipse config              | Tailwind 4 CSS                                                                |
| --------- | --------------------------------- | ----------------------------------------------------------------------------- |
| Class     | `eclipse({ attribute: 'class' })` | `@custom-variant dark (&:where(.dark, .dark *));`                             |
| Attribute | `eclipse()`                       | `@custom-variant dark (&:where([data-theme='dark'], [data-theme='dark'] *));` |

The attribute setup keeps your CSS variables and Tailwind on the same selector, which makes it the simpler one when you use both. The class setup matches what most Tailwind tutorials show.

The names in the variant must match your `themes`. If you rename `dark` to `night`, the selector becomes `.night` or `[data-theme='night']`.

:::note
Both setups were built in a scratch project with Astro 7.4.8, Tailwind 4.3.5 and `@tailwindcss/vite`, on astro-eclipse 1.2.0. The generated CSS contained `.dark\:bg-black:where(.dark, .dark *)` for the class setup and the `[data-theme=dark]` selectors for the attribute setup, and the page carried the inline head script. The result was not opened in a browser. For Tailwind 3 see its own `darkMode` documentation.
:::

Related: [CSS variables](/guides/css-variables), [config reference](/reference/config).
