简体   繁体   English

背景图像不显示

[英]background image is not displaying

I am trying to display a background image in my CSS and have used the relative path to the image.我试图在我的 CSS 中显示背景图像并使用了图像的相对路径。 I have tried using different browsers but nothing seems to work.我尝试使用不同的浏览器,但似乎没有任何效果。 Below is my HTML.下面是我的 HTML。

<header>
  <h2><a href="#">Rachel Doyle Studio</a></h2>
  <nav>
    <li><a href="#">Home</a></li>
    <li><a href="#">Embroidery</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </nav>
</header>
<section class="thimble">
  <div class="background-image" style="background-image: url(assets/img/art-arts-and-crafts-bright-colours-2564890.jpg)"></div>

  <div class="thimble-content-area">
    <h1>Rachel Doyles Studio</h1>
  </div>
</section>

and below is the CSS styling for the class of background-image.下面是 background-image 类的 CSS 样式。

.thimble background-image {

position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
z-index: -1;
}

I have attached a picture of the structure of my files.我附上了我的文件结构的图片。 项目结构

Why is it doing this even though I have the correct path to jpeg.为什么即使我有正确的 jpeg 路径,它也会这样做。

You are missing a dot before background-image class name: 您在背景图片类名称前缺少一个点:

.thimble .background-image {

position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
z-index: -1;
}

You can use this code您可以使用此代码

 body { margin: 0; } .thimble .background-image { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-size: cover; background-image: url('https://www.w3schools.com/images/picture.jpg'); background-repeat: no-repeat; z-index: -1; }
 <header> <h2><a href="#">Rachel Doyle Studio</a></h2> <nav> <li><a href="#">Home</a></li> <li><a href="#">Embroidery</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </nav> </header> <section class="thimble"> <div class="background-image"></div> <div class="thimble-content-area"> <h1>Rachel Doyles Studio</h1> </div> </section>

I think you need to add ../ to the path you're specifying 我认为您需要在指定的路径中添加../

<div class="background-image" style="background-image: url(../assets/img/art-arts-and-crafts-bright-colours-2564890.jpg)"></div>

This should work now.. 现在应该可以工作了。

Based on the code given, I don't think you need a separate div for the background image, either. 根据给出的代码,我也不认为背景图像也需要单独的div。 You don't need to position it if you put the background image on .thimble directly: 如果将背景图像直接放在.thimble上,则无需定位它:

.thimble {
  background-size: cover;
}
<section class="thimble" style="background-image: url(assets/img/art-arts-and-crafts-bright-colours-2564890.jpg)">
  <div class="thimble-content-area">
    <h1>Rachel Doyles Studio</h1>
  </div>
</section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM