背景图片不显示

background image is not displaying

我正在尝试在我的 CSS 中显示背景图片,并使用了图片的相对路径。我尝试过使用不同的浏览器,但似乎没有任何效果。 下面是我的 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>

以下是背景图像 class 的 CSS 样式。

.thimble background-image {

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

我附上了我的文件结构的图片。

即使我有正确的 jpeg 路径,为什么它会这样做。

您在 background-image class 姓名前少了一个点:

.thimble .background-image {

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

我认为您需要将 ../ 添加到您指定的路径中

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

现在应该可以了..

根据给出的代码,我认为您也不需要单独的 div 作为背景图片。直接把背景图放在.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>

您可以使用此代码

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>