HTML - 了解href以及链接和基本标记

时间:2010-10-12 00:13:28

标签: html hyperlink directory base

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

据我所知,我正在使用我的localhost,所以我有一个主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 个答案:

答案 0 :(得分:2)

通过您发现的关于href的内容,只需将有关导航的知识与最终方法结合起来:

所以如果你有这个:
本地主机/ aeac /
本地主机/ aeac /图像/
本地主机/ aeac /样式表/
本地主机/ aeac /脚本/
本地主机/ aeac /用户/

你在localhost / aeac / users / index.html 你只需要进入一个目录(../进入aeac),然后导航:

../样式表/ style.css中

希望这有帮助

答案 1 :(得分:1)

使用/作为基础

,您可以拥有样式表的绝对路径

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

答案 2 :(得分:1)

我相信你想要这个:

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

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

答案 3 :(得分:1)

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

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

无论“user”文件夹的名称是什么,.. / ..将始终返回2个文件夹:

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

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

相关问题