前端常见布局

三栏布局

浮动

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    div{
      height: 300px;
    }
    .layout.float .left{
      float:left;
      width:300px;
      background-color:red;
    }
    .layout.float .right{
      float:right;
      width:300px;
      background-color:blue;
    }

    .layout.float .center {
      background-color:orange;
    }
  </style>
</head>
<body>
<section class="layout float">
  <!-- 利用浮动解决布局时,center必须在最后。 -->
  <article class="left-right-center">
    <div class="left"></div>
    <div class="right"></div>
    <div class="center">
      <h1>浮动布局</h1>
      1.这是浮动布局解决方案
      2.这是浮动布局解决方案
    </div>
  </article>
</section>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

定位

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    div{
      height: 300px;
    }
    .layout.absolute .left-center-right>div{
      position: absolute;
    }

    .layout.absolute  .left{
      left: 0;
      width: 300px;
      background-color: red;
    }

    .layout.absolute  .center {
      left: 300px;
      right: 300px;
      background-color: orange;
    }

    .layout.absolute  .right{
      right: 0;
      width: 300px;
      background-color: blue;
    }
  </style>
</head>
<body>
<section class="layout absolute">
  <article class="left-center-right">
    <div class="left"></div>
    <div class="center">
      <h1>绝对定位</h1>
      1.这是绝对定位布局解决方案
      2.这是绝对定位布局解决方案
    </div>
    <div class="right"></div>
  </article>
</section>

</body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

圣杯布局和双飞翼布局

圣杯布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        body {
            min-width: 600px; /* 2*sub + extra */
        }
        .container {
            padding-left: 210px;
            padding-right: 190px;
        }
        .main {
            float: left;
            width: 100%;
            height: 300px;
            background-color: rgba(255, 0, 0, .5);
        }
        .sub {
            position: relative;
            left: -210px;
            float: left;
            width: 200px;
            height: 300px;
            margin-left: -100%;
            background-color: rgba(0, 255, 0, .5);
        }
        .extra {
            position: relative;
            right: -190px;
            float: left;
            width: 180px;
            height: 300px;
            margin-left: -180px;
            background-color: rgba(0, 0, 255, .5);
        }
    </style>
</head>
<body>
<div class="container">
    <div class="main"></div>
    <div class="sub"></div>
    <div class="extra"></div>
</div>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

双飞翼布局(淘宝使用的布局方式)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .main-wrapper {
            float: left;
            width: 100%;
        }
        .main {
            height: 300px;
            margin-left: 210px;
            margin-right: 190px;
            background-color: rgba(255, 0, 0, .5);
        }
        .sub {
            float: left;
            width: 200px;
            height: 300px;
            margin-left: -100%;
            background-color: rgba(0, 255, 0, .5);
        }
        .extra {
            float: left;
            width: 180px;
            height: 300px;
            margin-left: -180px;
            background-color: rgba(0, 0, 255, .5);
        }
    </style>
</head>
<body>
    <div class="main-wrapper">
        <div class="main"></div>
    </div>
    <div class="sub"></div>
    <div class="extra"></div>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  • 俩种布局方式都是把主列放在文档流最前面,使主列优先加载。
  • 两种布局方式在实现上也有相同之处,都是让三列浮动,然后通过负外边距形成三列布局。
  • 两种布局方式的不同之处在于如何处理中间主列的位置:圣杯布局是利用父容器的左、右内边距定位;双飞翼布局是把主列嵌套在div后利用主列的左、右外边距定位。

更多

  • table
  • flex
  • 网格布局

最为推荐的还是上面的淘宝用的布局方式

左边定宽,右边自适应

浮动

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    div{
      height: 300px;
    }
    .left{
      float:left;
      width: 200px;
      background-color: orange;
    }
    .right{
      width:100%;
      background-color: cadetblue;
      margin-left: 200px; /*等于左边栏宽度可有可无 */
    }
  </style>
</head>
<body>
<!-- 左边定宽 -->
<div class="left">Left</div>
<!-- 右边自适应 -->
<div class="right">Right</div>

</body>
</html>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

flex

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    body{
      display: flex;
    }
    div{
      height: 300px;
    }
    .left{
      width: 200px;
      background-color: orange;
    }
    .right{
      flex:1;
      background-color: cadetblue;
    }
  </style>
</head>
<body>
<!-- 左边定宽 -->
<div class="left">Left</div>
<!-- 右边自适应 -->
<div class="right">Right</div>

</body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

定位

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    div{
      height: 300px;
    }
    .left{
      position: absolute;
      left:0;
      width: 200px;
      background-color: orange;
    }
    .right{
      position: absolute;
      left:200px;
      width:100%;
      background-color: cadetblue;
    }
  </style>
</head>
<body>
<!-- 左边定宽 -->
<div class="left">Left</div>
<!-- 右边自适应 -->
<div class="right">Right</div>

</body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

更多

  • 使用负margin
  • table

摘自:https://www.jianshu.com/p/01747a08bcbf在新窗口打开

今日图 - 你和 Bug 的距离

16a9573bc26f194a.png