javascript - 如何让高度不确定的图片相对于固定容器垂直对齐

标签 javascript css

如果 block 是文本,那会很容易。但是 block 是图像en……

最佳答案

不使用表格就没有完美的方法,因为不幸的是 vertical-align CSS 属性对内联元素和表格元素做了不同的事情(参见 http://phrogz.net/css/vertical-align/index.html)。

可以在此处找到几个解决方案,以及讨论的具体优缺点:http://blog.themeforest.net/tutorials/vertical-centering-with-css/

其中,比较好的解决方案之一是介绍的第三种方法:

insert a div above the content element. This will be set to height:50%; and margin-bottom:-contentheight;. The content will then clear the float and end up in the middle.

<div id="floater">
<div id="content">
    <img src="my-image.png"/>
</div>
</div>

#floater    {float:left; height:50%; margin-bottom:-120px;}
#content    {clear:both; height:240px; position:relative;}

关于javascript - 如何让高度不确定的图片相对于固定容器垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3888826/

相关文章:

javascript - chrome 为对象迭代器显示了什么

java - GWT 日志记录仅显示级别 SERVE

asp.net - 样式嵌套在另一种样式的 asp.net View 中,但不确定如何嵌套

javascript - 可以通过脚本获取图像的 CSS 定义大小并在图像加载之前更改 URL 值吗?

javascript - 来自外部服务器的 phonegap 更新文件

javascript - 无法更新组件 (`App` ) 组件中出现错误

javascript - 使用 CSS 填充选中的复选框

css - 在 div 中为 <h1> 添加下划线

jquery - 内容高于页脚

html - 如何在一行中显示从数据库值生成的有序列表?