Details

Blog


Id
5
Title
Page Specific CSS in Core MVC
Author
me
Summary
How to specify the cascading style sheet for one html page in ASP.NET Core MVC
Content
<p>Say you are creating a website that has different pages (in my case a blog), And on one of those pages you need to include a specific style sheet that the rest of the pages do not use.&nbsp; You are in luck, because it can be done easily with sections.&nbsp;&nbsp;</p> <p>In the view that needs a different CSS, create a section and put the CSS in it like so:</p> <p><code>@section cssSection { &lt;link rel="stylesheet" href="~/css/myStyle.css"&gt; }</code></p> <p>In the shared layout view (_Layout.cshtml), render the section inside of the header:</p> <p><code>&nbsp; @RenderSection("cssSection", false)</code></p> <p>The first parameter is the section name we defined in our view. The second parameter says whether or not rendering the section is required.</p> <p>And that's all there is to it!&nbsp; The css should now be included with your html page.</p>
Image
AboutImage
Edit | Back to List