CSS and images on Master Page - css

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" />

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 />

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.

Fairly sure this will work
<link href="/css/style.css" rel="stylesheet" type="text/css />
/ takes you to the root of your site

You can use the tilde to get the link to work from anywhere. This will work in Images as well.
<link runat="server" href="~/css/style.css" rel="stylesheet" type="text/css />

Images in CSS are relative to the file they are referenced from.
(An exception from this is the "filter" rule in Internet Explorer which is used for PNG fixes. The images in this case are relative to the HTML document.)

Yes, the problem is that the materpage is using a relative url to load the CSS:
"../css/style.css"
you need to change this to the site root (depending on the location of your css files) something like:
"/css/style.css"
than all the various folder levels can use the same url.

Actually, master pages will rebase css files for you automatically, without you having to add runat="server". Be sure that your css file is located one directory down in the folder you specified.
You can use an absolute path to the css file, but visual studio doesn't seem to render the styles in design view when you do this. Also, sometime you won't know if you're going to be running in a virtual directory, so it's not always ideal to use the absolute path.
Also, use relative links to your image assests from the css file itself - which will work irrespective of how you link to your stylesheet.

You might also be interested in looking into themes and skins.
ASP.NET Themes and Skins Overview

Related

How to use external style sheet in webmatrix

I have a problem with my webmatrix in using an external style sheet,the page still loading and nothing appear,but when I put the css I used in a style tag inside the head tag the problem is solved , but i need to put them in an external file ... how ?
why I faced that problem ?
There isn't anything you do differently in WebMatrix for this (except that you may want to prepend the path to your file with a tilde (~) which has the effect of telling the server-side code to make an absolute path out of a relative one in ASP.Net Web-Pages [not sure about Web-Forms or MVC]).
You just do this:
In your HTML Page (in the <head> section):
<link href="~/someDirectory/someCSSFile.css" rel="Stylesheet" type="text/css" />
Then just make sure the css path and file name are correct, and you're good to go.

jQuery-ui: Differences between local and remote inclusion of stylesheet

When I include a jquery-ui stylesheet for a resizable jquery-ui dialog remotely like so
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css" />
I get this result:
but when I download the very same file and include it locally like so
<link rel="stylesheet" type="text/css" href="./jquery-ui.css" />
I get this result:
Note the missing resize handle in the lower right corner which seems to be the only difference.
What causes the difference?
Jquery-ui references a bunch of image sprite sheets. When including the reference from ajax.googleapis.com the path to the image resolves properly because google hosts those images on their server and has them in the proper location. However in your local copy of the jquery-ui.css i'm guessing there's a good chance your images are not in the proper location.
I'm using jquery ui in a current project and my folder structure is:
Content
images <--- this is the jquery ui sprite iamges folder
jquery-ui.css
go to /jquery-ui.css and make sure the bg paths are path correctly

Including CSS in MasterPages

I am new to the domain. I want to include a CSS file in my master pages,but it is not working can anyone try to help me out of this problem..
I give the link to the CSS externally as
<link href="Stylesheet1.css" rel="Stylesheet1" type="text/css" />
Is there any necessity to include CSS classes in master page if so how and where I have to include?
Try:
<link href="~/Stylesheet1.css" rel="Stylesheet1" type="text/css" />
Tilde (~) represent root directory.
If this doesn't work, if you can show us your directory structure, and look at the rendered source we can help more.
You can of course not use the Tilde (~) on tags that are not run server-side, what you have to do is something like this:
<link href="<%= ResolveClientUrl("~/Stylesheet1.css")%>" rel="stylesheet" type="text/css" />
Or just do a method that gives you back a full link-tag.
There's nothing wrong with the syntax of your <link> tag. The problem is likely to be caused by the fact that your CSS file isn't located at the URL you're attempting to get it from.
The href attribute in your markup specifies a file called Stylesheet1.css in the same folder as your master page. If its not you should specify the location of the stylesheet using standard, virtual path or move the stylesheet to the same folder as your masterpage.
More info.
http://w3schools.com/css/css_howto.asp

Why the CSS style is not applied in webpages within subdirectories?

In the root of my domain i have the CSS file style.css and the masterpage file site.master.
The link to the CSS file within the site.master is
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
The problem is that webpages contained within subdirectories do not inherit the CSS file.
What am i doing wrong here?
If i copy the style.css file to the subdirectories everything works like a charm...
UPDATE: If i change the path to /style.css or to ~/style.css the style is Not applied also to the webpages within the root folder.
MasterPages use the containing page for the path.
change your css tag to be a server control and use the "~" root symbol.
<link id="lnkStyle" runat="server" href="~/style.css"
rel="stylesheet" type="text/css" />
You need to specify the path as /style.css.
Well the obvious question is, does the other pages inherit the correct masterpage, namely the one with your css link?
ie.
<%# Page Language="C#" MasterPageFile="~/MasterPage.master"...
also perhaps '/' before file name would help
If you're including that link tag in your master page, the HTML being output by the content pages in subdirectories contains a link to "style.css", which the browser looks for in that directory.
If you're developing in ASP.NET, you should be placing your CSS files in themes, which will take care of this problem. If you really don't want to do that for some reason, make the URL to the stylesheet an application-relative path ("~/style.css") and make the link tag executed on the server; I believe that that will resolve the application path and generate an absolute URL.
Try this:
<link href="<%: Url.Content("~/style.css") %> rel="stylesheet" type="text/css" media="screen" />
It will make the path relative to your hostname, giving the correct path to your pages in the subdirectories. Right now, just linking style.css gives it the folder relative path, so its looking inside of the same folder your page is in, instead of where you intended. Hope that helps. You could also re-write your link with a preceding forward slash, like "/style.css" and that should also do the trick.

Why is CSS applied only into my master page?

In the head portion of my Master page i have a link to an external CSS file
<link href="style.css" rel="stylesheet" type="text/css" />
Although i am able to apply the style in child pages in design time...
<asp:Label ID="Label" runat="server" CssClass="BodyText" Text="This is a link"></asp:Label>
...in run time child pages have no style.
So, What am i missing here?
If your child pages are in a subdirectory, they'll expect the style sheet in that directory as well. changing the reference to the style sheet to ../style.css or /style.css should help.
The path to the CSS file (and any other file - images, javascript, etc) is relative to the page (the page address in the browser). If the master page is in a different folder than the page, then the css file may not be found.
Try using either an absolute path, a path relative to the root, or a path to the CSS file like this:
<link href="~/style.css" rel="stylesheet" type="text/css" />
Try using the root operator "~" for stylesheets in your master page:
<link type="text/css" href="~/css/style.css" rel="stylesheet" />
ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.

Resources