移动端如何实现圣杯布局

195 阅读1分钟

双飞翼(圣杯布局)

1利用定位实现两侧固定中间自适应

  • 父盒子分支左右padding值

  • 给左右盒子的width设置父盒子的padding值然后分别定位到padding出

  • 中间盒子自适应

<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    .box {
      position: relative;
      width: 500px;
      height: 50px;
      background-color: aqua;
      margin: 100px auto;
    }

    .box1 {
      position: absolute;
      left: 0;
      top: 0;
      width: 50px;
      height: 50px;
      background-color: bisque;
    }

    .box3 {
      position: absolute;
      right: 0;
      top: 0;
      width: 50px;
      height: 50px;
      background-color: bisque;
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
  </div>
</body>

</html>

Snipaste_2022-04-25_20-26-41.png

2:使用弹性布局display:flex

<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .box {
      display: flex;
      width: 40%;
      height: 50px;
      background-color: aqua;
      margin: 100px auto;
    }

    .box1,
    .box3 {
      width: 50px;
      height: 50px;
      background-color: red;
    }

    .box2 {
      flex: 1;
      background-color: black;
      margin: 0 10px;
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
  </div>
</body>

</html>

Snipaste_2022-04-25_20-45-24.png

通过这两种方法都可以实现圣杯布局但是相比较display:flex弹性布局更简洁一点这点比较实用,不过因人而异看自己喜好去实现去完成就行啦

崇高的理想就象生长在高山上的鲜花。如果要搞下它,勤奋才能是攀登的绳索。