2016-03-12 53 views
0

我有以下文件夾結構如何包括來自多個目錄

website 
    \styles\ 
     main.css 
    \directory\ 
     page.php 
    \layout\ 
     background.jpg 
     common_includes.php 
    index.php 

main.css的相同的CSS文件時處理路徑中包含CSS背景圖像,例如

#container { 
    background-image: url('../layout/background.jpg'); 
} 

common_includes。 php包含所有頁面通用的CSS和JS文件。例如:

<link rel="stylesheet" type="text/css" href="styles/main.css" /> 
<link rel="stylesheet" type="text/css" href="styles/other.css" /> 
... 

index.php

<?php include_once '/layout/common_includes.php'; ?> 

一切都很完美,直到這裏。

現在我想擁有一個包含其他頁面的文件夾(/directory/page.php)。

我想page.php使用相同common_includes.php

顯然這樣做:

<?php include_once '../layout/common_includes.php'; ?> 

不因不同的工作目錄工作。

我試圖複製common_includes.php有正確的路徑:

common_includes_directory.php

<link rel="stylesheet" type="text/css" href="../styles/main.css" /> 
<link rel="stylesheet" type="text/css" href="../styles/other.css" /> 
... 

然後我進入,在CSS中background.jpg無法發現問題。

如何在不需要複製所有內容的情況下實現這一目標?

回答

0

使用絕對路徑(相對於根)

<link rel="stylesheet" type="text/css" href="/styles/main.css" /> 
<link rel="stylesheet" type="text/css" href="/styles/other.css" /> 

在CSS中使用

#container { 
    background-image: url('/layout/background.jpg'); 
} 

與任何起始路徑「/」是相對於根,無論你使用它,它仍然是相同的。 欲瞭解更多詳情,請閱讀Having links relative to root?

+0

它似乎沒有工作 –

+0

在鉻打開頁面,然後打開控制檯檢查哪個鏈接不工作。它可能有404錯誤。檢查它並顯示結果,以便我可以相應地幫助你。 –