圣杯布局 float + 负 margin

使用 float + 负 margin 创建 圣杯布局

by Boyanliuu

HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>圣杯布局</title>
    </head>
    <body>
        <header> header</header>
        <div class="clearfix wrapper">
            <div class="center">
                Main content
            </div>
            <div class="left">
                Left content
            </div>
            <div class="right">
                right content
            </div>
        </div>
        <footer> footer</footer>
    </body>
</html>

CSS

:root {
    box-sizing: border-box;
}

*,
::before,
::after {
    box-sizing: inherit;
    margin: 0;
    padding: 0;
}


.center{
  background:lightblue;
}

.left{
  background:gold;
}
.right{
  background:yellow;
}
footer , header{
  background:CornflowerBlue;
}


.center,.left,.right{
  float:left;
  text-align:center;
  
}
.clearfix::after{
  content: "";
  display:block;
  clear: both;
}

.wrapper{
  padding: 0 100px;
}

.left,.right{
  width:100px;
}
.left{
  margin-left:-100%;
  position: relative;
 left:-100px;
}
.center{
  width:100%;
}
.right{
  margin-left:-100px;
  position: relative;
  right: -100px;
}