Set up Master CSS in Next.js
Guide to setting up Master CSS in your Next.js 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 nextjs.rr.beta.css.master.co.
You can skip all installation steps.
npx degit master-co/css/examples/next.js-with-runtime-rendering my-projectcd my-projectnpm install @master/css.react@betanpm installnpm run dev
Installation
Create a Next.js project
If you don't have a Next.js project, create one first. It's recommended to refer to Create Next App.
npx create-next-app@latest --experimental-app --tscd project
Install Master CSS
Install Master CSS React into your project via package managers.
npm install @master/css.react@beta
Initialize configuration file
Run npx mcss init
to create a configuration file master.css.ts.
npx mcss init
Set up CSS runtime engine
Register Master CSS with official CSSProvider
and provide instance context.
- Register the
CSSProvider
- Import the config
../master.css
- Add
display: 'none'
in<html>
to avoid FOUC
import './globals.css'import { CSSProvider } from '@master/css.react'import config from '../master.css' export const metadata = { title: 'Create Next App', description: 'Generated by create next app',} export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en" style={{ display: 'none' }}> <body> <CSSProvider config={config}> {children} </CSSProvider> </body> </html> )}
Launch server
Run npm run dev
to start your Next.js development server
npm run dev
Start using Master CSS
Now style your first element using Master CSS syntax!
export default function Home() { return ( <h1 className="font:40 font:heavy italic m:50 text:center">Hello World</h1> )}
Open your browser to watch the changes.