2010-10-12 95 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 /樣式表