关于前端弹性布局的问题

就是在前端中,比如我有一个div盒子,里面包含其他的div和ul,这个div的class叫做info,我在样式里给这个div写上 display: flex;,请问它实现的范围是这个div盒子里变成弹性布局吗?如果我这个div盒子自己也想居中,那么是不是也得把这个div外面包围着它的,也变成弹性布局呢?一直理解不了这块,多谢了

你说得是对的,动手验证一下即可,刚好今天写了一个示例,content 既是容器也是项目

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    html,body {
      padding: 0;
      margin: 0;
      height: 100%;
    }
    .root {
      display: flex;
      height: 100%;
      background-color: red;
    }
    .nav {
      width: 120px;
      background-color: blue;
    }
    .content {
      flex: 1;
      background-color: green;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .box {
      width: 120px;
      height: 120px;
      background-color: yellow;
    }
  </style>
</head>
<body>
<div class="root">
  <div class="nav"></div>
  <div class="content">
    <div class="box">我是一个盒子</div>
  </div>
</div>
</body>
</html>


给父元素开起flex,子元素实现弹性布局。如果你父元素想实现居中,在往上层找参考点即可。

写了felx的作用于他的子元素,不过你想让这个写了flex的div居中,直接给他定个宽度然后加上margin:0 auto就行

flex布局是相对子集而言的,父集要是也想居中,也可以用flex布局,或其他方式