Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Css in isolated files do not apply. #591

Closed
aspramsh opened this issue Jan 15, 2021 · 25 comments
Closed

Css in isolated files do not apply. #591

aspramsh opened this issue Jan 15, 2021 · 25 comments
Labels
question Further information is requested

Comments

@aspramsh
Copy link

aspramsh commented Jan 15, 2021

I use MudButton. I would like to customize for example text color, when I do it with Style attribute or write it in
app.css everything works just fine. But when I move the styling to my component's .css it does not work. Here is my style

.mud-button-text { color: black; }

Actually when I change MutButton to a common button CSS isolation works just fine.

@aspramsh aspramsh added the bug Something does not work as intended/expected label Jan 15, 2021
@HClausing
Copy link
Member

It can be related to your project. It's created on .net 5 or upgraded from net core 3.x ?

On my last project creation, the css declared link was changed to: <link href="ProjectName.styles.css" rel="stylesheet" /> on head tag.

I have some projects upgraded from .net core 3.x that don't work with CSS isolation.

@aspramsh
Copy link
Author

My project has initially been created with .Net 5.

@henon
Copy link
Collaborator

henon commented Jan 16, 2021

@porkopek do you know a solution to this?

@porkopek
Copy link
Contributor

Hard to tell without seeing the code, but maybe is specificity related
1). Check in the browser Developer Tools if when creating the same rule it gets applied. With CSS isolation, you also have to take into account CSS specificity. So, in this page, when you create the rule .mud-button-text{color:yellow}, the text of this button gets yellow?
2). Is your button a text-button with no color set? Because if color is set, like Color.Primary, your rule has a lower specificity, because the <MudButton Color="Color.Primary" /> has

.mud-button-text.mud-button-text-primary {
    color: var(--mud-palette-primary);
}

that is more specific than your rule
3). Try adding !important to your rule. If this way it gets applied, it's a specificity thing.

@aspramsh
Copy link
Author

Important does not help, when I do in with Style="...", it works fine, also when I add the same style with html <style></style> tag, it works.

@mikes-gh
Copy link
Contributor

@aspramsh please could you post what you have in head on your index.html page

@aspramsh
Copy link
Author

aspramsh commented Jan 18, 2021

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>Some title.</title>
    <base href="/" />
    <link href="css/app.css" rel="stylesheet" />
    <link href="ProjectName.styles.css" rel="stylesheet" />
    <!--Mud blazor-->
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
    <link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
</head>

@porkopek
Copy link
Contributor

I keep thinking this is a specificity thing, that's because with Style works.
For starters, you have your stylesheets before MudBlazor ones, and you should have it after. I think this is the issue
Did you do this:

  1. Check in the browser Developer Tools if when creating the same rule it gets applied. With CSS isolation, you also have to take into account CSS specificity. So, in this page, when you create the rule .mud-button-text{color:yellow}, the text of this button gets yellow?

@HClausing
Copy link
Member

HClausing commented Jan 18, 2021

ProjectName

At this line:

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

Keep in mind that the "ProjectName" must match our project's name. It's just an example when I wrote this on trying to help you.

Real example, on a project called "CAE.Server":

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@(Resources.String.MainLayout.Title) - Central de Aplicativos Eficaz</title>
    <base href="~/" />
    <link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
    <script src="_content/MudBlazor/MudBlazor.min.js"></script>
    <link href="_content/EficazFrameworkCore.Blazor/css/efcore-styles.min.css" rel="stylesheet" />
    <link href="//fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
    <link href="CAE.Server.styles.css" rel="stylesheet" />
</head>

@aspramsh
Copy link
Author

Sure, it is okay, CSS isolation works for other components for example <button></button> but not for MudBlazor components.

@porkopek
Copy link
Contributor

porkopek commented Jan 18, 2021

button is not styled, so it can work. MudBlazor components are all styled, and if your rule has lower precedence than MudBlazor ones, is not going to be applied. So you have to ensure higher specificity.
First of all, put your stylesheet under the MudBlazor one, like in @HClausing example

@HClausing
Copy link
Member

Tried here and can't get it working either on MudButton, but if I change the last attribute on MudButton manually it works:

image


image

To:

image

==================

By the moment, I suggest you use MudThemeProvider and customize the colors.
Example: https://mudblazor.com/customization/theming/overview

@HClausing
Copy link
Member

@aspramsh , try to use ::deep after you class declaration:

::deep .mud-button-text { color: black; }

It worked for me.

Special thanks for @zHaytam for the help.

@henon henon added answered question Further information is requested and removed bug Something does not work as intended/expected labels Jan 19, 2021
@porkopek
Copy link
Contributor

This could shed more light on this issue
dotnet/aspnetcore#29409

@henon
Copy link
Collaborator

henon commented Jan 19, 2021

@porkopek do you fully understand the problem now? I read it but I didn't get it ;)

@henon
Copy link
Collaborator

henon commented Jan 19, 2021

Nevermind, I got it now after your explanation on gitter:

Porkopek @porkopek Jan 18 20:50
It seems to me that it's a bug in the way the RenderTreeBuilder makes the component
With an span element, it makes this code
__builder.OpenElement(0, "span");
__builder.AddAttribute(1, "b-h03bphfufo");
Where "b-h0..." is the unique identifier
that makes CSS isolation possible
But when the top element it's a component, in this case TgmTable, it generates this
__builder.OpenComponent<TgmTable>(0);
__builder.AddAttribute(1, "DataSource",
So no "b-h0..." attribute it's applied

@porkopek
Copy link
Contributor

The problem of the OP I can't be sure what it is, because she didn't answer to the questions I make her, nor show us all the code, but what I learnt from this issue is that if you have a component from an external library and it is used outside of scope of the isolation, it won't work, so you need to wrap it in a <span> for example.

For ::deep to work, it has to attach itself to an html element, it won't do it to a component.

If you have a page like

@page "/index"
<Table Datasource="@Datasource"/>

a component Table.razor that is like

<table>
...
</table>

the Index.razor.css file is

::deep table{
  border:2px solid red;
}

What the isolation produces is something like

[a GUID here] table{
  border:2px solid red;
}

but your component Table doesn't have the [a GUID here] attribute, so doesn't get applied.

However, if you wrap it in a div

<div>
  <Table Datasource="@Datasource"/>
</div>

The blazor/razor engine gives to this div the [a GUID here] attribute, so the CSS rule works

<div [a GUID here]>
  <Table Datasource="@Datasource"/>
</div>

@aspramsh
Copy link
Author

In this case the rule gets applied to the div, but I need to override mud-blazor styles. For example I would like to have nav-items text in white, here is my CSS

.mud-nav-link-text {
    color: white !important;
}

This does not work in NavMenu.razor.css file, when I put navigation component, b-5fjnqwri5o applies to my div element, but the
text color does not change.

@porkopek
Copy link
Contributor

Please, show us the markup.
Also, I don't see the ::deep in your css rule

@HClausing
Copy link
Member

In this case the rule gets applied to the div, but I need to override mud-blazor styles. For example I would like to have nav-items text in white, here is my CSS

.mud-nav-link-text {
    color: white !important;
}

This does not work in NavMenu.razor.css file, when I put navigation component, b-5fjnqwri5o applies to my div element, but the
text color does not change.

Please try this:

::deep .mud-nav-link-text {
    color: white !important;
}

@aspramsh
Copy link
Author

@HClausing that helped, thank you.

1 similar comment
@aspramsh
Copy link
Author

@HClausing that helped, thank you.

@henon
Copy link
Collaborator

henon commented Jan 19, 2021

I know @Garderoben thinks our documentation should not be a "How-To-Blazor-Wiki" but I would make an exception for this and document it (i.e. under Customization) for future reference because I have seen it comes up many times on the chat and we can then simply post the url of the page.

@henon
Copy link
Collaborator

henon commented Jan 20, 2021

I discovered a similar question on Stackoverflow and answered with what I learned from you guys. Hope it is all correct, if not, please let me know.

@porkopek
Copy link
Contributor

Enlightening others could never be wrong :-)

@henon henon closed this as completed Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants