Installation

Set up Master CSS in Blazor

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 ~17KB transfer cost

Fully automatic

Capture any program-generated class names

CSS lifecycle

Generated on-demand and frees memory when not in use


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

Initialize configuration file

Run npm create @master/css@rc to create a configuration file master.css.ts.

npm create @master/css@rc

Install Master CSS

Install Master CSS into your project via package managers.

npm install @master/css-runtime@rc

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 { initCSSRuntime } from '@master/css-runtime';
import config from 'master.css';
initCSSRuntime(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:12x 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

© Aoyue Design LLC.