skip to Main Content

I’m facing an issue with my Blazor project (using .Net 8) where the .razor.css file is not being recognized by the corresponding Razor component.
Issue: The styles defined in MyComponent.razor.css are not being applied to MyComponent.razor. I have tried the following steps to resolve the issue:

1.Ensured that the .razor.css file has the same name as the .razor file and is in the same folder.

2.Cleaned and rebuilt the solution in Visual Studio.

3.Verified the inclusion of the styles in index.html by adding:
<link rel="stylesheet" href="MyComponent.razor.css" />
Despite these efforts, the styles are still not being applied. Any suggestions on what might be causing this issue and how to resolve it would be greatly appreciated.

MyComponentBase.razor :

@inherits MyComponentBase

@foreach (var item in Products) {
    <div class="col-md-3 mb-2">
        <a href="/ProductDetails/@item.Id">
            <div class="card">
                <img class="img-thumbnail img-hover" src="@item.ImageURL">
                <div class="card-body">
                    <h5 class="card-title mb-3">
                        @item.Name
                    </h5>
                    <p class="card-text">
                        <b>@item.Price.ToString("C")</b>
                    </p>
                </div>
            </div>
        </a>
    </div>
}

MyComponentBase.razor.css :

a {
    text-decoration: none;
}

    a:hover .card {
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
        transition: box-shadow 0.3s ease-in-out;
    }

.card {
    transition: box-shadow 0.3s ease-in-out;
    overflow: hidden;
}

a:hover .img-hover {
    transform: scale(1.07);
    transition: 0.5s ease-in-out;
}

.img-hover {
    transition: 0.5s ease-in-out;
}

2

Answers


  1. According to your description "Verified the inclusion of the styles in index.html by adding: ", we could know that you are working on a blazor web assembly application. And you are creating Component.razor.css so that I’m afraid you are trying to scope CSS to corresponding Razor components and this is called CSS isolation as we know.

    To enable CSS isolation in our blazor application, we don’t need to add <link rel="stylesheet" href="MyComponent.razor.css" />, we just need to make sure <link href="{ProjectName}.styles.css" rel="stylesheet" /> is added. And the project created by template will have this link by default.

    In the meantime, even if we added this extra line <link rel="stylesheet" href="MyComponent.razor.css" />, it won’t affect the app except an 404 error. Please see my test result below. ProjectName.style.css is loaded and the css we defined in .raozr.css just over there and the request for Component.razor.css returned 404. The hover style is applied successfully too. By the way, the style name is in format b-{string}, if you can’t see it, there shall be something wrong within the build progress as CSS isolation occurs at build time.

    enter image description here
    enter image description here

    Login or Signup to reply.
  2. In a Blazor Wasm standalone app, in index.html, you have

        <!-- If you add any scoped CSS files, uncomment the following to load them
        <link href="BlazorApp1.styles.css" rel="stylesheet" /> -->
    

    so make sure you have just

     <link href="YourAppname.styles.css" rel="stylesheet" /> 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search