Search code examples
cssasp.netwebformsmaster-pages

How can I add CSS to a content page without modifying master page?


We have a masterpage for many webforms. The CSS of all these webforms is in the masterpage.

This one particular webform will use the same masterpage, but I don't want to add the CSS of this webform to the masterpage.

Considering that this page is temporary, I want to add CSS to my content page without touching the masterpage.

Is it possible to add CSS to a content page without touching the masterpage?

I do have a ContentPlaceHolder in the <head> of my master page.


Solution

  • You have a ContentPlaceHolder in the <head> section of your Master Page. So, the only thing you need to do is to specify your CSS file in this ContentPlaceHolder of your content page that uses your Master Page (no changes in your Master Page):

    <asp:Content ID="contentHead" ContentPlaceHolderID="head" runat="server">
        <link href="fileName.css" rel="stylesheet" />
    </asp:Content>
    

    Or you can use inline style:

    <asp:Content ID="contentHead" ContentPlaceHolderID="head" runat="server">
        <style>
            .yourClass { ... }
        </stlye>
    </asp:Content>
    

    When your page is rendered, the CSS of your content page will be applied after the CSS of the Master Page.