【发布时间】:2015-07-23 06:58:23
【问题描述】:
我有几个 div 和这样的图像
<div class="show-image">
<div class="image-info-box">
<div class="info-content">test</div>
</div>
<div class="image-container">
<img class="img-box" src="image.png" />
</div>
</div>
css文件是:
.show-image {
display: inline-block;
vertical-align: top;
}
.img-box {
padding-top: 30px;
width: 75%;
text-align: center;
display: block;
}
.image-container {
display: inline-block;
white-space: nowrap;
width: auto;
}
.image-info-box {
width: 100%;
padding-top: 30px;
text-align: center;
}
.info-content {
float: center;
}
我希望图像具有 75% 的宽度,因此我将 img-box 类的宽度设置为 75%(例如图像宽度为 1008px,因此 img-box 的宽度为 756px)。问题是为什么具有类 image-container 的 div 仍然保持原始图像宽度,即示例中的 1008px。图像容器如何适应 img-box 的宽度?
【问题讨论】:
-
div默认是块元素,默认情况下,width: 100%;是所有块元素。基本 CSS 行为。 -
那么如何设置父 div 适合子图像宽度?