html - CSS 垂直对齐文本在网站上不起作用?

标签 html css css-float

我想将一行或多行的文本在 div 中垂直居中。但由于某种原因,这行不通:

#image {
    height: 96px;
    width: 100%;
    float: left;
    line-height: 96px;
}
    #image h5 {
        display: inline-block;
        vertical-align: middle;
        line-height: 13px;
    }

我做错了什么?

最佳答案

由于您希望它适用于不同数量的线路,因此您需要一个动态解决方案。试试 table/table-cell 方法:

#banner_middle {
    height: 96px;
    width: 100%;
    float: left;
    display: table;
}
#banner_middle h5 {
    display: table-cell;
    vertical-align: middle;
}

无需使用硬编码的 line-height 值。

关于html - CSS 垂直对齐文本在网站上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25676843/

相关文章:

php - 在 php 中将 html 属性与正则表达式匹配

css - 带有列表和 float 的奇怪包装

css - Bootstrap div 在移动设备上跳出父 div

html - 使用 Shopify 定位或 float 到右上角

html - Flexbox 不适用于 iPad 和 Safari

javascript - DLL 作为 `src` 的 `<script>`

html - 如何使图像的一部分可点击?

html - 旋转时如何将 span 或 div 对齐到中间?

javascript - 如何仅使用 javascript 显示此数组 onchange 的第二列中的内容

javascript - 从图像轮播中删除过渡