【问题标题】:Cover a body background by another image用另一个图像覆盖身体背景
【发布时间】:2016-12-02 23:47:00
【问题描述】:

我正在尝试用另一张图片掩盖 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 的 div 和 background opacity

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

【问题讨论】:

  • 你没有给 div 任何宽度或高度,尝试添加它们
  • @Akshay,它有效!但无法让它在我的实际页面上运行。很奇怪
  • 你是怎么设置宽高的?喜欢100%px
  • @Akshay,以百分比表示。是的 100% 宽度和高度。我使用我的样本对其进行了测试,并且可以正常工作。也许是因为其他 div 使用了 vh?
  • 如果你将100%高度赋予一个div,我相信它的所有父母也应该有100%高度

标签: html css


【解决方案1】:

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

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

看看下面的sn-p:

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%;
}
&lt;div id="main-content"&gt;&lt;/div&gt;

希望这会有所帮助!

【讨论】:

  • 是的,它可以正常工作,但无法在我的实际页面上正常工作。 RIP 给我
  • @claudios 也许你遗漏了什么,如果你能告诉我你的实际代码,也许我可以帮助你。
  • main-content 内是高度等于100vh 的div。会影响吗?
  • @claudios 没有看到实际情况就无法猜测,如果可能的话,你能在网上的任何地方给我看代码吗?
  • 我会尝试重新创建 sn-p。
【解决方案2】:

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

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

插入图片

【讨论】:

  • 是的,但对我来说不起作用。请看新的sn-p
【解决方案3】:

您的意思如下,如果是,则设置#main-content height:auto,它工作正常,如果您设置height:100%,则将其视为100vh,滚动隐藏放置在顶部的图像,所以尝试如下。

所以现在#main-contentheight:auto 计算并获取分配给其中存在的子元素的高度,并且分配给此 div #main-content 的背景图像只能看到直到 100vh 的高度,之后默认图像分配给身体变得可见。

body, html {
    margin: 0;
    background-image: url('http://lorempixel.com/output/nature-q-c-1176-907-10.jpg');
    height: 100%; 
    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');
    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;
    width:100%;
    height:auto;
}
    
.content {
    height: 100vh;
    background:rgba(220,60,60,0.6);
    color:#fff;
}
<div id="main-content">
  <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>
</div>

【讨论】:

  • 它不工作。 main-content 不应被背景不透明度覆盖。我试过了,但没有快乐。
  • @claudios 它被覆盖了更改背景颜色并将颜色分配给您会发现不同的字体,因为占位符图像和分配的背景颜色具有相同的阴影,因此难以识别。
  • 我的目标是让main-content 成为最重要的。我认为字体不是这里的问题。
  • @claudios 检查我更新的解决方案,正文图像位于底部,然后 #main-content 图像位于其顶部,但现在 #main-content 中存在的内容显然将位于 #main- 顶部内容,因为它们是存在于 #main 中的子元素......
  • 不透明的背景应该在主要内容的后面
猜你喜欢
  • 2014-10-03
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多