【发布时间】:2014-05-09 14:49:05
【问题描述】:
对于我的响应式网站,我希望将英雄图像作为背景图像,而不是 html 中的图像。
我可以轻松地将 div 设为 100% 宽度,但我不确定如何使其具有正确的高度。我必须以 px 为单位设置高度,但所需的高度取决于 div 的宽度,该宽度会随着站点的响应而变化。
<div class="cont">
<p>Below is an image in the html that scales correctly</p>
<img src="http://www.hdwallpapers.in/walls/abstract_color_background_picture_8016-wide.jpg" />
<p>The example below is a background image in CSS</p>
<div class="hero-img"></div>
<p>Some text</p>
</div>
.cont {
width: 20%;
margin: auto
}
img {
max-width: 100%;
}
.hero-img {
background: url("http://www.hdwallpapers.in/walls/abstract_color_background_picture_8016-wide.jpg") no-repeat;
background-size: 100% 100%;
height: 200px;
}
http://codepen.io/anon/pen/FBxkz
更新 - 背景大小的封面或包含不能解决我的问题。带有图像的 div 仍然具有固定的高度。
【问题讨论】:
标签: css