div内的div包括背景图像

时间:2015-11-03 03:35:58

标签: html css image css-position

我有两个div。第一个是父母',它为另一个div提供宽度和高度。第二个div具有背景附件:固定并且存在问题...当宽度:100%和高度:100%时,我的图像不会显示。当图像具有高度:800px时,一切都很好,但是当我改变屏幕分辨率时,第一个div不会与图像缩放,因此图像和其余内容之间存在空白空间。任何想法如何解决它?谢谢你的回复!

<div class="divimgparent">
<div class="divimg"></div>
</div>
.divimgparent {width: 100%; height: auto;}
.divimg {background: url("../images/1.jpg"); width: 100%; height: 800px; background-repeat: no-repeat; background-size: contain; background-attachment: fixed;}

2 个答案:

答案 0 :(得分:0)

试试这个。当你的css为height:auto;时,你的父div不会得到高度。因此,您需要为父div定义高度。

&#13;
&#13;
html, body {
  width: 100%;
  height: 100%;
}
.divimgparent {
    width: 100%;
    height: 100%;
}
.divimg {background: url("http://www.ecdevelopment.org/wp-content/uploads/2015/04/hippie-flower.jpg");width: 100%; height: 100%; background-repeat: no-repeat; background-size: contain; background-attachment: fixed;}
&#13;
<div class="divimgparent">
<div class="divimg"></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<div class="divimgparent">
    <div class="divimg"></div>
</div>

&#13;
&#13;
.divimgparent {width: 100%; height: 90vh;}
.divimg {background: url("http://s9.postimg.org/bxayc3y3z/Desert.jpg"); width: 100%; height:100%; background-repeat: no-repeat; background-size: contain; background-attachment: fixed;}
&#13;
&#13;
&#13;

请试试这个。

演示

相关问题