Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include CSS in master pages?

Tags:

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.

like image 421
Zo Has Avatar asked Oct 25 '10 11:10

Zo Has


People also ask

How do you add CSS to pages?

CSS can be added to HTML documents in 3 ways: Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section. External - by using a <link> element to link to an external CSS file.

Can I use one CSS file for multiple pages?

Yes, It is possible to include one CSS file in another and it can be done multiple times. Also, import multiple CSS files in the main HTML file or in the main CSS file. It can be done by using @import keyword.

Can we include CSS in JavaScript?

JavaScript can also be used to load a CSS file in the HTML document.

Can you include CSS in HTML?

There are three ways to add CSS to HTML. You can add inline CSS in a style attribute to style a single HTML element on the page. You can embed an internal stylesheet by adding CSS to the head section of your HTML doc. Or you can link to an external stylesheet that will contain all your CSS separate from your HTML.


2 Answers

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> 
like image 130
Marko Avatar answered Sep 25 '22 07:09

Marko


In my situation, i used the same masterpage from different locations in the solution. And since the ~ (Tilde) prefix on the reference to my css files, i added a response.write to the reference like so:

<%= ResolveUrl("~/css/myStyle.css") %> 
like image 39
theodor.johannesen Avatar answered Sep 24 '22 07:09

theodor.johannesen