Set up Master CSS in Svelte
Guide to setting up Master CSS in your Svelte project.
Master CSS Runtime Rendering observes changes in DOM class names at browser runtime, generates corresponding CSS rules, and injects them into the running style sheet.
Fixed style cost
All features work out of the box with ~16KB transfer cost
Fully automatic
Capture any program-generated class names
CSS lifecycle
CSS is generated on demand and frees memory when not in use
Quick start
Clone the example
Copy-paste the following commands to quickly start using the svelte.rr.beta.css.master.co.
You can skip all installation steps.
npx degit master-co/css/examples/svelte-with-runtime-rendering my-projectcd my-projectnpm install @master/css.svelte@betanpm installnpm run dev -- --open
Installation
Create a project
If you don't have a Svelte project, create one first. It's recommended to refer to Svelte Kit.
npm create svelte@latest projectcd projectnpm install
Install Master CSS
Install Master CSS Svelte into your project via package managers.
npm install @master/css.svelte@beta
Initialize configuration file
Run npx mcss init
to create a configuration file master.css.ts.
npx mcss init
Allow configuration file
Add ./master.css.ts
to the server.fs.allow
section in vite.config.ts
to be allowed by the server file system.
Accessing files outside this directory list that aren't imported from an allowed file will result in a 403.
import { sveltekit } from '@sveltejs/kit/vite'import { defineConfig } from 'vite' export default defineConfig({ plugins: [sveltekit()], server: { fs: { allow: ['./master.css.ts'] } }})
Set up CSS runtime engine
Register the <CSSProvider>
in src/routes/+layout.svelte
.
<script> import { CSSProvider } from "@master/css.svelte"; import config from '../../master.css'; import Header from "./Header.svelte"; import "./styles.css";</script><svelte:component this={CSSProvider} config={config}> <slot /></svelte:component>
Avoid flash of unstyled content
Add display: 'none'
in <html>
to avoid FOUC caused by the runtime engine not yet injecting CSS rules.
<!DOCTYPE html><html lang="en" style="display:none">…
Launch server
Run npm run dev -- --open
to start your Svelte development server
npm run dev -- --open
Start using Master CSS
Now style your first element using Master CSS syntax!
<h1 class="font:40 fg:blue font:heavy italic m:50 text:center">Hello World</h1>
Open your browser to watch the changes.