question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any better solution for Blazor CSS isolation?

See original GitHub issue

Is 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

  1. For native element, write css in {ComponentName}.razor.css
  2. 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 image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
MrDachcommented, Jul 8, 2021

Would love to see this fixed. Also not being able to target components in the CSS files is somewhat unexpected.

0reactions
TanayParikhcommented, Oct 28, 2022

Thanks for reporting this issue, I’m closing it out as a duplicate of https://github.com/dotnet/razor-tooling/issues/7606.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found