6

How can I add a css file to only a child page while using ASP.NET master pages rather then specifying them in the master?

Zo Has
  • 12,599
  • 22
  • 87
  • 149

2 Answers2

14

In the master page

<head>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>

In the specific page

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
  <link href="~/pagecssfile.css" rel="stylesheet" type="text/css" />
</asp:Content>
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
  • 2
    this one is actually a lot better since the tag must be in the part, for quick-rendering and w3c-compatibility reasons. Read more here : http://stackoverflow.com/questions/1642212/whats-the-difference-if-i-put-css-file-inside-head-or-body – Trajan Jul 01 '13 at 15:09
  • 1
    this is a really neat solution, +1 – benscabbia Oct 30 '15 at 20:52
4

You can specify it in child page inside the content as mentioned below

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<link href="Css/main.css" rel="stylesheet" type="text/css" />

scope of this css will remain for this page only

Asif Mukhida
  • 142
  • 2
  • 3
    references to css and js files should all be in the section so they're loaded before the body is. Adrian Iftode's way is more w3c-compliant and more efficient. – Trajan Jul 01 '13 at 15:50