0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am experiencing this same issue with Telerik UI for Blazor controls. The CSS identifiers will not be added to HTML created by the Telerik UI library only those that are added directly to the code.
-1

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.

1 Comment

This answer doesn't address the question properly: the point is to get isolated CSS working on the child css file, not to put all the child CSS in the parent's css and then use the ::deep selector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.