CSS style loading failed after RCL library release ?

929Free 641 Reputation points
2025-06-07T17:44:00.5566667+00:00

hi, I have created a new RCL library. (RazorClassLibrary1 .net 9.0)

Component1.razor :

@page "/test1"

<div class="my-component">
    This component is defined in the <strong>RazorClassLibrary1</strong> library.
</div>

Component1.razor.css :

.my-component {
    border: 2px dashed red;
    padding: 1em;
    margin: 1em 0;
    background-image: url('background.png');
}

Then I created a blazor web app project (server mode).

_Imports.razor :

@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using BlazorApp1
@using BlazorApp1.Components

@using RazorClassLibrary1;

App.razor :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="/" />
    <link rel="stylesheet" href="@Assets["app.css"]" />
    <link rel="stylesheet" href="@Assets["BlazorApp1.styles.css"]" />
    <ImportMap />
    <link rel="stylesheet" href="@Assets["RazorClassLibrary1.styles.css"]">
    <link rel="stylesheet" href="@Assets["_content/RazorClassLibrary1/RazorClassLibrary1.styles.css"]">
    <HeadOutlet @rendermode="InteractiveServer" />
</head>
<body>
    <Routes @rendermode="InteractiveServer" />
    <script src="_framework/blazor.web.js"></script>
</body>
</html>

Routes.razor:

<Router AppAssembly="typeof(Program).Assembly"
        AdditionalAssemblies="new[] {
                    typeof(RazorClassLibrary1._Imports).Assembly
                }">
    <Found Context="routeData">
        <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
        <FocusOnNavigate RouteData="routeData" Selector="h1" />
    </Found>
</Router>

Programs.cs:

app.MapRazorComponents<App>()
    .AddAdditionalAssemblies(typeof(RazorClassLibrary1._Imports).Assembly)
    .AddInteractiveServerRenderMode();

app.Run();

IDE: Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.14.4

I have copied the RazorClassLibrary1.styles.css file of the released RCL library to wwwroot file.

Why is the style still not displaying correctly?

I didn't set it up somewhere, right? Can you help me? Thank you.

Community Center Not monitored
{count} votes

Accepted answer
  1. Pradeep M 9,765 Reputation points Microsoft External Staff Volunteer Moderator
    2025-06-08T15:11:02.46+00:00

    Hi 929Free

    Thank you for reaching out to Microsoft Q & A forum.

    When using CSS isolation in a Razor Class Library (RCL), it is important not to manually copy the .styles.css file into your application's wwwroot folder. Instead, please ensure the following: 

    The static assets such as background.png are included within the RCL project’s own wwwroot folder so they are properly published. 

    Your Blazor application references the RCL styles using the automatically generated path: 

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

    Avoid duplicating or manually adding additional stylesheet references. 

    Manually copying the stylesheet can break CSS scoping and resource paths, which may cause the styles not to load correctly. 

    After making these adjustments, please perform a clean rebuild of your solution.   

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community. 


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2025-06-08T17:25:31.72+00:00

    razor

    Copy

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


Your answer

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