2010-10-12 36 views
1

我常常感到困惑与href属性,链接标签,以及基本标记。我不知道该如何正确链接CSS文件,以便文件可以被移动到子文件夹,并且仍然正常工作(例如以后),所以我说干就干,发现了如何在href作品指定位置时。HTML - 了解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文件夹中的链接不工作了。

这就是我坚持,我想,我应该使用基本标签来解决我的问题,但我仍然对如何写的困惑。另外,我知道我可以只改变链接标签在用户文件夹中的所有文件,但我想知道如何做这种方式(如果这甚至有可能。)

回答

1

我相信你想这个基地:

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

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

2

你发现什么了约HREF,只是结合有关导航的知识与您的最终办法:

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

,你在本地主机/ aeac /用户/ index.html的 你只是去一级目录(../进入aeac),然后导航上:

../stylesheets/style。CSS

希望这有助于

1

可以使用具有样式表的绝对路径/为

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

1

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

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

无论什么 “用户” 文件夹命名,../。将始终走2个文件夹背:

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

../../stylesheet总是会得到/ aeac /样式表