为什么子目录下的网页不应用CSS样式?
在我的域的根目录中,我有 CSS 文件 style.css
和母版页文件 site.master
。
site.master 中 CSS 文件的链接为
问题是子目录中包含的网页不会继承 CSS 文件。
我在这里做错了什么?
如果我将 style.css
文件复制到子目录中,一切都会像魅力一样工作...
更新: 如果我将路径更改为 /style.css 或
~/style.css
样式也不会应用于根文件夹中的网页。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MasterPages 使用包含页面作为路径。
将 css 标记更改为服务器控件并使用
"~"
根符号。MasterPages use the containing page for the path.
change your css tag to be a server control and use the
"~"
root symbol.您需要将路径指定为
/style.css
。You need to specify the path as
/style.css
.那么明显的问题是,其他页面是否继承了正确的母版页,即带有您的 css 链接的母版页?
IE。
也许文件名前的“/”会有所帮助
Well the obvious question is, does the other pages inherit the correct masterpage, namely the one with your css link?
ie.
also perhaps '/' before file name would help
如果您在母版页中包含该链接标记,则子目录中的内容页输出的 HTML 包含指向“style.css”的链接,浏览器将在该目录中查找该链接。
如果您在 ASP.NET 中进行开发,则应该将 CSS 文件放置在主题中,这样就可以解决这个问题。如果出于某种原因您确实不想这样做,请将样式表的 URL 设置为应用程序相对路径(“~/style.css”)并使链接标记在服务器上执行;我相信这将解析应用程序路径并生成绝对 URL。
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.
试试这个:
它将使路径相对于您的主机名,为子目录中的页面提供正确的路径。现在,只需链接 style.css 即可为其提供文件夹相对路径,因此它会在您的页面所在的同一文件夹内查找,而不是您想要的位置。希望有帮助。您还可以使用前面的正斜杠重写链接,例如“/style.css”,这也应该可以解决问题。
Try this:
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.