My Blazor build (ASP.NET Core 9.0, VS 2022) is correctly generating the scoped selectors for isolated CSS:
Program.cs
:
builder.WebHost.UseStaticWebAssets();
App.razor
<head>
tag:
<link rel="stylesheet" href="MyAssemblyName.styles.css" />
MainLayout.razor
:
<AppBar class="test-class"></AppBar>
The AppBar.razor.css
part compiled into MyAssemblyName.styles.css
:
.test-class[b-rx6ij8vkro] {
color: red;
background-color: aquamarine;
}
So far so good, I have the b-rx6ij8vkro
selector. However, here's the generated html:
<header class="mud-appbar mud-appbar-fixed-top mud-elevation-0 test-class">
No b-rx6ij8vkro
selector on the header tag. Moreover, no selectors are generated for any other tags in the html file!
How do I generate the correct html?
My Blazor build (ASP.NET Core 9.0, VS 2022) is correctly generating the scoped selectors for isolated CSS:
Program.cs
:
builder.WebHost.UseStaticWebAssets();
App.razor
<head>
tag:
<link rel="stylesheet" href="MyAssemblyName.styles.css" />
MainLayout.razor
:
<AppBar class="test-class"></AppBar>
The AppBar.razor.css
part compiled into MyAssemblyName.styles.css
:
.test-class[b-rx6ij8vkro] {
color: red;
background-color: aquamarine;
}
So far so good, I have the b-rx6ij8vkro
selector. However, here's the generated html:
<header class="mud-appbar mud-appbar-fixed-top mud-elevation-0 test-class">
No b-rx6ij8vkro
selector on the header tag. Moreover, no selectors are generated for any other tags in the html file!
How do I generate the correct html?
I was using Mudblazor, and isolated CSS identifiers are only generated on plain html markup. Therefore, you have to wrap your styled components in a <div class="test-class">
etc.
You may try
::deep .test-class{
color: red;
background-color: aquamarine;
}
in MainLayout.razor.css
as stated in this document:
CSS isolation only applies to the component you associate with the format {COMPONENT NAME}.razor.css, where the {COMPONENT NAME} placeholder is usually the component name. To apply changes to a child component, use the ::deep pseudo-element to any descendant elements in the parent component's .razor.css file. The ::deep pseudo-element selects elements that are descendants of an element's generated scope identifier.