CSS布局(二) 圣杯布局

什么是圣杯布局

圣杯布局是来源于该布局效果类似圣杯而得名。简单来说,就是三行三列布局。

CSS布局(二) 圣杯布局_第1张图片

第一种解决方案(calc)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圣杯布局</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .left,.right,.center{
            height: 300px;
            display: inline-block;
        }
        .left,.right{
            width: 300px;
        }

        .center{
            width: calc(100% - 600px);
        }

        .left{
            background-color: #c9394a;
        }

        .center{
            background-color: green;
        }

        .right{
            background-color: #ccc;
        }
    </style>
</head>
<body>
<header>我是头部</header>
<div><div class="left"></div><div class="center"></div><div class="right"></div></div>
<footer>我是尾部</footer>
</body>
</html>

第二种解决方案(浮动)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圣杯布局</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .left,.right,.center{
            height: 300px;
        }
        .left,.right{
            width: 300px;
        }

        .left{
            float: left;
        }
        .right{
            float: right;
        }
        .center{
            margin-left: 300px;
            margin-right: 300px;
        }
        .left{
            background-color: #c9394a;
        }

        .center{
            background-color: green;
        }

        .right{
            background-color: #ccc;
        }
    </style>
</head>
<body>
<header>我是头部</header>
<div>
    <div class="left"></div>
    <div class="right"></div>
    <div class="center"></div>
    <div style="clear: both"></div>
</div>
<footer>我是尾部</footer>
</body>
</html>

第三种

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圣杯布局</title>
    <style>
        .left,.right,.center{
            height: 300px;
            float: left;
        }
        .left,.right{
            width: 300px;
        }

        .center{
            width: 100%;
        }

        .left{
            background-color: #c9394a;
            margin-left: -100%;
            left: -300px;
            position: relative;
        }

        .center{
            background-color: green;
        }

        .right{
            background-color: #ccc;
            margin-left: -300px;
            position: relative;
            left:300px;
        }

        .parent{
            margin-left: 300px;
            margin-right: 300px;
        }
    </style>
</head>
<body>
<header>我是头部</header>
<div class="parent">
    <div class="center"></div>
    <div class="left"></div>
    <div class="right"></div>
    <div style="clear: both"></div>
</div>
<footer>我是尾部</footer>
</body>
</html>

你可能感兴趣的:(#,css)