#container {
width: 250px;
margin: 0 auto;
}
.group {
border: 1px solid grey;
margin-bottom: 20px;
}
.group p {
text-align: center;
font-family: Helvetica sans-serif;
font-size: 25px;
color: #2e3d49;
}
.group img{
width: 100%;
}
<div id="container">
<div class="group">
<p>Hello World</p>
<img src="https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg">
</div>
</div>
请点击“运行代码段”按钮查看结果。您可以在图像的底部看到,有一个空白部分。这是从哪里来的?
答案 0 :(得分:2)
img
是内联元素,因此设置为display:block
或默认为vertical-align:baseline
,您可以设置vertical-align:bottom
#container {
width: 250px;
margin: 0 auto;
}
.group {
border: 1px solid grey;
margin-bottom: 20px;
}
.group p {
text-align: center;
font-family: Helvetica sans-serif;
font-size: 25px;
color: #2e3d49;
}
.group img {
width: 100%;
display: block;
/* vertical-align: bottom - would work as well */
}
<div id="container">
<div class="group">
<p>Hello World</p>
<img src="https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg">
</div>
</div>