Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor CSS Isolation not working and not adding scope identifiers after migrating to .NET 5.0 and using SASS

I've recently migrated a pet project from Blazor .NET 3.1 to .NET 5.0 and am trying to switch to using css isolation.

I've created a scss file (which compiles to a css file using the webcompiler extension) with the same prefix as my component:

SCSS

I've included the generated css in my index.html file:

<link href="FinNodeWASM.Client.styles.css" rel="stylesheet">

Dotnet is generating the css correctly and appending the scope identifier attribute as expected: Generated CSS

However, when I look at the generated HTML I don't see the expected score identifier attribute there:

enter image description here

like image 684
Matthew Conradie Avatar asked Nov 14 '20 11:11

Matthew Conradie


People also ask

How add CSS file to Blazor?

Add a CSS File to a ProjectCopy a CSS file with your styles to the wwwroot/css folder. Add the CSS file link to the head section of the layout file: Pages/_Layout. cshtml in Blazor Server apps.

How do you add CSS to Razor page?

All you have to do is to place a style sheet in the Pages folder alongside the page that it is intended to affect. You just need to follow a specific naming convention, which is the Razor page file name with . css on the end. The bundled file itself is placed in the wwwroot folder when the application is published.

What is CSS isolation?

The isolation property in CSS is used to define whether an element must create a new stacking content. It prevents an element from acquiring mix-blend-mode from elements in the backdrop by creating a different stack element. Note: Isolate property is applied to the elements parent.

How do I add a style to Blazor?

To apply custom CSS style to a Blazor component, create a custom CSS file in your Razor page with the name of razorpage. css (i.e. Index. razor. css) and add your styles.


2 Answers

I was having the same issue when upgrading a simple project. I compared a newly created Blazor csproj file and deleting the RuntimeIdentifier and RazorLangVersion fixed it for me.

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    *** DELETE THIS LINE *** -> <RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
    *** DELETE THIS LINE *** -> <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net5.0' ">
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0-*" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0-*" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="5.0.0-*" />
    <PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="5.0.0-*" />
    <PackageReference Include="System.Net.Http.Json" Version="5.0.0-*" />
  </ItemGroup>


</Project>
like image 158
Tyler Avatar answered Oct 19 '22 06:10

Tyler


I experienced this issue when my ASPNETCORE_ENVIRONMENT was set to something other than 'Development'. This seems to be a known issue, and to resolve you need to set webBuilder.UseStaticWebAssets(); in Program.cs:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStaticWebAssets();
                    webBuilder.UseStartup<Startup>();
                });

Issue: https://github.com/dotnet/aspnetcore/issues/28911

Solution: https://github.com/dotnet/aspnetcore/issues/28174#issuecomment-734239932

like image 28
nf313743 Avatar answered Oct 19 '22 05:10

nf313743