Scripts and Stylesheets in Layout Pages

Kmcnet 786 Reputation points
2024-08-01T22:29:13.8533333+00:00

Hello everyone and thanks for the help in advance. I am developing an Asp.Net Core application that utilizes layout pages for things such as headers and footers. However, many of the pages using the layout page also need specific css and script files. What is the best practice to handle this? Any help would be appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,553 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tiny Wang-MSFT 2,641 Reputation points Microsoft Vendor
    2024-08-02T02:20:22.99+00:00

    Hi Kmcnet, I think CSS isolation shall be the feature you want to use. Please feel free to let us know your detailed requirement if it doesn't solve your issue. Just like what you can see in screenshot below. By default the form elements uses the styles inherit from Layout.cshtml. To cover it, I define CSS isolation style with font color and width, then we can see the width and font color used the style we defined in CSS isolation.

    User's image

    But CSS isolation also has limitations. Just like what we can see in the document. When we faced the limitation, we can only add inline style like <style> .my-input{ color:blue; } </style> in front of that cshtml file.

    CSS isolation only applies to HTML elements. CSS isolation isn't supported for Tag Helpers


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Tiny


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.