母版页中的 ASP.NET CSS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2707800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET CSS file in master page
提问by w1z
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
在我的应用程序中,我遇到了下一个问题。我创建了母版页和一些内容页,其中一些位于嵌套文件夹中。在母版页中,我添加了 .css 文件的链接
<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 (:
但是位于嵌套文件夹中的页面不能使用这个 .css 文件。我怎样才能解决这个问题?我想为所有页面创建一个 .css 文件(:
Thanks!
谢谢!
回答by Tim Mahy
<link href="~/default.css" rel="stylesheet" type="text/css" />
回答by w1z
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.
但是 VS 的设计师无法处理这个,您无法在其中查看您的样式。
回答by Guven
If you using website under website, change in subfolder masterpage CSS link
如果您使用网站下的网站,请更改子文件夹母版页 CSS 链接
<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
change with below
与下面改变
<link href="../Styles/Site.css" rel="stylesheet" type="text/css" />
回答by Mauro
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
css 不应与母版页相关,而应与使用母版页的 Page 实例的位置相关。在大多数情况下,这将是同一件事,但我总是尝试使用完全限定路径或站点相对路径
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)
或相对路径(请注意,如果您的版本只能托管一个站点但可以托管许多应用程序(例如 WinXP),则这可能不起作用)
<link href="/default.css" rel="stylesheet" type="text/css" />
Win xp relative path
Win xp 相对路径
<link href="/path/to/application/default.css" rel="stylesheet" type="text/css" />
回答by Alex
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:
如果你想为所有页面使用一个样式表,你应该放在一个地方(我更喜欢/assets/css
应用程序根目录中的文件夹)并使用这个文件夹定义路径:
<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.
存档的另一种方法是使用Themes,在这种情况下,将自动添加样式。