Is there any better solution for Blazor CSS isolation?
See original GitHub issueIs your feature request related to a problem? Please describe.
Blazor only adds the scope attribute to native html elements. If the root element of your component is a custom component instead of a native HTML element, such as <div>
, then the scope attribute will not be added, so the ::deep
modifier will not work as expected.
Describe the solution you’d like
- For native element, write css in {ComponentName}.razor.css
- To applay css in child component, parent component needs to add a native html element to wrap it self.
But this is more like a trick, If the existed style requires the child component must be a child
, not grand child
, the solution may break the style.
Example:
- Test.razor
@page "/test"
<div>
<Layout Class="layout">
<Header>
<div class="logo" />
<Menu Theme="MenuTheme.Dark" Mode="MenuMode.Horizontal" DefaultSelectedKeys=@(new[]{"2"})>
<MenuItem Key="1">nav 1</MenuItem>
<MenuItem Key="2">nav 2</MenuItem>
<MenuItem Key="3">nav 3</MenuItem>
</Menu>
</Header>
<Content Style="padding: 0 50px;">
<Breadcrumb Style="margin: 16px 0;">
<BreadcrumbItem>Home</BreadcrumbItem>
<BreadcrumbItem>List</BreadcrumbItem>
<BreadcrumbItem>App</BreadcrumbItem>
</Breadcrumb>
<div class="site-layout-content">Content</div>
</Content>
<Footer Style="text-align: center; ">Ant Design ©2018 Created by Ant UED</Footer>
</Layout>
</div>
- Test.razor.css
::deep .site-layout-content {
background: #fff;
padding: 24px;
min-height: 280px;
color: red;
font-size: 36px;
}
::deep .ant-layout-footer {
background-color: #2f54eb;
}
-
In
Pages/_Host.cshtml
, add<link href="{PROJECT NAME}.styles.css" rel="stylesheet">
-
Result
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:5 (3 by maintainers)
Top Results From Across the Web
ASP.NET Core Blazor CSS isolation
Learn how CSS isolation scopes CSS to Razor components, which can simplify CSS and avoid collisions with other components or libraries.
Read more >CSS Isolation in Blazor Applications
Well, the answer is yes. To be more precise, Blazor CSS isolation doesn't offer a preprocessor functionality but it supports it.
Read more >How to achieve style inheritance with Blazor CSS isolation
In my opinion, using inheritance with CSS isolation works best when you want to share styles across a small group of similar components....
Read more >What is Blazor CSS isolation? How do you enable it for ...
Blazor CSS isolation is a process that occurs at build time. By using CSS isolation, you can use CSS files or specific components...
Read more >Adding Sass to Blazor with CSS Isolation using just one line of ...
Easily add sass to your next blazor project with just one line of code. Even better, it also supports CSS Isolation. All you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Would love to see this fixed. Also not being able to target components in the CSS files is somewhat unexpected.
Thanks for reporting this issue, I’m closing it out as a duplicate of https://github.com/dotnet/razor-tooling/issues/7606.