代码之家  ›  专栏  ›  技术社区  ›  rene smith

虽然为标记设置了绝对和相对显示,但背景图像不显示

  •  0
  • rene smith  · 技术社区  · 5 年前

    </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;
    }
    

    不过,这张照片根本没有出现。我确信网址是正确的。有人能帮我吗?

    1 回复  |  直到 5 年前
        1
  •  1
  •   johannchopin    5 年前

    所以首先你没有有效的 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>

    我在这个例子中使用了一个可靠的来源(谷歌标志)为您的图像路径。所以如果在你的例子中它仍然不起作用,也许图像的来源不是好的。

    @KoshVery 请别忘了 li ul ol html标记。