我试图垂直居中我的标签内容,但当我添加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>


当前回答

.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>

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

其他回答

将一个盒子垂直和水平居中的最好方法是使用两个容器:

外面的容器:

应有显示:表;

内容器:

应该有display: table-cell; 应该有垂直对齐:中间; 应该有text-align: center;

内容框:

应该有显示:inline-block; 应该调整水平文本对齐,除非你想让文本居中

演示:

body { margin : 0; } .outer-container { display: table; width: 80%; height: 120px; background: #ccc; } .inner-container { display: table-cell; vertical-align: middle; text-align: center; } .centered-content { display: inline-block; text-align: left; background: #fff; padding : 20px; border : 1px solid #000; } <div class="outer-container"> <div class="inner-container"> <div class="centered-content"> Center this! </div> </div> </div>

看看这小提琴!

居中的:在书页中间居中的:

要将内容置于页面中央,请将以下内容添加到外部容器中:

位置:绝对; 宽度:100%; 高度:100%;

下面是一个演示:

body { margin : 0; } .outer-container { position : absolute; display: table; width: 100%; height: 100%; background: #ccc; } .inner-container { display: table-cell; vertical-align: middle; text-align: center; } .centered-content { display: inline-block; text-align: left; background: #fff; padding : 20px; border : 1px solid #000; } <div class="outer-container"> <div class="inner-container"> <div class="centered-content"> Center this! </div> </div> </div>

看看这小提琴!

在这种情况下,我试图在button::before和button::after中垂直对齐文本内容,我能够使用vertical-align: text-top让它工作。

button::after {
  vertical-align: text-top;
}

网格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 >

我使用这个CSS代码:

display: grid;
justify-content: center;
align-items: center;

来源是CSS-Tricks

下面是如何使用两个简单的flexbox属性在两个轴上居中n个div:

设置容器的高度:在这里,主体被设置为至少100个视口高度。 对齐项目:中心;如果弯曲方向是行,将垂直中心块,否则水平,如果弯曲方向是列 justify-content:空间;将分配自由空间垂直如果弯曲方向是行或水平如果弯曲方向是列围绕div元素

身体{ 最小高度:100 vh; 显示:flex; 对齐项目:中心; justify-content:空间; } < div > foo < / div > 酒吧< div > < / div >