欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

flex布局实现筛子点数以及对于flex布局的思考

程序员文章站 2022-04-30 12:09:15
...

之前在一次面试中被问道flex布局较普遍CSS布局的优点在什么地方,就个人经历而言,flex布局比普通CSS布局简便很多,比如我们要进行一个简单的一行四个li的无序列表的排列,要求相邻li元素间隔自适应,首尾元素要与ul容器没有间隙,效果图如下:

flex布局实现筛子点数以及对于flex布局的思考

普通CSS布局为:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>1</title>
  <style>
    #box {
      margin: 0px;
      padding:  0px;
      width: 300px;
      height: 300px;
      border: 2px solid slategrey;
      list-style-type: none;
    }
    .item {
      width: 60px;
      height: 60px;
      background: salmon;
      float: left;
      margin-right: 20px;
    }
  </style>
</head>
<body>
  <div>
    <ul id="box">
      <li class="item"></li>
      <li class="item"></li>
      <li class="item"></li>
      <li class="item" style="margin-right: 0px;"></li>
    </ul>
  </div>
</body>
</html>

首先将li元素设置float:left,在已知ul宽度和li宽度的情况下要计算处相邻li之间的间隔宽度,本例中则是300px-4*60px=60px,因为四个元素间有三个间隔故60px*3=20px,这才计算出相邻li的间隔宽度,并为li元素设置margin-right:20px。又不能让最后一个元素因为超出父元素宽度而换行,则需在标签上直接设置style="margin-right:0px;"覆盖首部样式表里的设置,整个过程相当繁琐。

若使用flex布局,实现这一布局就显得轻松多了:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>1</title>
  <style>
    #box {
      margin: 0px;
      padding:  0px;
      width: 300px;
      height: 300px;
      border: 2px solid slategrey;
      list-style-type: none;
      display: flex;
      justify-content: space-between;
    }
    .item {
      width: 60px;
      height: 60px;
      background: salmon;
    }
  </style>
</head>
<body>
  <div>
    <ul id="box">
      <li class="item"></li>
      <li class="item"></li>
      <li class="item"></li>
      <li class="item"></li>
    </ul>
  </div>
</body>
</html>

flex布局中简单设置两个属性就可以达到我们想要的效果,即简便也不用计算间隔宽度


flex布局实现筛子里的一、二、三、四点


结果:

flex布局实现筛子点数以及对于flex布局的思考

代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>1</title>
  <style>
    #box {
      display: flex;
      width: 300px;
      height: 300px;
      border: 2px solid slategrey;
      justify-content: center;
      align-items: center;
      
    }
    .item {
      width: 60px;
      height: 60px;
      background: salmon;
    }
  </style>
</head>
<body>
  <div id="box">
    <div class="item"></div>
  </div>
</body>
</html>


结果:

flex布局实现筛子点数以及对于flex布局的思考

代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>2</title>
  <style>
    #box {
      display: flex;
      width: 300px;
      height: 300px;
      border: 2px solid slategrey;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
    }
    .item {
      width: 60px;
      height: 60px;
      background: salmon;
    }
  </style>
</head>
<body>
  <div id="box">
    <div class="item"></div>
    <div class="item"></div>
  </div>
</body>
</html>


结果:

flex布局实现筛子点数以及对于flex布局的思考

代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>3</title>
  <style>
    #box {
      display: flex;
      width: 300px;
      height: 300px;
      border: 2px solid slategrey;
      flex-direction: column;
      justify-content: space-between;
    }
    .item {
      width: 60px;
      height: 60px;
      background: salmon;
    }
    .item:nth-child(2) {
      align-self: center;
    }
    .item:nth-child(3) {
      align-self: flex-end;
    }
  </style>
</head>
<body>
  <div id="box">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
</body>
</html>


结果:

flex布局实现筛子点数以及对于flex布局的思考

代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>4</title>
  <style>
    #box {
      display: flex;
      width: 300px;
      height: 300px;
      border: 2px solid slategrey;
      flex-wrap: wrap;
      align-content: space-around;
    }
    .item-box {
      display: flex;
      flex-basis: 100%;  /*关键的设置*/
      justify-content: space-around;
    }
    .item {
      width: 60px;
      height: 60px;
      background: salmon;
    }
  </style>
</head>
<body>
  <div id="box">
    <div class="item-box">
      <div class="item"></div>
      <div class="item"></div>
    </div>
    <div class="item-box">
        <div class="item"></div>
        <div class="item"></div>
      </div>
  </div>
</body>
</html>