首页 > 解决方案 > 如何将 div 中的文本从 div 居中

问题描述

我有这种情况:

<div id="main">
    <div id="my-items">
        <div class=m id="id1">
            Smth here(1)
        </div>

        <div class=m id="id2">
            Smth here(2)
        </div>

        <div class=m id="id3">
            Smth here(2)
        </div>
        ...
    </div>
</div>

我有未确定的 div 数量(div 的子项(div 的子项))
我想垂直对齐文本(在他的 div 中(第一个文本 - #id1,第二个文本 - #id2 ...))!我尝试使用vertical-align: middle;. 请创建一个 jsfiddle 或 codepen 示例!

CSS:

.m{
height:32px;
width:90px;
text-align: center;
}
#my-items{
max-height:400px;
width:90px;
text-align:center;
}

我创建了一个非常简单的 3d 示意图

标签: htmlvertical-alignment

解决方案


以你的#my-items 风格试试这个

position: absolute;
top: 50%;

推荐阅读