我试图垂直居中我的标签内容,但当我添加CSS样式display:inline-flex时,水平文本对齐消失了。

我怎么能使文本对齐x和y为我的每个制表?

* { box-sizing: border-box; } #leftFrame { background-color: green; position: absolute; left: 0; right: 60%; top: 0; bottom: 0; } #leftFrame #tabs { background-color: red; position: absolute; top: 0; left: 0; right: 0; height: 25%; } #leftFrame #tabs div { border: 2px solid black; position: static; float: left; width: 50%; height: 100%; text-align: center; display: inline-flex; align-items: center; } <div id=leftFrame> <div id=tabs> <div>first</div> <div>second</div> </div> </div>


当前回答

要在页面中居中Div,请检查fiddle链接

# vh { border - radius: 15 px; Box-shadow: 0 0 8px rgba(0,0,0,0.4); 填充:25 px; 宽度:200 px; 身高:200 px; 背景:白色; text-align:中心; 保证金:汽车; 位置:绝对的; 上图:0; 左:0; 底部:0; 右:0; } <div id="vh"> div要垂直对齐</div>

更新 另一种选择是使用flex box检查小提琴链接

.vh { background - color: # ddd; 身高:200 px; 对齐项目:中心; 显示:flex; } .vh > div { 宽度:100%; text-align:中心; vertical-align:中间; } < div class = " vh”> <div> div要垂直对齐 < / div >

其他回答

需要遵循以下新的和简单的解决方案: .centered-class { 对齐项目:中心; 显示:flex; justify-content:中心; 宽度:100大众; 身高:100 vh; } < div class = " centered-class”> 我在垂直和水平的中心。 < / div >

为了使元素垂直和水平居中,我们还可以使用下面提到的属性。

这个CSS属性垂直对齐项目,并接受以下值:

flex-start:项目对齐到容器的顶部。

flex-end:物品与容器底部对齐。

center:物品在容器的垂直中心对齐。

baseline:项目显示在容器的基线。

拉伸:物品被拉伸以适应容器。

这个CSS属性justify-content,用于水平对齐项目,并接受以下值:

flex-start:项目对齐到容器的左侧。

flex-end:物品对齐到容器的右侧。

center:项目在容器的中心对齐。

space-between:项目之间以相等的间距显示。

space-around:物品以等距显示。

另一种方法是使用表:

<div style="border:2px solid #8AC007;身高:200 px;宽度:200 px;" > <表风格= "宽度:100%;高度:100%”> < tr风格= "高度:100%”> < td风格= "高度:100%;Text-align:center">你好,这里有多行,这太长了,太棒了,伙计</td> < / tr > 表> < / < / div >

.align { display: flex; width: 400px; height: 400px; border: solid 1px black; align-items: center; justify-content: space-around; } .align div:first-child { width: 20px; height: 20px; background-color: red; position: absolute; } .align div { width: 20px; height: 20px; background-color: blue; } <div class='align'> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div>

第一个子将在中心垂直和水平对齐

网格css方法

#包装{ 位置:绝对的; 上图:0; 底部:0; 右:0; 左:0; 显示:网格; Grid-template-columns: repeat(3,1fr); Grid-template-rows: repeat(3,1fr); } .main { background - color: # 444; } < div id = "包装" > < div class = "盒子" > < / div > < div class = "盒子" > < / div > < div class = "盒子" > < / div > < div class = "盒子" > < / div > <div class="box main"></div> < / div >