在固定高度块中垂直对齐图像

发布于 2024-08-05 18:43:35 字数 254 浏览 1 评论 0原文

我正在尝试将图像底部对齐在固定高度块中:

div { float: left; width: 100px; height: 100px; line-height: 100px; }
div img { vertical-align: middle; }

...适用于现代浏览器,IE 除外!毫无疑问,IE 很糟糕,但如果可能的话,我真的需要修复。

编辑添加:无法使用表格或背景图像。

非常感谢

I'm trying to BOTTOM align an image in a fixed-height block:

div { float: left; width: 100px; height: 100px; line-height: 100px; }
div img { vertical-align: middle; }

...works in modern browsers, except IE! IE sucks no wonder, but I really need a fix, if possible.

Edited to add: Can't use tables or background image.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

远山浅 2024-08-12 18:43:35

为什么你不能只定位:相对分割,定位:绝对并混乱底部/左侧属性?

或者将其位置切换为内联块并进行操作。

Why can't you just position:relative the division, position:absolute and mess with bottom/left properties?

Or toggle it's position to inline-block and play around.

故人的歌 2024-08-12 18:43:35

尽管我说起来很痛苦,并且冒着被纯粹主义者分成两半的风险,但以通用兼容的方式垂直对齐某些东西的最可靠方法是使用桌子。

table {
    float: left; 
    width: 100px; 
    height: 100px; 
    line-height: 100px;
}

<table cellspacing="0" cellpadding="0">
    <tr>
        <td align="center" valign="bottom"><img src="foo.jpg" /></td>
    </tr>
</table>

As much as it pains me to say it and at the risk of being drawn and quartered by the purists out there, the most reliable way to vertically align something in a universally compatible way is to use a table.

table {
    float: left; 
    width: 100px; 
    height: 100px; 
    line-height: 100px;
}

<table cellspacing="0" cellpadding="0">
    <tr>
        <td align="center" valign="bottom"><img src="foo.jpg" /></td>
    </tr>
</table>
み格子的夏天 2024-08-12 18:43:35

最简单的方法是在 DIV style="background:url(...) no-repeat center Bottom" 中使用而不是 IMG 标签。

Simplest way to do this is to use in the DIV style="background:url(...) no-repeat center bottom" instead of IMG tag.

简单 2024-08-12 18:43:35

我现在找不到它,但我看到有一些东西将元素定位在 50%(顶部),然后给它一个负的顶部边距 -50%。

显然只是为了垂直对齐......

I can´t find it right now, but I saw something positioning the element at 50% (the top) and then giving it a negative top-margin of -50%.

Just for the vertical alignment obviously...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文