1

我经常对 href 属性、链接标签和基本标签感到困惑。我不确定如何正确链接 CSS 文件,以便文件可以移动到子文件夹中并且仍然可以工作(稍后的示例),所以我继续了解在指定位置时 href 是如何工作的。

据您所知,我正在使用我的本地主机,所以我有主htdocs文件夹,在其中,我有多个文件夹,每个文件夹用于我正在处理的项目。它最终看起来像这样:

localhost/index.php (which redirects to localhost/home/index.php)
localhost/home/
localhost/zune/
localhost/school/
localhost/aeac/

通常,文件夹的布局将遵循以下原则:

localhost/aeac/images/
localhost/aeac/stylesheets/
localhost/aeac/scripts/

继续,假设我有文件localhost/aeac/test/index.html,其中有 4 个用于测试的链接。我发现

<a href="/"> will link to "localhost/"
<a href="./"> will link to "localhost/aeac/test/" (the directory the file is in.)
<a href="../"> will link to "localhost/aeac/" (the directory above the file.)
<a href="about.html">will link to "localhost/aeac/test/about.html" (the directory the file is in.)

现在我了解了 href,我需要了解如何正确链接 CSS。

想象一下该站点的目录如下所示:

localhost/aeac/
localhost/aeac/images/
localhost/aeac/stylesheets/
localhost/aeac/scripts/

就在/aeac/我拥有的文件夹中index.html。该文件有一个如下所示的链接标签:

<link rel="stylesheet" href="stylesheets/main.css" />

所以效果很好,主要是网站的结构/主题,并包含在每个文件中。当我必须创建子文件夹时会出现问题。现在我们有一个localhost/aeac/users/*username*/index.html. 该站点仍然使用 main.css,但该链接不再起作用,因为那里没有stylesheets文件夹。

这就是我卡住的地方,我想我应该使用基本标签来解决我的问题,但我仍然对如何编写感到困惑。另外,我知道我可以更改用户文件夹中所有文件的链接标签,但我想知道如何做到这一点(如果可能的话。)

4

4 回答 4

2

根据您对 href 的了解,只需将有关导航的知识与您的最终方法结合起来:

所以如果你有这个:
localhost/aeac/
localhost/aeac/images/
localhost/aeac/stylesheets/
localhost/aeac/scripts/
localhost/aeac/users/

并且您在 localhost/aeac/users/index.html 中,您只需向上一个目录(../ 进入 aeac),然后继续导航:

../stylesheets/style.css

希望这可以帮助

于 2010-10-12T00:25:27.057 回答
1

您可以使用 / 作为基础的样式表的绝对路径

<link rel="stylesheet" href="/aeac/stylesheets/main.css" />

于 2010-10-12T00:31:08.697 回答
1

我相信你想要这个:

<link rel="stylesheet" href="/aeac/stylesheets/main.css" />

这以 开头/,因此无论您的页面位于何处(即 at/aeac/index.html或 at /aeac/users/foo/index.html),它将始终从根向上遍历。现在,如果您可以控制每个副本中的标签index.html(您可能会这样做),您也可以使用.., to向上../../stylesheets/main.css导航,但从根目录导航可能更简单。

于 2010-10-12T00:32:25.667 回答
1

您可以使用:/stylesheet/main.css

或 ../../stylesheet/main.css

无论“用户”文件夹的名称是什么,../.. 总是会返回 2 个文件夹:

/aeac/users/user1/index.html /aeac/users/user2/index.html

../../stylesheet 将始终到达 /aeac/stylesheet

于 2010-10-12T00:38:05.757 回答