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


当前回答

网格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属性垂直对齐项目,并接受以下值:

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

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

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

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

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

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

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

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

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

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

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

对我来说,最简单和最干净的解决方案是使用CSS3属性“transform”:

.container { 位置:相对; } .container a { 位置:绝对的; 上图:50%; 变换:翻译(0 -50%); } < div class = "容器" > <a href="#">Hello world!< / > < / div >

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

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

我使用这个CSS代码:

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

来源是CSS-Tricks

你可以使用CSS(你的元素显示:inline-grid + grid-auto-flow: row;)网格和Flex Box(父元素显示:Flex;),

参见下面的片段

# leftFrame { 显示:flex; 身高:100 vh; 宽度:100%; } 标签# { 显示:inline-grid; grid-auto-flow:行; grid-gap: 24 px; justify-items:中心; 保证金:汽车; } html,身体{ 保证金:0; 填充:0; } < div > < div id = leftFrame > < div id = >标签 首先< div > < / div > < div >第二< / div > < / div > < / div > < / div >