30秒学会 CSS 片段 · 2018年8月26日

30秒学会 CSS 片段 – Flexbox centering

Horizontally and vertically centers a child element within a parent element using flexbox.

  • display: flex creates a flexbox layout.
  • justify-content: center centers the child horizontally.
  • align-items: center centers the child vertically.

预览

Centered content.



HTML

<div class="flexbox-centering">
  <div>Centered content.</div>
</div>

CSS

.flexbox-centering {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
}

翻译自:https://www.30secondsofcode.org/css/s/flexbox-centering