css常见布局

120 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>两栏布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box{
            
        }
        /* .left {
            float: left;
            width: 200px;
            background-color: salmon;
            height: 100vh;
        }
        .right {
            margin-left: 210px;
            background-color: royalblue;
            height: 100vh;
            margin: 0;
        } */
        .box {
            display: flex;
        }
        .left {
            width: 200px;
            background-color: salmon;
            height: 100vh;
        }
        .right {
            flex: 1;
            background-color: royalblue;
            height: 100vh;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="left">左边</div>
        <div class="right">右边</div>
    </div>
</body>
</html>

效果图:

image.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三栏布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        /* 
            flex 布局
         */
        /* .container {
            display: flex;
            justify-content: space-between;
            height: 500px;
        }
        .center {
            width: 100%;
            background-color: springgreen;
        }
        .left, .right {
            width: 200px;
        }
        .left{
            background-color: thistle;
        }
        .right {
            background-color: steelblue;
        } */
        /* 
            grid 布局
         */
        .container {
            display: grid;
            grid-template-columns: 200px auto 200px;
            width: 100vw;
            height: 100vh;
            margin: 0;
        }
        .left {
            background-color: thistle;
        }
        .center {
            background-color: turquoise;
        }
        .right {
            background-color: steelblue;
        }
        
    </style>
</head>
<body>
    <div class="container">
        <div class="left">left</div>
        <div class="center">center</div>
        <div class="right">right</div>
    </div>
</body>
</html>

效果图:

image.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>grail圣杯布局</title>
    <style>
        #header,#footer{
            height: 100px;
            background-color: #ccc;
            border: 2px solid red;
        }
        #container{
            display: flex;
            width: calc(100% -200px);
            height: 100vh;
        }
        #center{flex: 1;}
        #left{
            width: 100px;
            background-color: orange;
        }
        #right{
            width: 100px;
            background-color: orchid;
        }
    </style>
</head>
<body>
    <div id="header">header</div>
    <div id="container">
        <div id="left">left</div>
        <div id="center">center</div>
        <div id="right">right</div>
    </div>
    <div id="footer">footer</div>
</body>
</html>

效果图: image.png