2017-03-14 132 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; 
      } 

希望它可以帮助