0

I have done all of my coding in desktop so far.Now I am learning blazor.How does CSS classes work.I was under the impression that the classes were isolated within the component but it seems this is incorrect!

Component 1.

//Component One;
<h3 class="my-h3">One</h3>
<style>
   .my-h3{
       background-color:red;
   }
</style>

@code {

}

Component Two:

//component 2
 <h3 class="my-h3">Two</h3>
 <style>
    .my-h3{
        background-color:blue;
    }
 </style>
 @code {

 }

Index Page:

@page "/"
<One></One>
<Two></Two>
Welcome to your new app.

Both compents have a background of red. I thought that the classes would be self-contained but I guess not. Is there a recognised way to deal with styling components?

1
  • 4
    CSS Isolation is currently planned for : 5.0.0-preview7, that should be out in a few weeks. Commented Jun 11, 2020 at 12:44

1 Answer 1

2

I think you are looking for scoped styles . You can add this using a nuget package BlazorScopedCss .

Blazor Scoped Css

Sign up to request clarification or add additional context in comments.

1 Comment

Seems like css in Blazor is not scoped by default. Scoping css was not a thing back then. Good find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.