四种方法实现CSS三栏布局(圣杯布局、双飞翼布局)

1,375 阅读1分钟

方法一:浮动法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>CSS实现三栏布局——float</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }
        
        .container{
            overflow: auto;
        }
      
        .left{
            width: 200px;
            height: 200px;
            background: red;
            
            float: left;
        }

        .middle{
            height: 200px;
            background: yellow;
        }

        .right{
            width: 200px;
            height: 200px;
            background: blue;
            
            float: right;
        } 
    </style>
</head>
<body>
    <div class="container">
        <div class="left">左栏</div>
        <div class="right">右栏</div>
        <div class="middle">中间栏</div>
    </div>
</body>
</html>

优化版:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>CSS实现三栏布局——float</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }
        
        .container > div{
            float: left;
            height: 200px;
        }
      
        .left{
            width: 200px;
            background: red;
            
            margin-left: -100%;
        }

        .middle{
            width: 100%;
            background: yellow;
          
            box-sizing: border-box;
            padding: 0 200px;
        }

        .right{
            width: 200px;
            background: blue;
            
            margin-left: -200px;
        } 
    </style>
</head>
<body>
    <div class="container">
        <div class="middle">中间栏</div>
        <div class="left">左栏</div>
        <div class="right">右栏</div>
    </div>
</body>
</html>

方法二:绝对定位法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>CSS实现三栏布局——Position</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }
        
        .container {
            position: relative;
        }
        
        .left {
            width: 200px;
            height: 200px;
            background: red;
        
            position: absolute;
            top: 0;
            left: 0;
        }
        
        .middle {
            height: 200px;
            background: yellow;
        
            margin-left: 200px;
        }
        
        .right {
            width: 200px;
            height: 200px;
            background: blue;
        
            position: absolute;
            top: 0;
            right: 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="middle">中间栏</div>
        <div class="left">左栏</div>
        <div class="right">右栏</div>
    </div>
</body>
</html>

方法三:Flex

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>CSS实现三栏布局——Flex</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }
        
        .container {
            display: flex;
        }
        
        .left {
            width: 200px;
            height: 200px;
            background: red;
        }
        
        .middle {
            height: 200px;
            background: yellow;
          
            flex: auto;
        }
        
        .right {
            width: 200px;
            height: 200px;
            background: blue;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="left">左栏</div>
        <div class="middle">中间栏</div>
        <div class="right">右栏</div>
    </div>
</body>
</html>

方法四:Grid

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>CSS实现三栏布局——Grid</title>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
        }
        
        .container {
            height: 200px;
            display: grid;
            grid-template-columns: 200px 1fr 200px;
        }
        
        .left {
            background: red;
        }
        
        .middle {
            background: yellow;
        }
        
        .right {
            background: blue;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="left">左栏</div>
        <div class="right">右栏</div>
        <div class="middle">中间栏</div>
    </div>
</body>
</html>

参考链接

不懂圣杯布局?5种方式包教包会

四种方法实现──三栏布局(圣杯布局、双飞翼布局)

三种方法实现CSS三栏布局