2011-05-19 43 views
1

我有以下HTML:背景不工作的一個div

<html> 
    <head> 
     <title> 
      Think in a NEW BOX. 
     </title> 

     <link rel="stylesheet" type="text/css" href="styles/default.css" /> 
    </head> 
    <body onload=""> 
     <div id="content"> 
      <div id="header"> 
       <img src="images/title-1.png" /><img src="images/title-2a.png" /><img src="images/title-3.png" /></div><div id="fcontent"> 
       hi 
hi 
      </div> 
     </div> 
    </body> 
</html> 

...下面CSS:

div#fcontent{ 
     background-image:url('images/forground.png'); 
     width:100%; 
     padding:0px; 
     margin-top:0px; 
     background-repeat:repeat-y; 
    } 

我的背景沒有顯示出來,這是爲什麼?

這裏是所有CSS的,以防萬一(問題可能是在上面的CSS代碼段,不過):

html, body { 
     background-color:black; 
     font-family:"arial bold"; 
     overflow:auto; 
     text-align: center; 
     display: inline-block; 
     color:white; 
    } 
div#content { 
     width:792px; 
     height:100%; 
     padding:0px; 
    } 

div#header { 
     height:216px; 
     width:100%; 
     padding:0px; 
     margin-bottom:0px; 
    } 

div#fcontent{ 
     background-image:url('images/forground.png'); 
     width:100%; 
     padding:0px; 
     margin-top:0px; 
     background-repeat:repeat-y; 
    } 

*   { 
     -webkit-user-select: none; 
      -khtml-user-select: none; 
      -moz-user-select: none; 
      -o-user-select: none; 
      user-select: none; 
    } 
+1

我的意思是,「前景」的確對初學者有一個「e」。 :) – pixelbobby 2011-05-19 17:17:24

+0

添加一個最小高度到你的fcontent,看看它是否工作:)你也有一個文檔類型? – 2011-05-19 17:19:00

+0

問題:當你指定背景顏色時,背景會顯示出來嗎?有人猜測你的圖像路徑是錯誤的或不存在的。 – 2011-05-19 17:19:39

回答

10

請記住,圖像的路徑是相對於CSS文件,所以如果你的CSS文件是在「樣式」文件夾等,當前被請求的圖像必須位於/styles/images/forground.png。

如果您將URL的位置更改爲絕對URL(例如從根目錄),則可以避免此類問題。

url('/images/forground.png'); 

或者你可能想跳出當前文件夾,然後將圖像文件夾:

url('../images/forground.png'); 

希望吳丹幫助。

+0

我會從來沒有預料到這是問題。我一直在同一個文件中使用CSS,這是我第一次使用外部CSS文件。非常感謝! – FreeSnow 2011-05-19 17:32:53

1

我沒有看到ID爲fcontent一個DIV,可能是首要問題。

+0

滾動到右邊,你看到它;) – 2011-05-19 17:21:36

+0

是的,我不得不把他們端到端,以消除IE喜歡投入的微小間距。 – FreeSnow 2011-05-19 17:33:22

1

我剛剛粘貼了你的代碼,它在Chrome和Firefox中適用於我。

確保您的圖像路徑正確,並且您可能需要嘗試設置高度。

0

注意的是,這是根路徑:

url('/images/forground.png'); 

總是提到:www.yoursite.com/images/forground.png

儘管這是相對路徑,你應該正常使用:

url('images/forground.png'); 
url('../images/forground.png'); 

對於您可以看看這個主題的更多信息:Background not working for a div as it should