用另一张图片覆盖身体背景

时间:2016-12-02 14:04:08

标签: html css

我试图通过另一张图片掩盖html正文背景图片。我使用z-index但无法使其正常工作。我错过了什么或者只是无法理解我在做什么。

请参阅以下示例代码:

body, html {
    margin: 0;
    /* The image used */
    background-image: url('http://lorempixel.com/output/nature-q-c-1176-907-10.jpg');

    /* Full height */
    height: 100%; 

    /* Create the parallax scrolling effect */
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
}

#main-content {
    background-image: url('http://lorempixel.com/output/food-q-c-640-633-1.jpg');
    z-index: 10;
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    position: relative;
}

.content {
    /*height: 700px;*/
    height: 100vh;
    background:rgba(255,255,255,0.5);
    /*margin-top: 20%;*/
}
<div id="main-content">
  <div class="content">something in here..</div>
  <div class="content">something in here..</div>
  <div class="content">something in here..</div>
</div>

  

另外,main-content内部是100vh和\ n的div   background opacity

主要内容图片应位于所有内容之上,包括具有背景不透明度的content div。

2 个答案:

答案 0 :(得分:1)

100%上使用#main-content宽度和高度,例如:

#main-content {
  width: 100%;
  height: 100%;
}

请看下面的代码段:

body, html {
    margin: 0;
    /* The image used */
    background-image: url('http://lorempixel.com/output/nature-q-c-1176-907-10.jpg');

    /* Full height */
    height: 100%; 

    /* Create the parallax scrolling effect */
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
}

#main-content {
    background-image: url('http://placehold.it/500x500');
    z-index: 10;
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    position: relative;
    width: 100%;
    height: 100%;
}
<div id="main-content"></div>

希望这有帮助!

答案 1 :(得分:1)

如果使用CSS定义div的高度,它将起作用。或插入内容以使其垂直扩展。

背景图片对于包含更多内容的元素有意义,否则您可以使用&lt; img&gt;

插入图片