# Styling

Customize colors, fonts and color mode.

Your stylesheet is `src/theme.css`. Without one, the site uses the `neutral` color preset. With one, the site uses only what the file imports, so start it with a preset:

```css title="src/theme.css"
@import "vuesho/style/ocean.css";
```

## Color Presets [#color-presets]

Each preset has colors for light and dark mode:

```css
@import "vuesho/style/neutral.css";
@import "vuesho/style/black.css";
@import "vuesho/style/vitepress.css";
@import "vuesho/style/dusk.css";
@import "vuesho/style/catppuccin.css";
@import "vuesho/style/ocean.css";
@import "vuesho/style/purple.css";
@import "vuesho/style/solar.css";
@import "vuesho/style/emerald.css";
@import "vuesho/style/ruby.css";
@import "vuesho/style/aspen.css";
@import "vuesho/style/shadcn.css";
```

The `shadcn` preset reads [shadcn/ui](https://ui.shadcn.com)'s CSS variables, like `--primary` and `--border`, so the docs match an app built with it.

## Colors [#colors]

The colors are CSS variables named `--color-fd-<name>`. Set them after the preset's import, in `:root` for light mode and in `.dark` for dark mode:

```css title="src/theme.css"
@import "vuesho/style/neutral.css";

:root {
  --color-fd-primary: hsl(160, 84%, 39%);
}

.dark {
  --color-fd-primary: hsl(158, 64%, 52%);
}
```

Most colors come in pairs: `--color-fd-primary` is a primary button's background, and `--color-fd-primary-foreground` is its text color.

| Name             | Where the UI uses it                            |
| ---------------- | ----------------------------------------------- |
| `background`     | The page, with its text in `foreground`         |
| `primary`        | Links, the active sidebar item, primary buttons |
| `muted`          | Subtle surfaces and secondary text              |
| `card`           | Cards, code blocks and the sidebar              |
| `popover`        | Popovers, menus and dialogs                     |
| `secondary`      | Secondary controls, like the search button      |
| `accent`         | Hovered items, like the sidebar's links         |
| `border`, `ring` | Borders, and outlines of focused elements       |

## Color Mode [#color-mode]

Readers switch between light and dark mode with the button in the sidebar or the navbar, or with the <kbd>D</kbd> key, and their choice is saved in the browser. Configure the color mode with `ui.colorMode` in the [config](/config):

```ts title="vuesho.config.ts"
createSite(docs.toSource()).config({
  ui: {
    colorMode: {
      // The color mode before the reader picks one: "system" (default), "light" or "dark"
      default: "dark",
      // The key that switches the color mode, or `false` for none
      hotKey: "t",
      // The `localStorage` key for the reader's choice
      storageKey: "acme-color-mode",
    },
  },
});
```

`fixed: true` keeps every page in the `default` color mode and hides the button:

```ts title="vuesho.config.ts"
createSite(docs.toSource()).config({
  ui: { colorMode: { default: "dark", fixed: true } },
});
```

In a component, read or set the color mode with [`useColorMode()`](/client-api). When `hotKey` is `false`, your component can bind another shortcut, like <kbd>Shift</kbd>+<kbd>D</kbd>.

An inline script sets the color mode before the first paint, so a strict [Content Security Policy](/deploying#content-security-policy) must allow that script.

### Color Mode per Page [#color-mode-per-page]

`colorMode` in the page data keeps one page in a color mode. In a custom page, set it with `definePage()`:

```vue title="src/pages/index.vue"
<script setup lang="ts">
import { definePage } from "vuesho/client";

definePage({ data: { title: "Acme", colorMode: "dark" } }); // [!code highlight]
</script>
```

For a Markdown page, return `colorMode` from a [plugin's](/plugin-api) `transformPage` hook, which changes the page data:

```ts title="vuesho.config.ts"
createSite(docs.toSource()).plugins(recommendedPreset(), {
  name: "dark-showcase",
  transformPage: (_data, { path }) => (path === "/showcase" ? { colorMode: "dark" } : undefined),
});
```

## Fonts [#fonts]

The UI uses `--font-sans` for text and `--font-mono` for code. Install a font from [Fontsource](https://fontsource.org), and Vite bundles its files with the site:

```npm
npm i @fontsource-variable/geist
```

```css title="src/theme.css"
@import "vuesho/style/neutral.css";
@import "@fontsource-variable/geist";

:root {
  --font-sans: "Geist Variable", sans-serif;
}
```

### Font Files [#font-files]

To load a font file from your project, use `@font-face` with a relative URL, and Vite bundles the file:

```css title="src/theme.css"
@import "vuesho/style/neutral.css";

@font-face {
  font-family: "Inter";
  src: url("./fonts/Inter.woff2") format("woff2");
  font-weight: 100 900;
  font-display: swap;
}

:root {
  --font-sans: "Inter", sans-serif;
}
```

### Preloading Fonts [#preloading-fonts]

To render text in your font from the first paint, preload the font file with a [head tag](/theme#head-tags) in the theme. `?url` imports the file's URL:

```ts title="src/theme.ts"
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import geist from "@fontsource-variable/geist/files/geist-latin-wght-normal.woff2?url"; // [!code ++]

export default defineTheme({
  extends: defaultTheme,
  // [!code ++:6]
  head: [
    {
      tag: "link",
      attrs: { rel: "preload", href: geist, as: "font", type: "font/woff2", crossorigin: "" },
    },
  ],
});
```

### Fallback Fonts [#fallback-fonts]

[fontaine](https://github.com/unjs/fontaine) generates a fallback system font with your font's metrics, so the layout doesn't shift once the font loads:

```npm
npm i -D fontaine
```

```ts title="vite.config.ts"
import { FontaineTransform } from "fontaine"; // [!code ++]
import { defineConfig } from "vite";
import vuesho from "vuesho/vite";

export default defineConfig({
  plugins: [vuesho(), FontaineTransform.vite({ fallbacks: ["Arial"] })], // [!code highlight]
});
```

fontaine doesn't read CSS variables, so add the fallback font, named `<font name> fallback`, to `--font-sans`:

```css title="src/theme.css"
:root {
  --font-sans: "Geist Variable", "Geist Variable fallback", sans-serif; /* [!code highlight] */
}
```

## Tailwind CSS [#tailwind-css]

The stylesheet compiles with [Tailwind CSS](https://tailwindcss.com), so a Tailwind class works in any file of your site, like a custom page, a component or a Markdown page, with no setup:

```vue title="src/pages/index.vue"
<template>
  <main class="mx-auto grid max-w-5xl gap-6 md:grid-cols-2">...</main>
</template>
```

In `src/theme.css`, use the directives of Tailwind CSS, like `@theme` for a color that your classes use:

```css title="src/theme.css"
@import "vuesho/style/ocean.css";

@theme {
  --color-brand: hsl(160, 84%, 39%); /* bg-brand, text-brand */
}
```

Tailwind CSS [detects classes](https://tailwindcss.com/docs/detecting-classes-in-source-files) in your project's files, except those that `.gitignore` lists, and in the modules of Vuesho and its plugins. Add a class that none of those files has with `@source inline()`, like one that a markdown-it plugin from npm writes, and leave out a folder that holds no UI with `@source not`:

```css title="src/theme.css"
@source inline("bg-red-500 bg-green-500");
@source not "../public/registry";
```

To pass options to Tailwind CSS, like `important`, import it yourself, then the colors of a preset from `vuesho/css` and the stylesheet of the UI:

```css title="src/theme.css"
@import "tailwindcss" important;
@import "vuesho/css/ocean.css";
@import "vuesho/style/ui.css";
```

## Layout Width [#layout-width]

`--fd-layout-width` sets the maximum width of the `docs` and `notebook` layouts, `97rem` by default. The `home` layout is always `1400px` wide, and the other layouts ignore the variable:

```css title="src/theme.css"
:root {
  --fd-layout-width: 80rem;
}
```

## Utility Classes [#utility-classes]

These classes style your components like the UI:

- `prose` styles HTML like a Markdown page.
- `prose-sm` and `prose-xs` make prose smaller.
- `prose-no-margin` removes the margins at the start and the end.
- `fd-steps` on a container and `fd-step` on each child number the children, like the [Steps](/markdown/components#steps) component.

```vue title="src/components/Note.vue"
<template>
  <div class="prose prose-sm">
    <slot />
  </div>
</template>
```
