css - 布局 flexbox : how to get the leaf nodes centered vertically and horizontally within the half-containers?

标签 css layout flexbox

我想有一个非常简单的布局,但我无法让它工作

<div class="page-wrapper">
    <div class="top-half">
        <div class="center-me-vertically-and-horizontally"></div>
    </div>
    <div class="bottom-half">
        <div class="center-me-vertically-and-horizontally"></div>
    </div>
</div>

page-wrapper 和 top-half/bottom-half 使用 flexbox(列)很容易实现,但是如何让叶节点在半容器内垂直和水平居中?

最佳答案

你可以给 flex boxes 加上标签:

html, body, .page-wrapper {
  height:100%;
  margin:0;
  }
.page-wrapper {
  display:flex;
  flex-direction:column;
  }
.top-half , .bottom-half {
  display:flex;
  flex:1;
    align-items:center;
  justify-content:center;
  box-shadow: 0 0 0 1px ; /* lets see them */
  }
<div class="page-wrapper">
    <div class="top-half">
        <div class="center-me-vertically-and-horizontally">top</div>
    </div>
    <div class="bottom-half">
        <div class="center-me-vertically-and-horizontally">bottom</div>
    </div>
</div>

关于css - 布局 flexbox : how to get the leaf nodes centered vertically and horizontally within the half-containers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766433/

相关文章:

html - 我的菜单栏子菜单不可见

jquery cookie 设置 css 显示 : none

android - CardView 布局重叠

email - Grails 电子邮件的 DRY 布局?

CSS 多列蛇形图案

css - 圆边,不是圆 Angular

ruby-on-rails - rails "undefined method"从布局 View 调用辅助方法时

html - 将 flexbox 元素与 flex-grow 元素对齐

css - chrome 69 更新后, flexbox 在布局中产生问题

javascript - 使用 Javascript 单击移动图像?