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