223

I know how to set .css files on the _Layout.cshtml file, but what about applying a stylesheet on a per-view basis?

My thinking here is that, in _Layout.cshtml, you have <head> tags to work with, but not so in one of your non-layout views. Where do the <link> tags go?

MrBoJangles
  • 12,127
  • 17
  • 61
  • 79

6 Answers6

388

For CSS that are reused among the entire site I define them in the <head> section of the _Layout:

<head>
    <link href="@Url.Content("~/Styles/main.css")" rel="stylesheet" type="text/css" />
    @RenderSection("Styles", false)
</head>

and if I need some view specific styles I define the Styles section in each view:

@section Styles {
    <link href="@Url.Content("~/Styles/view_specific_style.css")" rel="stylesheet" type="text/css" />
}

Edit: It's useful to know that the second parameter in @RenderSection, false, means that the section is not required on a view that uses this master page, and the view engine will blissfully ignore the fact that there is no "Styles" section defined in your view. If true, the view won't render and an error will be thrown unless the "Styles" section has been defined.

MrBoJangles
  • 12,127
  • 17
  • 61
  • 79
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
24

I tried adding a block like so:

@section styles{
    <link rel="Stylesheet" href="@Href("~/Content/MyStyles.css")" />
}

And a corresponding block in the _Layout.cshtml file:

<head>
<title>@ViewBag.Title</title>
@RenderSection("styles", false);
</head>

Which works! But I can't help but think there's a better way. UPDATE: Added "false" in the @RenderSection statement so your view won't 'splode when you neglect to add a @section called head.

MrBoJangles
  • 12,127
  • 17
  • 61
  • 79
17

Using

@Scripts.Render("~/scripts/myScript.js")

or

@Styles.Render("~/styles/myStylesheet.css")

could work for you.

https://stackoverflow.com/a/36157950/2924015

Community
  • 1
  • 1
Nishanth Shaan
  • 1,422
  • 15
  • 18
  • 4
    Please do not post links to [duplicate answers](//meta.stackexchange.com/a/211726/206345). Instead, consider other actions that could help future users find the answer they need, as described in the linked post. – Mogsdad Mar 22 '16 at 15:12
3

layout works the same as an master page. any css reference that layout has, any child pages will have.

Scott Gu has an excellent explanation here

shanethehat
  • 15,460
  • 11
  • 57
  • 87
BentOnCoding
  • 27,307
  • 14
  • 64
  • 92
1

I prefer to use the razor html helper from Client Dependency dll

Html.RequireCss("yourfile", 9999); // 9999 is loading priority 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
john blair
  • 466
  • 5
  • 13
0

You can this structure in _Layout.cshtml file

<link href="~/YourCssFolder/YourCssStyle.css" rel="stylesheet" type="text/css" />
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
mofidul
  • 119
  • 3
  • 11
  • 3
    How does this allow me to selectively apply a style sheet per view? – MrBoJangles Mar 31 '17 at 16:45
  • You can also add a class or id to each section, and inside the header you could have a styles like the one mentioned by mofidul. What I do is I work with sass, so each view has a separate class container. In that way I created sass pages for each section, which in the end is more structured and organized. – Leo Jun 07 '18 at 07:53
  • what is the meaning of rel="stylesheet"? – CleanCoder Apr 23 '20 at 18:27
  • 1
    @SvenKrauter `` can also be used for things other than stylesheets (ie, prefetching next pages, favicons), so `rel="stylesheet"` tells the browser that the referenced file is a stylesheet. – jao Oct 06 '20 at 08:43