代码示例:(标识:css3_flexbox_perfect_center)
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  background-color: DodgerBlue;
}

.flex-container>div {
  background-color: #f1f1f1;
  color: white;
  width: 100px;
  height: 100px;
}
</style>
</head>
<body>
<h1>完美的居中</h1>

<p>如果容器的 justify-content 和 align-items 属性都设置为 <em>center</em>,则项目会在两个轴的方向同时居中。</p>

<div class="flex-container">
  <div></div>
</div>

</body>
</html>
运行结果: