【发布时间】:2016-11-10 19:08:27
【问题描述】:
我有一个这样的横幅部分:
.bannerImage {
width: 100%;
height: auto;
}
<img src="http://www.w3schools.com/css/trolltunga.jpg" alt="banner image" class="bannerImage" />
所以我没有使用背景图片,而是使用带有width 100% 和height auto 的图片。
现在我想在横幅部分添加横幅标题。因此,由于仅使用图像无法达到效果,因此我现在使用background-image 属性。但是对于背景图像,我想要与图像相同的布局。背景应该总是宽度为 100%,高度应该是自动的。下面是我的方法。如您所见,该部分会根据内容的高度进行调整。是否可以将它的高度设置为图像的高度。
.bg_banner_image {
background-size: 100% auto;
background-position: center top;
background-repeat: no-repeat;
}
<div class="bg_banner_image" style="background-image: url('http://www.w3schools.com/css/trolltunga.jpg')">some content here</div>
【问题讨论】:
-
容器是空的,所以为什么容器从背景图像中获取高度,如果你需要容器与背景图像高度相同,那么你必须给它们高度,否则将一些内容放在容器中
-
我希望容器采用背景图像的高度而不是其中的内容
标签: css