【发布时间】:2019-07-30 14:21:45
【问题描述】:
我有一个高度未知的表格单元格,其中包含img。这个img 有一个固定的像素宽度,但我需要它拉伸以填充(但不超过)表格单元格的整个高度。无论高度如何,宽度都应保持不变。
通常,我会使用height: 100% 来执行此操作,但这在这种情况下似乎不起作用:
img {
display: block;
width: 25px;
height: 100%;
}
<table>
<tr>
<td>
Here is some content.<br/>
I would like the adjacent image to stretch to be<br/>
the same height as it, but not have the image get<br/>
any wider.
</td>
<td>
<img src="https://placehold.it/20x20" />
</td>
</tr>
</table>
如何让图片填满其单元格的高度?
【问题讨论】:
-
如果您将高度作为属性添加到图像(不是 CSS)上,它可能会满足您的需求。
<img src="https://placehold.it/20x20" height="100%" width="25" />但是下面的CSS背景答案是更灵活的解决方案。
标签: html css html-table html-email