Search code examples
asp.netmaster-pagesasp.net-2.0

How to include CSS in master pages?


How do I include CSS reference in only certain pages on my asp.net website? If I include the reference in my master page, all pages of the website share the CSS reference.


Solution

  • Just add a CSS ContentPlaceHolder with a default value in it.

    Basically, the CSS file you specify as default will be included unless you override that placeholder with an <asp:Content /> tag from a child page.

    Your Master Page should look something like this.

    <head>
        <asp:ContentPlaceHolder ID="Stylesheets" runat="server">
            <link rel="stylesheet" href="/css/master.css" type="text/css" />
        </asp:ContentPlaceHolder>
    </head>
    

    Then from any pages using that Master Page, you can simply override that with a different stylesheet.

    On (example) AboutUs.aspx

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