Hello,
We’re attempting to use isolated CSS files for our Blazor components, but it appears not to work correctly when the WASM loads.
Steps to reproduce:
- Create a basic microservice solution with a Blazor Web App (Server + WASM).
- Notice the pre-included Index.razor.css in the generated .Client assembly.
- For testing, add background-color: red; to the existing .card-bg-image selector.
- Launch the application via ABP Studio and log in.
- Notice that the background-color: red is applied when using the server-side app but does not appear when the WASM loads..
Is there something missing in the startup configuration?
Thanks in advance!
6 Answer(s)
-
0
In ABP 9.x Blazor WebAssembly/WebApp projects, CSS isolation for component styles (e.g.,
Index.razor.css
) does not work out-of-the-box in the WASM client due to the new global assets and bundling system. The isolated CSS files are not automatically included in the global bundles that are loaded by the WASM client.To ensure your isolated CSS is included and applied in the WASM context, you need to explicitly add the generated CSS files to the global style bundle using a
BundleContributor
. Here’s how you can do it:
- Create a custom
BundleContributor
for your styles:
public class MyProjectNameStyleBundleContributor : BundleContributor { public override void ConfigureBundle(BundleConfigurationContext context) { // Add the generated CSS file for your component context.Files.Add("_content/MyProjectName.Blazor.Client/Index.razor.css"); } }
- Register your contributor in your module’s
ConfigureServices
:
Configure<AbpBundlingOptions>(options => { var globalStyles = options.StyleBundles.Get(BlazorWebAssemblyStandardBundles.Styles.Global); globalStyles.AddContributors(typeof(MyProjectNameStyleBundleContributor)); });
- Ensure your
.csproj
for the client project includes the following so the CSS is published:
<ItemGroup> <Content Update="Index.razor.css"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>
Remove any manual
global.css
fromwwwroot
and rely on the bundling system.Run your application and verify that the CSS is present in
/global.css
(or the configured bundle) and applied in both server and WASM modes.
This approach ensures your isolated CSS is bundled and available to the WASM client, resolving the issue where styles only appear in server-side rendering.
Sources:
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.
-
0
that does not seem to fix the issue. I have included the the css file manually in the bundle and I still have the issue.
-
0
hi
I will try your steps.
Create a basic microservice solution with a Blazor Web App (Server + WASM).
Can you share your solution info?
Thanks.
-
0
-
0
I created a new solution on 9.2.0 and the issue is not there anymore. I will need to investigate a bit more to see what could potentially be the difference between 9.1.0 and 9.2.0 that make it works.
-
0
ok