Flexbox:水平和垂直居中。
Flexbox:水平和垂直居中。
如何使用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; }
1 2 3 4
http://codepen.io/anon/pen/zLxBo
如何在Flexbox中垂直和水平居中元素
以下是两个一般的居中解决方案。
一种用于垂直对齐的伸缩项(flex-direction: column
),另一种用于水平对齐的伸缩项(flex-direction: row
)。
在这两种情况下,居中的
以下是两者的HTML代码:
DIV #1
DIV #2
CSS样式(不包括装饰样式)
当伸缩项垂直堆叠:
#container { display: flex; /* establish flex container */ flex-direction: column; /* make main axis vertical */ justify-content: center; /* center items vertically, in this case */ align-items: center; /* center items horizontally, in this case */ height: 300px; } .box { width: 300px; margin: 5px; text-align: center; /* will center text in, which is not a flex item */ }
当伸缩项水平堆叠时:
请从上面的代码中调整flex-direction
规则。
#container { display: flex; flex-direction: row; /* make main axis horizontal (default setting) */ justify-content: center; /* center items horizontally, in this case */ align-items: center; /* center items vertically, in this case */ height: 300px; }
将伸缩项的内容居中
Flex格式化上下文的范围仅限于父子关系。伸缩容器的后代超出子项将不参与伸缩布局并将忽略伸缩属性。实际上,伸缩属性在子项之外不可继承。
因此,为了在伸缩项中垂直和/或水平居中文本或其他内容,请将该项设为(嵌套)伸缩容器,并重复居中规则。
.box { display: flex; justify-content: center; align-items: center; /* for single line flex container */ align-content: center; /* for multi-line flex container */ }
在此处了解更多详细信息:如何在flexbox中垂直对齐文本?
或者,您还可以将margin:auto
应用于伸缩项的内容元素。
p { margin: auto; }
关于伸缩auto
边距的详细信息在此处对齐伸缩项的方法(请参见box#56)。
居中多行伸缩项
当伸缩容器具有多行(由于换行)时,需要使用align-content
属性进行跨轴对齐。
从规范中可以看出:
8.4. Packing Flex Lines: the
align-content
property当伸缩容器的交叉轴上有额外的空间时,
align-content
属性将在其中对齐伸缩容器的行,类似于justify-content
对齐主轴上的单个项目。注意,此属性对于单行伸缩容器没有影响。更多细节请参见:flex-wrap 如何与 align-self、align-items 和 align-content 一起使用?
浏览器支持
Flexbox 获得了所有主要浏览器的支持,除了 IE < 10。一些最近的浏览器版本,如 Safari 8 和 IE10,需要供应商前缀。如果需要快速添加前缀,可以使用Autoprefixer。更多详细信息请参见此答案。
旧浏览器的居中解决方案
使用 CSS 表格和定位属性的备选居中解决方案,请参见这个答案:https://stackoverflow.com/a/31977476/3597276
我认为您想要类似以下内容的东西。
html, body { height: 100%; } body { margin: 0; } .flex-container { height: 100%; padding: 0; margin: 0; display: flex; align-items: center; justify-content: center; } .row { width: auto; border: 1px solid blue; } .flex-item { background-color: tomato; padding: 5px; width: 20px; height: 20px; margin: 10px; line-height: 20px; color: white; font-weight: bold; font-size: 2em; text-align: center; }
1 2 3 4
请查看演示:http://jsfiddle.net/audetwebdesign/tFscL/
如果您想要 .flex-item
元素的高度和上/下内边距正常工作,它们应该是块级元素(使用 div
而不是 span
)。
此外,在 .row
上,将宽度设置为 auto
,而非 100%
。
您的 .flex-container
属性都没有问题。
如果您希望 .row
在视口中垂直居中,将 100% 的高度分配给 html
和 body
,并将 body
的边距清零。
请注意,.flex-container
需要一个高度才能看到垂直对齐效果,否则,容器会计算出包含内容所需的最小高度,这在本例中小于视口高度。
注:
flex-flow
、flex-direction
、flex-wrap
属性可能会使此设计更容易实现。我认为除非您想在元素周围添加一些样式(背景图像、边框等),否则不需要 .row
容器。