Installation

Set up Master CSS in Blazor

Guide to setting up Master CSS in your Blazor 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 blazor.rr.beta.css.master.co.

You can skip all installation steps.

npx degit master-co/css/examples/blazor-with-runtime-rendering my-project
cd my-project
npm install @master/css@beta
npm install
npm run dev

Installation

Create a project

If you don't have a Blazor project, create one first. It's recommended to refer to Build your first Blazor app

dotnet new blazorserver -o project --no-https -f net6.0
cd project

Create a New Folder

Add a new folder named npm_packages to root directory.

mkdir npm_packages
cd npm_packages

Set up NPM

Set up NPM and install the required webpack dependencies.

npm init -y
npm install webpack webpack-cli --save-dev

Install Master CSS

Install Master CSS into your project via package managers.

npm install @master/css@beta

Initialize configuration file

Run npx mcss init to create a configuration file master.css.ts.

npx mcss init

Set up CSS runtime engine

Create a new file npm_packages/src/index.js and import the master.css.js to enable the runtime engine.

import { initRuntime } from '@master/css';
import config from 'master.css';
initRuntime(config)

Add an NPM build script

Add an NPM build script on script section of the package.json file.

{
...
scripts: {
...
"build": "webpack ./src/index.js --output-path ../wwwroot/js --output-filename index.bundle.js --mode=development"
}
}

Add prebuild step

Add a prebuild step that will run npm install and npm run build whenever compile or build the application to the project.csproj file.

<Project>
...
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="npm install" WorkingDirectory="npm_packages" />
<Exec Command="npm run build" WorkingDirectory="npm_packages" />
</Target>
...
</Project>

Import bundles JavaScript file

Webpack will create a index.bundle.js file in the wwwroot/js, and import the file to entry file Pages/_Layout.cshtml

<html>
...
<body>
...
<script src="_framework/blazor.server.js"></script>
<script src="js/index.bundle.js"></script>
</body>
</html>

Build and start the app

Navigate to project directory, and build and start your app with dotnet watch

cd project
dotnet watch

Hello world!

Now style your first element using Master CSS syntax!

@page "/"
<PageTitle>Index</PageTitle>
<h1 class="font:40 font:heavy italic m:50 text:center">
Hello World
</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />

Open your browser to watch the changes.

localhost:3000

Hello World

MIT License © Aoyue Design LLC.