网页布局方式总结

1.水平居中方式汇总

1.1.文本/行内/行内块级居中

如果要使文本、行内元素、行内块级元素水平居中,直接使用 text-aligin:center即可

<style>
  .box {
    text-align: center;
  }
</style>

<div class="box">
  <span>hello</span>
</div>

注意:只对行内内容有效

如果是有多个块元素居中,可以这样做:

<style>
  .box {
    width: 500px;
    height: 400px;
    background-color: red;
    margin: 50px auto;
    text-align: center;
  }
  .box1 {
    width: 100px;
    height: 100px;
    background-color: green;
    margin-left: 10px;
    display: inline-block;
  }
</style>
<div class="box">
  <div class="box1"></div>
  <div class="box1"></div>
  <div class="box1"></div>
  <div class="box1"></div>
</div>

1.2.单个块级元素居中

块级元素可以使用margin:auto自动居中,原理是当margin左右有剩余值的时候,如果设置为auto,左右会自动平分剩余值

<style>
  .box {
    width: 400px;
    height: 400px;
    background-color: red;
    margin: 50px auto;
  }
</style>

<div class="box"></div>

注意:必须给盒子设置宽度才行

1.3.使用position实现水平居中

这种方式是利用定位的left值和margin值组合实现,先让left值为50%,然后在让margin-left值往回拉自身宽度的一半,这个时候内层盒子的中心和外层盒子的中心是重叠的,达到居中的效果

<style>
  .box {
    width: 500px;
    height: 400px;
    background-color: red;
    margin: 50px auto;
    position: relative;
  }
  .box1 {
    width: 100px;
    height: 100px;
    background-color: green;
    position: absolute;
    left: 50%;
    margin-left: -50px;
  }
</style>
<div class="box">
  <div class="box1"></div>
</div>

知道了上述原理,你还可以使用position + transform的方式来实现

<style>
  .box {
    width: 500px;
    height: 400px;
    background-color: red;
    margin: 50px auto;
    position: relative;
  }
  .box1 {
    width: 100px;
    height: 100px;
    background-color: green;
    position: absolute;
    left: 50%;
    /* margin-left: -50px; */
    /* 注意:使用translateX的时候,设置的50%是相对于自身宽度来计算的 */
    transform: translateX(-50%);
  }
</style>

总结:使用position+margin的这种方式兼容性比较好,不管是块元素还是行内元素都适合,缺点是脱离文档流,代码比较多,使用position+transform这种方式需要 ie9+支持,不过都2020了,没有必要考虑ie了

1.5.flex布局

这是最简单的一种方式,我们会有单独的专题来讲flex布局

<style>
.box2 {
  width: 500px;
  height: 300px;
  background-color: red;
  display: flex;
  justify-content: center;
}
</style>
<div class="box2">
  <span>hello</span>
</div>

2.垂直居中方式汇总

2.1.单行文本/行内/行内块级

让行高和外层盒子高度一致即可实现

<style>
  .box {
    height: 300px;
    background-color: green;
    line-height: 300px;
  }
</style>
<div class="box">
  <span>hello</span>
</div>

2.2.图片垂直居中

设置vertical-align为middle

<style>
  .box {
    height: 300px;
    background-color: green;
    line-height: 300px;
    font-size: 0;
  }
  .box img {
    vertical-align: middle;
  }
</style>
<div class="box"><img src="logo.png" alt="" /></div>

2.3.块元素垂直居中

方式1: position + margin

<style>
  .box1 {
    height: 300px;
    width: 100%;
    position: relative;
    background-color: green;
  }
  .box2 {
    height: 100px;
    position: absolute;
    top: 50%;
    width: 100%;
    background-color: red;
    margin-top: -50px;
  }
</style>
<div class="box1">
  <div class="box2"></div>
</div>

方式2: position + transform

<style>
  .box1 {
    height: 300px;
    width: 100%;
    position: relative;
    background-color: green;
  }
  .box2 {
    height: 100px;
    position: absolute;
    top: 50%;
    width: 100%;
    background-color: red;
    /* margin-top: -50px; */
    transform: translateY(-50%);
  }
</style>
<div class="box1">
  <div class="box2"></div>
</div>

方式3: position+margin的另一种写法

当position的top和bottom为0的时候,如果正好margin的top和bottom设置为auto,那么它们将无限延伸充满整个空间,并且平分,这个和水平设置为auto一样

<style>
  .box1 {
    height: 300px;
    width: 100%;
    position: relative;
    background-color: green;
  }
  .box2 {
    height: 100px;
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
    background-color: red;
    margin: auto 0;
  }
</style>
<div class="box1">
  <div class="box2"></div>
</div>

2.3.使用flex布局垂直居中

 <style>
  .box1 {
    height: 300px;
    width: 100%;
    display: flex;
    align-items: center;
    background-color: green;
  }
  .box2 {
    height: 100px;
    width: 100%;
    background-color: red;
  }
</style>
<div class="box1">
  <div class="box2"></div>
</div>

3.水平垂直居中方式汇总

3.1.定位+transform

<style>
  .box1 {
    height: 300px;
    width: 100%;
    position: relative;
    background-color: green;
  }
  .box2 {
    height: 100px;
    width: 100px;
    background-color: red;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
</style>
<div class="box1">
  <div class="box2"></div>
</div>

3.2.定位+margin

<style>
  .box1 {
    height: 300px;
    width: 100%;
    position: relative;
    background-color: green;
  }
  .box2 {
    height: 100px;
    width: 100px;
    background-color: red;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
  }
</style>
<div class="box1">
  <div class="box2"></div>
</div>

3.3.flex

<style>
  .box1 {
    height: 300px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: green;
  }
  .box2 {
    height: 100px;
    width: 100px;
    background-color: red;
  }
</style>
<div class="box1">
  <div class="box2"></div>
</div>

4.左侧定宽,右侧自适应

实现效果:

实现方式1: float + margin

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .left {
        width: 200px;
        height: 500px;
        background-color: orangered;
        float: left;
      }
      .right {
        margin-left: 200px;
        height: 500px;
        background-color: green;
      }
    </style>
  </head>
  <body>
    <div class="left"></div>
    <div class="right"></div>
  </body>
</html>

原理:让左边的盒子浮动,脱离文档流,右边盒子会往上挤,这个时候一部分被左边盒子盖住,设置一下右边盒子的margin-left值,让这个值等于或者大于左边盒子的宽度,就能让被盖住的部分显示出来, 右边盒子不设置宽度,默认填充完剩下部分

实现方式2: float+overflow

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .left {
        width: 200px;
        height: 500px;
        background-color: orangered;
        float: left;
      }
      .right {
        height: 500px;
        background-color: green;
        /*触发bfc达到自适应*/
        overflow: hidden;
      }
    </style>
  </head>
  <body>
    <div class="left"></div>
    <div class="right"></div>
  </body>
</html>

这种方式不需要去计算margin-left值

实现方式3: 使用表格形式

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .parent {
        display: table;
        height: 500px;
        width: 100%;
      }
      .left,
      .right {
        display: table-cell;
      }
      .left {
        width: 200px;
        background-color: orangered;
      }
      .right {
        background-color: green;
        /*触发bfc达到自适应*/
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

这种方式,适用于宽度高度未知情况;兼容性好(ie8+),注意,这种方式也有很多缺点,margin会失效;设置间隔比较麻烦;设置tabl-cell的元素,宽度和高度的值设置百分比无效,需要给它的父元素设置display: table; 才生效;table-cell不感知margin,在父元素上设置table-row等属性,也会使其不感知height;设置float或position会对默认布局造成破坏,可以考虑为之增加一个父div定义float等属性;内容溢出时会自动撑开父元素

实现方式4: 使用position实现

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .parent {
        position: relative;
        height: 500px;
        width: 100%;
      }

      .left {
        width: 200px;
        background-color: orangered;
        position: absolute;
        height: 500px;
        left: 0;
        top: 0;
      }
      .right {
        background-color: green;
        position: absolute;
        top: 0;
        right: 0;
        left: 100px;
        height: 500px;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

这种方式的好处是兼容性好 容易理解,缺点就是要去计算位置

方式5: 使用flex方式实现

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .parent {
        display: flex;
        height: 500px;
        width: 100%;
      }

      .left {
        width: 200px;
        background-color: orangered;
      }
      .right {
        background-color: green;
        flex: 1;
        height: 500px;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

方式6: 使用grid来实现

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .parent {
        display: grid;
        height: 500px;
        width: 100%;
        grid-template-columns: 300px auto;
      }

      .left {
        background-color: #ff4500;
      }
      .right {
        background-color: green;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

5.右侧定宽,左侧自适应

方式和 左侧定宽类似,这里不做过多讲解

1. float + margin
2. float + overflow
3. 使用table
4. 使用position
5. 使用flex
6. 使用grid

6.一列不定,一列自适应

float+overflow方式实现

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .left {
        float: left;
        height: 500px;

        background-color: #ff4500;
      }
      .right {
        background-color: green;
        overflow: hidden;
        height: 500px;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="left">
        hellohello
      </div>
      <div class="right"></div>
    </div>
  </body>
</html>

7.两列定宽,一列自适应

方式1: float + margin

原理:两个盒子浮动,另一个盒子计算好两个盒子的宽度、间隔之和设置一个margin值

<body>
<div id="parent">
    <div id="left">左列定宽</div>
    <div id="center">中间定宽</div>
    <div id="right">右列自适应</div>
</div>
</body>
#parent{min-width: 310px;} /*100+10+200,防止宽度不够,子元素换行*/
#left {
    margin-right: 10px;  /*#left和#center间隔*/
    float: left;
    width: 100px;
    height: 500px;
    background-color: #f00;
}
#center{
    float: left;
    width: 200px;
    height: 500px;
    background-color: #eeff2b;
}
#right {
    margin-left: 320px;  /*等于#left和#center的宽度之和加上间隔,多出来的就是#right和#center的间隔*/
    height: 500px;
    background-color: #0f0;
}

方式2: float + overflow

原理:两个盒子浮动,另一个盒子触发bfc达到自适应

<body>
<div id="parent">
    <div id="left">左列定宽</div>
    <div id="center">中间定宽</div>
    <div id="right">右列自适应</div>
</div>
</body>
#parent{min-width: 320px;} /*100+10+200+20,防止宽度不够,子元素换行*/
#left {
    margin-right: 10px; /*间隔*/
    float: left;
    width: 100px;
    height: 500px;
    background-color: #f00;
}
#center{
    margin-right: 10px; /*在此定义和#right的间隔*/
    float: left;
    width: 200px;
    height: 500px;
    background-color: #eeff2b;
}
#right {
    overflow: hidden;  /*触发bfc*/
    height: 500px;
    background-color: #0f0;
}

8.两列定宽,中间自适应

方式1: 双飞翼布局

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .header {
        height: 60px;
        background-color: black;
      }
      .left {
        float: left;
        height: 500px;
        width: 100px;
        background-color: green;
        margin-left: -100%;
        opacity: 0.1;
      }
      .center {
        float: left;
        height: 500px;
        background-color: orchid;
        width: 100%;
      }
      .right {
        float: left;
        height: 500px;
        background-color: blue;
        width: 200px;
        margin-left: -200px;
        opacity: 0.1;
      }
      .inner {
        background-color: red;
        height: 500px;
        margin: 0 220px 0 120px;
      }
      .footer {
        height: 120px;
        background-color: #ccc;
        clear: both;
      }
    </style>
  </head>
  <body>
    <div class="header"></div>
    <div class="main">
      <div class="center">
        <div class="inner"></div>
      </div>
      <div class="left"></div>
      <div class="right"></div>
    </div>
    <div class="footer"></div>
  </body>
</html>

方式2: 圣杯布局

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .header {
        height: 60px;
        background-color: black;
      }
      .main {
        height: 500px;
        background-color: #999;
        padding: 0 200px 0 100px;
      }
      .left {
        float: left;
        width: 100px;
        height: 500px;
        background-color: blue;
        margin-left: -100%;
        position: relative;
        left: -100px;
        /* opacity: 0.1; */
      }
      .center {
        width: 100%;
        height: 500px;
        background-color: orchid;
        float: left;
      }
      .right {
        width: 200px;
        height: 500px;
        background-color: green;
        float: left;
        margin-left: -200px;
        position: relative;
        left: 200px;
      }
      .footer {
        height: 120px;
        background-color: #ccc;
        clear: both;
      }
    </style>
  </head>
  <body>
    <div class="header"></div>
    <div class="main">
      <div class="center">
        <div class="inner"></div>
      </div>
      <div class="left"></div>
      <div class="right"></div>
    </div>
    <div class="footer"></div>
  </body>
</html>

方式3: 使用flex实现

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .header {
        height: 60px;
        background-color: black;
      }
      .main {
        height: 500px;
        background-color: #999;
        display: flex;
      }
      .left {
        width: 100px;
        height: 500px;
        background-color: green;
      }
      .center {
        flex: 1;
        height: 500px;
        background-color: red;
      }
      .right {
        width: 200px;
        height: 500px;
        background-color: blue;
      }
      .footer {
        height: 120px;
        background-color: #ccc;
      }
    </style>
  </head>
  <body>
    <div class="header"></div>
    <div class="main">
      <div class="left"></div>
      <div class="center"></div>
      <div class="right"></div>
    </div>
    <div class="footer"></div>
  </body>
</html>

9.四列等宽布局

10.多列等宽布局

11.九宫格布局

12.栅格系统

13.全屏布局

14.后台布局