4

In my application I have next problem. I created master page and some content pages, some of which are located in the nested folders. In the master page I added the link to .css file

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

But pages are located in the nested folders can't use this .css file. How can I fix this? I want to have one .css file for all pages (:

Thanks!

5 Answers 5

10
<link href="~/default.css" rel="stylesheet" type="text/css" />
Sign up to request clarification or add additional context in comments.

6 Comments

It doesn't work. Maybe link to css isn't the server side content, and this link couldn't work on client side..
It doesn't work. Maybe link to css isn't the server side content, and this link couldn't work on client side..
Sorry, you are right! If you add 'runat=server' to your body tag, it will work! so, your answer is most correct!! thanks!
This didn't work for me even with 'runat="server"'. I needed also to add ResolveURL in the href. So, here is the result <link id="Link1" href='<%= ResolveUrl("~/css/site.css") %>' rel="stylesheet" media="screen" type="text/css"/>
For a very nice & simple example scroll down to solution by wfz.
|
7

This problem can be resolved by adding next code in master page

<style type="text/css" runat="server">
    @import '<%= ResolveUrl("~/default.css")%>';
</style>

But designer of VS can't process this and you couldn't view your styles in it.

1 Comment

This worked great for me (VS2013)... after trying ones above that did not work. Also has advantage of being very simple!
3

The css shouldnt be relative to the master page but rather it should be relative to the location of the Page instance using the master page. In most cases this will be the same thing but I would always try to use either a fully qualified path or a site relative path

Fully qualified path

<link href="http://some.site.com/mysite/styles/default.css" rel="stylesheet" type="text/css" /> 

or a relative path (note this might not work if you have a version which can only host one site but many apps such as WinXP)

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

Win xp relative path

<link href="/path/to/application/default.css" rel="stylesheet" type="text/css" />

Comments

3

If you using website under website, change in subfolder masterpage CSS link

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

change with below

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

2 Comments

This is the best solution: Just link to the style sheet with a path relative from the location of the master page. ASP.NET will fix the path when generating the HTML. (It might be necessary to add runat="server" to the head element on the master page.)
Unfortunately, this doesn't seem to work for the src-attributes of javascript and image resources.
2

The way you defined you style sheet means: the style sheet is in the same folder as the page which uses it.

If you want to have one style sheet for all pages you should put in in one place (I prefer /assets/css folder in the application root) and define the path using this folder:

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

The other way to archieve this is to use Themes, in this case styles will be added automatically.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.