html - Div 垂直对齐图像

标签 html css

我有一个带有图像“image.jpg”的 div。

通过使用 text-align=center,图像水平居中。

但是它不是垂直对齐的。我将如何实现垂直对齐?

<div style="vertical-align: middle;text-align:center;">
    <img title="Title3" src="image.jpg" style="display: inline;" id="idImage">
</div>

最佳答案

使用 vertical-align this article 中描述的属性.注意为了得到vertical-align工作你需要做display: table-cell

div.centered {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    /* other styling */
}

我给你的 <div> centered 的类名.

另外你不应该使用 text-align用于居中,而是添加 margin: 0 auto;到图像样式。

编辑

HTML:

<div class="centered">
    <img src="..." class="centered-image" />
</div>

CSS:

div.centered {
    display: table-cell;
    vertical-align: middle;
}

img.centered-image {
    margin: 0 auto;
}

关于html - Div 垂直对齐图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12079421/

相关文章:

html - 高度 : Fill whole viewport but scroll if neccessary

javascript - HTML5 和其他格式的动态视频播放列表

html - 如何删除textarea和父div之间的空间

html - 位于表格外部的 CSS 单元格以及如何处理它们的宽度和边框

jquery - 在另一个 div 之上展开一个 div(悬停时)

javascript - 如何在不改变图案的情况下改变 HTML5 Canvas 中图像的颜色

html - 谷歌地图 JavaScript 错误

html - 行数影响方框位置

javascript - 将 jspdf 中的文档元素存储在 HTML5 webstorage 中

javascript - 固定位置div底部的定位元素