CSS isolation in blazor-server components
Asked Answered
M

2

7

with the new .NET 5 preview CSS isolation comes in blazor. Does anybody know, is it possible to use the CSS isolation in blazor server?

I have play a lot around, but in my blazor server app CSS isolation doesn't work. Or is this feature only available in blazor WebAssembly?

so I tried it:

  • create a new blazor component, e.g. test.razor
  • create component CSS file with name test.razor.css and write the CSS code in this file
  • now start the blazor-server app, but no static CSS file will added and the CSS classes doesn't found.

TargetFramework is "net5.0". installed dotnet version is "5.0.100-rc.2.20479.15".

greetings Daniel

Mog answered 8/11, 2020 at 13:4 Comment(2)
The template uses it by default in Server-side. For example MainLayout.razor and MainLayout.razor.cssBloom
i tried it exactly so, but it doesn't workMog
M
12

ok i found the issue. In my "_Host.cshtml" file was the link to the "project.styles.css" missing:

<link href="ProjectName.styles.css" rel="stylesheet" />

with this everything works fine.

Mog answered 8/11, 2020 at 18:29 Comment(3)
Thank you for the hint. Also keep in mind that the {ProjectName} has to match Solution > Project > Properties > Package > Product. This may be different to the Project namespace.Theall
This also worked for me, but is it anywhere in the documentation that this needs to be added for CSS isolation to work?Ricercar
yes have a look at learn.microsoft.com/en-us/aspnet/core/blazor/components/…Mog
D
1

If you are using .NET 6.0 blazor server side, paste the following line:

<link href="ProjectName.styles.css" rel="stylesheet" />

On the Pages > _Layout.cshtml file and it will work as expected.

Defensive answered 15/7, 2022 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.