Why is the blazor css isolation not working?

Haneen Al-fakhry 10 Reputation points
2024-02-25T21:05:38.5766667+00:00

I have an AppLogin.razor component and its css file is AppLogin.razor.css. They are working fine. I have a UserLogin.razor component and its css file is UserLogin.razor.css. The working mechanism is as follows When you login from the AppLogin.razor component, it navigates to the UserLogin.razor component by NavigationManager route, but the problem is that it does not execute the css file for UserLogin.razor, but rather executes the css file for AppLogin.razor, even after moving to the UserLogin.razor component. What's the reason?

note : I have included this <link href="AssembleName.styles.css" rel="stylesheet">

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Blazor
Developer technologies | .NET | Other
Developer technologies | ASP.NET | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} vote

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 81,726 Reputation points Volunteer Moderator
    2024-02-26T16:50:10.05+00:00

    please read the docs until you understand how css isolation works:

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-8.0

    then use the browsers CSS debug tools to see why the isolation selectors are not work as you expected


  2. Mick Barretto 0 Reputation points
    2025-11-06T13:48:33.3566667+00:00

    There's a bug (imo) that you need to add <div> around your razor components for css isolation to take affect

    component.razor
    <div>
    //Existing html
    </div>

    This should make your css isolation work

    https://github.com/dotnet/aspnetcore/issues/29409

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.