如何在容器中使用flexbox水平和垂直地居中div。在下面的例子中,我想要每个数字都在对方下面(在行中),这是水平居中。

.flex-container { padding: 0; margin: 0; list-style: none; display: flex; align-items: center; justify-content: center; } row { width: 100%; } .flex-item { background: tomato; padding: 5px; width: 200px; height: 150px; margin: 10px; line-height: 150px; color: white; font-weight: bold; font-size: 3em; text-align: center; } <div class="flex-container"> <div class="row"> <span class="flex-item">1</span> </div> <div class="row"> <span class="flex-item">2</span> </div> <div class="row"> <span class="flex-item">3</span> </div> <div class="row"> <span class="flex-item">4</span> </div> </div>

http://codepen.io/anon/pen/zLxBo


当前回答

您可以在flex-container中添加flex-direction:column

.flex-container {
  flex-direction: column;
}

将display:inline-block添加到flex-item

.flex-item {
 display: inline-block;
}

因为您添加的宽度和高度对该元素没有影响,因为它具有内联显示。尝试添加display:inline-block或display:block。了解更多关于宽度和高度的知识。

也添加到row类(你被给予row{}而不是作为样式)

.row{
  width:100%;
  margin:0 auto;
  text-align:center;
}

工作演示:

.flex-container { padding: 0; margin: 0; list-style: none; display: flex; align-items: center; justify-content:center; flex-direction:column; } .row{ width:100%; margin:0 auto; text-align:center; } .flex-item { background: tomato; padding: 5px; width: 200px; height: 150px; margin: 10px; line-height: 150px; color: white; font-weight: bold; font-size: 3em; text-align: center; display: inline-block; } <div class="flex-container"> <div class="row"> <span class="flex-item">1</span> </div> <div class="row"> <span class="flex-item">2</span> </div> <div class="row"> <span class="flex-item">3</span> </div> <div class="row"> <span class="flex-item">4</span> </div> </div>

工作演示栏:

.flex-container { padding: 0; margin: 0; width: 100%; list-style: none; display: flex; align-items: center; } .row { width: 100%; } .flex-item { background: tomato; padding: 5px; width: 200px; height: 150px; margin: 10px; line-height: 150px; color: white; font-weight: bold; font-size: 3em; text-align: center; display: inline-block; } <div class="flex-container"> <div class="row"> <span class="flex-item">1</span> </div> <div class="row"> <span class="flex-item">2</span> </div> <div class="row"> <span class="flex-item">3</span> </div> <div class="row"> <span class="flex-item">4</span> </div> </div>

其他回答

您可以在flex-container中添加flex-direction:column

.flex-container {
  flex-direction: column;
}

将display:inline-block添加到flex-item

.flex-item {
 display: inline-block;
}

因为您添加的宽度和高度对该元素没有影响,因为它具有内联显示。尝试添加display:inline-block或display:block。了解更多关于宽度和高度的知识。

也添加到row类(你被给予row{}而不是作为样式)

.row{
  width:100%;
  margin:0 auto;
  text-align:center;
}

工作演示:

.flex-container { padding: 0; margin: 0; list-style: none; display: flex; align-items: center; justify-content:center; flex-direction:column; } .row{ width:100%; margin:0 auto; text-align:center; } .flex-item { background: tomato; padding: 5px; width: 200px; height: 150px; margin: 10px; line-height: 150px; color: white; font-weight: bold; font-size: 3em; text-align: center; display: inline-block; } <div class="flex-container"> <div class="row"> <span class="flex-item">1</span> </div> <div class="row"> <span class="flex-item">2</span> </div> <div class="row"> <span class="flex-item">3</span> </div> <div class="row"> <span class="flex-item">4</span> </div> </div>

工作演示栏:

.flex-container { padding: 0; margin: 0; width: 100%; list-style: none; display: flex; align-items: center; } .row { width: 100%; } .flex-item { background: tomato; padding: 5px; width: 200px; height: 150px; margin: 10px; line-height: 150px; color: white; font-weight: bold; font-size: 3em; text-align: center; display: inline-block; } <div class="flex-container"> <div class="row"> <span class="flex-item">1</span> </div> <div class="row"> <span class="flex-item">2</span> </div> <div class="row"> <span class="flex-item">3</span> </div> <div class="row"> <span class="flex-item">4</span> </div> </div>

还:flex;对于它的容器和边缘:auto;因为它的项目工作完美。

注意:你必须设置宽度和高度才能看到效果。

#{容器 宽度:100%;/*宽度需要设置*/ 身高:150 px;/*高度需要设置*/ 显示:flex; } .item { 保证金:汽车;/*这些将使项目在中心*/ background - color: # CCC; } < div id = "容器" > 中心< div class = "项目" > < / div > < / div >

使用CSS +

<div class="EXTENDER">
  <div class="PADDER-CENTER">
    <div contentEditable="true">Edit this text...</div>
  </div>
</div>

看这里

不要忘记使用重要的浏览器特定属性:

对齐项目:中心;-->

-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;

justify-content:中心;-->

-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;

你可以阅读这两个链接来更好地理解flex: http://css-tricks.com/almanac/properties/j/justify-content/和 http://ptb2.me/flexbox/

祝你好运。

你可以利用

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

在父组件上