为什么子目录下的网页不应用CSS样式?

发布于 2024-09-25 14:02:06 字数 444 浏览 4 评论 0原文

在我的域的根目录中,我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我还不会笑 2024-10-02 14:02:06

MasterPages 使用包含页面作为路径。

将 css 标记更改为服务器控件并使用 "~" 根符号。

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

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" />
山有枢 2024-10-02 14:02:06

您需要将路径指定为 /style.css

You need to specify the path as /style.css.

深居我梦 2024-10-02 14:02:06

那么明显的问题是,其他页面是否继承了正确的母版页,即带有您的 css 链接的母版页?

IE。

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"...

也许文件名前的“/”会有所帮助

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

睫毛上残留的泪 2024-10-02 14:02:06

如果您在母版页中包含该链接标记,则子目录中的内容页输出的 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.

提笔书几行 2024-10-02 14:02:06

试试这个:

<link href="<%: Url.Content("~/style.css") %> rel="stylesheet" type="text/css" media="screen" />

它将使路径相对于您的主机名,为子目录中的页面提供正确的路径。现在,只需链接 style.css 即可为其提供文件夹相对路径,因此它会在您的页面所在的同一文件夹内查找,而不是您想要的位置。希望有帮助。您还可以使用前面的正斜杠重写链接,例如“/style.css”,这也应该可以解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文