Live Chat Icon For mobile
Live Chat Icon

How do you style Blazor components with SCSS?

Platform: Blazor| Category: Components

SCSS, also known as Sassy CSS, is a CSS advancement that includes advanced features. It is more expressive, loads faster, uses fewer lines of code, and encourages proper rule nesting. It has all of the features of CSS and more, making it a good choice for developers to use. It is an SASS-specific file, similar to CSS, but with improved formatting.

To style your Blazor components with SCSS, follow these steps:

  1. Create a Blazor WebAssembly application by referring to this link.

  2. Create a new folder named TowerRange, and inside it, create TowerRange.razor, a new Blazor component, and define it as shown.

     <div class="tower">
        <div class="tower-range @BackgroundStyle"
             style="height: @Height"
        public int CurrentValue { get; set; }
        public BackgroundColor Color { get; set; } = BackgroundColor.LightGrey;
        public enum BackgroundColor { LightGrey, MediumGrey, DarkGrey }
        private string BackgroundStyle => $"BackColor-{Color.ToString()}";
        private string Height => $"{CurrentValue}%";

  3. Create a new SCSS file named TowerRange.scss inside the TowerRange folder and specify the style required for the TowerRange component as shown in the following.

       .tower {
        width: 15px;
        height: 200px;
        background-color: #fff;
        position: relative;
    .tower-range {
        width: 100%;
        display: block;
        font-family: arial;
        font-size: 12px;
        background-color: white;
        color: #fff;
        position: absolute;
        bottom: 0;
    $BackColor-LightGrey: #d4d3d2;
    $BackColor-MediumGrey: #8f9194;
    $BackColor-DarkGrey: #585959;
    .BackColor {
        &-LightGrey {
            background-color: $BackColor-LightGrey;
        &-MediumGrey {
            background-color: $BackColor-MediumGrey;
        &-DarkGrey {
            background-color: $BackColor-DarkGrey;

  4. Add the following code in the Helper.scss in the Shared folder to provide custom styles for the TowerRange component’s appearance in the Index.razor main page.

      .marginStyle {
        margin-left: 2px;
        display: inline-block;


  5. To combine all SCSS files, create an App.scss file in the main application and add the following code.

      @import 'TowerRange/TowerRange';
    @import 'Shared/Helper';

  6. Now, follow this link to install the Web Compiler extension and restart Visual Studio. After restarting, right-click App.css and select Web Compiler > Compile File. The compilerconfig.json file will be generated under the main application. Add the following code to the compilerconfig.json file.  

        "outputFile": "wwwroot/css/site.css",
        "inputFile": "App.scss",
        "minify": {
          "enabled": true
        "options": {
          "sourceMap": true

  7. Insert the site.css file created by the Web Compiler into the index.html page.

        <link href="css/site.css" rel="stylesheet" />

  8. To run Web Compiler during the build action (F5), install a NuGet package, or right-click compilerconfig.json and select Web Compiler > Enable compile on build, which automatically installs this NuGet package.  

     <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
                            <Content Remove="compilerconfig.json" />
                            <None Include="compilerconfig.json" />
                            <PackageReference Include="BuildWebCompiler" Version="1.12.394" />
                            <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.10" />
                            <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.10" PrivateAssets="all" />
                            <PackageReference Include="System.Net.Http.Json" Version="5.0.0" />

  9. Add the TowerRange component to the Index.razor page, as shown in the following.

    @page "/"
    @using SCSS.TowerRange;
    <h1> Blazor component styling with SCSS </h1>
    <div class="marginStyle">
        <TowerRange CurrentValue="20" Color="TowerRange.BackgroundColor.DarkGrey" />
    <div class="marginStyle ">
        <TowerRange CurrentValue="40" Color="TowerRange.BackgroundColor.MediumGrey" />
    <div class="marginStyle ">
        <TowerRange CurrentValue="60" Color="TowerRange.BackgroundColor.LightGrey" />

    When you run the application, you will see the following output.

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.