<a>
标签内的<img>
标签一直存在。
为什么它们的边框不能对齐?
假设页面上只有这一个元素:
<a href='#'><img src='images/x.png' border='0'></a>
当我在Firefox调试器中查看时,它们的宽度都相同(即图像的宽度)。
但是,图像位于y = 0,h = img的高度处。
然而,a标签的y值在0以下(也许在图像的中间位置下方?),高度使其低于图像几个像素。
为什么会这样?这样会导致后面的布局混乱并使一切变得困惑。
<a>
标签内的<img>
标签一直存在。
为什么它们的边框不能对齐?
假设页面上只有这一个元素:
<a href='#'><img src='images/x.png' border='0'></a>
<a>
元素的文本基线对齐。在基线之下的额外空间是为了下降字母(例如字母p和q中延伸出来的部分)而预留的。a {border: 1px solid #00F}
img#two {vertical-align: bottom}
img#three {vertical-align: text-bottom}
Image at baseline:
<a href='#'><img src='http://placehold.it/50x50'></a>
Image at bottom:
<a href='#'><img id="two" src='http://placehold.it/50x50'></a>
Image at text-bottom:
<a href='#'><img id="three" src='http://placehold.it/50x50'></a>
规则并不同义:text-bottom
将元素定位于“父元素字体底部”,而bottom
将其定位于“元素底部”。根据这里的问题,“bottom”可能是技术上正确的选择,但正如下面的评论所指出的那样,对于至少一些用户/浏览器,它可能会重叠边框。
<a>
和<img>
的默认呈现方式有关。锚链接(默认情况下)是inline
元素,而图像是inline-block
元素。<a>
设置为inline-block
,并将子<img>
设置为block
。考虑以下示例,说明了当这些元素结合在一起时的“默认”呈现方式,以及当您更改它们的默认CSS时的呈现方式。
a {
border: 1px solid black;
}
img {
border: 1px solid red;
}
.correct-display {
display: inline-block;
margin-top: 20px; /* Push the 2nd example down so the <a> border is visible on the 1st example */
}
.correct-display img {
display: block;
}
<a href="#null">
<img src="http://placehold.it/300x150" />
</a>
<a href="#null" class="correct-display">
<img src="http://placehold.it/300x150" />
</a>
float
或更改元素以支持flex
,或折叠列表元素,使项目水平显示而不是以符号/数字格式显示。 - Robert