# Set up Master CSS in Astro

Connect the Astro integration and shared CSS entry, then verify production styles in the rendering mode you choose.

Connect Master CSS to an Astro site through a shared CSS entry. Astro renders the pages, and Master CSS delivers their styles in static, prerender, runtime or progressive mode.

<a id="installation"></a>

## Installation

<a id="add-master-css"></a>

### Add Master CSS

In an existing [Astro project](https://docs.astro.build/en/install-and-setup/), run the installer from the project root. Static is the default: it scans complete class strings and emits ordinary CSS.

```bash
npm create @master/css@2.1.8 -- --framework astro --yes
```

<a id="connect-the-stylesheet"></a>

### Connect the stylesheet

The installer creates a standalone CSS entry:

```css name=src/styles/global.css
@import '@master/css';
```

[Import it from page or layout frontmatter](https://docs.astro.build/en/guides/imports/#import-statements). A shared layout delivers it to every page using that layout. Keep the Master CSS integration beside the project's existing integrations in `astro.config.mjs`.

<a id="start-using-master-css"></a>

### Start using Master CSS

Start the dev server. Import `src/styles/global.css` from the page frontmatter, then add the paragraph. Use the same import in a shared layout when several pages need it.

```astro name=src/pages/index.astro
---
import '../styles/global.css'
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Hello Master CSS</title>
  </head>
  <body>
    <p id="master-check" class="w-fit m(0) p-md b-solid b(1) b-line-divider r-md text-sm font-regular bg-surface-raised fg-text-strong">
      Hello World
    </p>
  </body>
</html>
```

<a id="check-the-setup"></a>

## Check the setup

Run `npm run dev`, change `p-md` to `p-lg` and check that padding grows from `1rem` to `1.5rem`. Restore it and test the production page:

```bash
npm run build
npm run preview
```

Disable JavaScript and reload. The paragraph should retain its padding, rounded corners and raised surface. If no styles appear, confirm the page or its layout imports `global.css` and the Astro configuration includes the Master CSS integration. For custom definitions and scoped styles, see [Global styles](/guide/global-styles).

Enjoying Master CSS? [Star it on GitHub](https://github.com/master-co/css) to help other developers find it.

<a id="cli-build"></a>

## Build static CSS with the CLI

Use this as an alternative to the Astro integration. Remove its registration from `astro.config.mjs`, preserving other integrations, and remove the `global.css` import from the page or layout. Install the CLI:

```bash
npm install -D @master/css-cli@2.1.8
```

Create the source entry at the project root. Put any project definitions here too:

```css name=master.css
@import '@master/css';
```

Link the generated stylesheet from the page or shared layout's `<head>`:

```html
<link rel="stylesheet" href="/master.css" />
```

Generate the CSS before building Astro:

```bash
npx @master/css-cli generate "src/**/*.{astro,html,js,jsx,ts,tsx,vue,svelte}" --output public/master.css
npm run build
npm run preview
```

During development, add `--watch` to the generation command and run `npm run dev` in a second terminal. Astro copies `public/` into the build; deploy the generated stylesheet and its companion imports together. For a site with a configured base path, prefix the stylesheet URL with that base. Verify the paragraph with JavaScript disabled.

---

<a id="rendering-modes"></a>

## Choose another rendering mode

Static mode is the default. The integration injects a page script in runtime and progressive modes. In prerender mode, Astro middleware adds CSS to HTML without a browser runtime.

| Mode | Installer option | Delivery |
| --- | --- | --- |
| Static | None | Generated stylesheet from source scanning |
| Prerender | `--mode prerender` | CSS in built pages and server HTML responses, with no browser runtime |
| Runtime | `--mode runtime` | Browser runtime, without Astro middleware pre-rendering |
| Progressive | `--mode progressive` | Middleware-rendered CSS plus a runtime handoff |

**Generate new classes in the browser**

```bash
npm create @master/css@2.1.8 -- --framework astro --mode runtime --yes
```

Run the build and preview scripts; the paragraph should be styled after the runtime starts. Runtime mode depends on JavaScript and its compiled program loading successfully, so check the console and network requests if it stays unstyled. To test a class introduced after the page was generated, run:

```js
document.querySelector('#master-check').classList.add('underline')
```

The paragraph should gain an underline without rebuilding.

**Include server-rendered styles**

```bash
npm create @master/css@2.1.8 -- --framework astro --mode progressive --yes
```

Astro middleware renders the CSS into prerendered pages and server HTML responses, then the runtime handles later class changes. Disable JavaScript and reload: the paragraph should still be styled. Re-enable JavaScript, reload, and add `underline` from the console as above.

To deliver CSS with the HTML and skip browser generation, use `--mode prerender` in the same command. Verify that the response source contains `style#master-css`. Any Master CSS classes introduced in the browser must already be covered by the delivered CSS.
