Search code examples
asp.netcssascx

How can I register a css page from an ascx control?


How can I register a css code block inside an ascx control?

Can I just have

<head id="head" runat="server">
    <style type="text/css">
        .customClass
        {
        background-color: Lime;
        }

        </style>
</head>

Anywhere in ascx page? I doesn't seem to work?


Solution

  • You have three options to insert CSS into a page:

    • External style sheet
    • Internal style sheet
    • Inline style

    The style element must be enclosed within the head element. If you are attempting to style elements contained within a user control you can use any of these three options. As a note (mostly my opinion) inline styling is 99.9% of the time the wrong decision.

    One option is to expose a ContentPlaceHolder in your Site.Master inside the head section. Then using this ContentPlaceHolder on pages where you use your user control you'll be able to place a link element specifying a style sheet for your user control.

    Another option is to simply put the styling rules for your user control in the style sheet used for your entire site.