尽管为标签设置了绝对和相对显示,但背景图像没有显示

background image doesn't show up although has set the absolute and relative display for the tag

这是html

</head>
<body>
    <header>
        <h2>Mountain Travel</h2>
        <nav>
            <li><a href="#">Tour</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
    </header>
    <section class="introduction">
        <div class="background-image" style="background-image: url(assets/image/main.jpg);" >

        </div>
        <div class="content-area">
            <h1>Mountain Travel</h1>
            <h3>Unmissable Adventure Tours Around The World</h3>
            <a href="#" class="btn">Contact Us Now</a>
    </div>
  </section>
</body>
</html>

这是CSS:

.introduction.background-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-size: cover;
    z-index: -1;

}
.introduction{
    position:relative;
}

但是,图像根本没有显示。我确定 url 是正确的。有人可以帮我吗?

所以首先你没有一个有效的 html 结构(你的 nav 标签没有关闭)。其次 .background-image.introduction 的子代。因此,要在 css 中访问它,您需要使用 .introduction .background-image 而不是 .introduction.background-image:

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

.introduction{
    position:relative;
}
<header>
  <h2>Mountain Travel</h2>
  <nav>
    <ul>
      <li><a href="#">Tour</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav> <!-- Don't forget to close your nav tag-->
</header>

<section class="introduction">
  <div class="background-image" style="background-image: url(https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png);" ></div>

  <div class="content-area">
    <h1>Mountain Travel</h1>
    <h3>Unmissable Adventure Tours Around The World</h3>
    <a href="#" class="btn">Contact Us Now</a>
  </div>
</section>

我在这个例子中为你的图像路径使用了一个可靠的来源(google 标志)。因此,如果在您的示例中它仍然无法正常工作,则可能是图片来源不正确。

@KoshVery 所述,请不要忘记 li 需要在 ulol html 标签中。