2017-03-14 129 views
0

所以我想通過一個單獨的CSS文件調用背景圖像。 HTML文件和CSS文件位於同一級別,但背景圖像的級別較低。爲什麼我的背景圖片不會顯示使用CSS?

例子:

的index.html

stylesheet.css中

資源>資產>背景圖像

這裏是我的html:

<div class="second-background"> 
     <h2>Our</h2> 
     <div class="purpose-heading"><h2>Purpose</h2></div> 
     <p class="textstyle6">To provide unaccompanied runaway and homeless youth with a safe and nurturing environment where they can develop the needed skills to become active, healthy, successful members of our future world.</p> 
     <div id="rectangle"><p class="textstyle2">7,085 MEALS SERVED. 511 DROP-IN SERVICES.<br/>245 STREET OUTREACH HOURS. 64 SHELTERED YOUTH.</p></div> 
    </div> 

這裏是我的CSS:

.second-background { 
background-image:url("../resources/assets/purpose.png"); 
} 
#rectangle { 
width: 1110px; 
height: 105px; 
background-color: #ffffff; 
} 

回答

2

路徑錯誤,從css中的圖像路徑中刪除../。如果結構像你說的那樣。

瞭解更多有關文件路徑在這裏:https://www.w3schools.com/html/html_filepaths.asp這裏:https://css-tricks.com/quick-reminder-about-file-paths/

+0

太感謝你了。我以爲我曾嘗試過,但顯然不是。它工作完美。所以我的問題是,你什麼時候使用「../」 – smithky3

+1

../是上面的一級。如果css位於自己的文件夾中,情況就會如此。檢查我添加的CSS技巧鏈接,這完全解釋了它。 –

1

路徑規則打破這樣的:../意思是「上去一平」(即,到包含的文件夾 樣式文件夾);
圖片/表示「轉到圖片文件夾」;和bg.gif 指定該文件。

根據您所提供的文件夾結構: 正確的方法是這樣的:

  .second-background { 
       background-image:url("resources/assets/purpose.png"); 
      } 
      #rectangle { 
      width: 1110px; 
      height: 105px; 
       background-color: #ffffff; 
      } 

希望它可以幫助