Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS and images on Master Page

I have this quite popular problem, but have failed to find a solution that works.

Basicly, I am using a Master Page (/Masterpages/Default.master), that includes

<link href="../css/style.css" rel="stylesheet" type="text/css />

And it also includes some images with the same relative linking.

But when I apply the Master Page to content pages (in diffrent folderlevels) the css formating and images is lost.

Is there anyway to dynamicaly solve the folderlevel links to css and images to all content pages using the masterpage?

Thanks in advance

UPDATE:

There is an additional problem. It's tricky to get the output to render correctly in both the browser and in design view in Visual Studio.

I got it to work by using the asp:image solution for the images in the masterpage and by double linking the css in the masterpage, one to make it render in VS and one to make it render correctly browsing the site.

<link href="../css/style.css" rel="stylesheet" type="text/css" />
<link href="<%=ResolveUrl("~/css/style.css")%>" rel="stylesheet" type="text/css" />
like image 518
Andreas Avatar asked Oct 13 '09 11:10

Andreas


People also ask

What is difference between master page and content page?

The master page establishes a layout and includes one or more ContentPlaceHolder controls for replaceable text and controls. The content page includes only the text and controls that are merged at run time with the master page's ContentPlaceHolder controls.

What does a master page contain?

What is a Master Page? A Master Page is a nonprinting page that you can use as the template for the rest of the pages in your document. Master pages can contain text and graphic elements that will appear on all pages of a publication (i.e. headers, footers, page numbers, etc.)

What is the relationship between master and content page?

master page. In the content page, you create the content by adding Content controls and mapping them to ContentPlaceHolder controls on the master page. For example, the master page might have content placeholders called Main and Footer.

What is master page what contents can be added into master page?

Master pages typicially contain page headers, footers, margin and column guides, and other elements that occur on multiple pages in your document. Every new document automatically contains one master page called “A-Master,” and this master page is automatically applied to every page in the document.


2 Answers

best to use:

<link href="<%=ResolveUrl("~/css/style.css") %>" rel="stylesheet" type="text/css />

since this will cope with iis application roots unlike:

<link href="/css/style.css" rel="stylesheet" type="text/css />
like image 140
Richard Avatar answered Sep 19 '22 09:09

Richard


You can make your link runat="server" and use tilde mapping to make the CSS path relative to the site root.

<link runat="server" id="siteStyle"
      href="~/css/style.css"
      rel="stylesheet"
      type="text/css" />

The images referenced in the CSS should be relative to the location of the CSS file and should resolve normally once the CSS file itself is included properly. For images in tags on the page, you would need to use the ASP:Image control and, again, use the tilde mapping for a path relative to the root.

like image 23
tvanfosson Avatar answered Sep 20 '22 09:09

tvanfosson