圣杯布局详解三步
总结:圣杯布局用到了文档流,浮动流,定位流。还有用到了负margin的。圣杯布局解决了先加载主要内容的问题
把大象装进冰箱需要几步?
1.布局成这个样子,中间一个容器包裹中间,左边,右边
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no,viewport=cover">
<title>Document</title>
<style>
body {
width: 100%;
}
.header {
width: 100%;
height: 50px;
background-color: #878e7d;
}
.center {
width: 100%;
background-color: #6737c6;
}
.left {
width: 200px;
background-color: #369b42;
}
.right {
width: 150px;
background-color: #d68419;
}
.footer {
width: 100%;
height: 50px;
background-color: #9dc4c3;
}
.box {
/* 居中文字 */
text-align: center;
}
</style>
</head>
<body>
<header class="header box">header</header>
<section class="section clearfix">
<div class="center column box">中间部分</div>
<div class="left column box">左边部分</div>
<div class="right column box">右边部分</div>
</section>
<footer class="footer box">footer</footer>
</body>
</html>
2.开启左浮动,并清除浮动影响,并设置父元素padding
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no,viewport=cover">
<title>Document</title>
<style>
body {
width: 100%;
}
.header {
width: 100%;
height: 50px;
background-color: #878e7d;
}
.box {
/* 居中文字 */
text-align: center;
}
.section {
padding-left: 200px;
padding-right: 150px;
}
.column {
/* 2.开启左浮动,并清除浮动并给父元素设置padding */
float: left;
}
/* 清除浮动 */
.clearfix:after {
content: '';
display: block;
height: 0;
visibility: hidden;
clear: both;
}
.clearfix {
/* IE的清除浮动,触发hasLayout */
zoom: 1;
}
.center {
width: 100%;
background-color: #6737c6;
}
.left {
width: 200px;
background-color: #369b42;
}
.right {
width: 150px;
background-color: #d68419;
}
.footer {
width: 100%;
height: 50px;
background-color: #9dc4c3;
}
</style>
</head>
<body>
<header class="header box">header</header>
<section class="section clearfix">
<div class="center column box">中间部分</div>
<div class="left column box">左边部分</div>
<div class="right column box">右边部分</div>
</section>
<footer class="footer box">footer</footer>
</body>
</html>
3.把左右两边使用负margin和positon realtive 放到对应的位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no,viewport=cover">
<title>Document</title>
<style>
body {
width: 100%;
}
.header {
width: 100%;
height: 50px;
background-color: #878e7d;
}
.box {
/* 居中文字 */
text-align: center;
}
.section {
padding-left: 200px;
padding-right: 150px;
}
.column {
/* 2.开启左浮动,并清除浮动并给父元素设置padding */
float: left;
position: relative;
}
/* 清除浮动 */
.clearfix:after {
content: '';
display: block;
height: 0;
visibility: hidden;
clear: both;
}
.clearfix {
/* IE的清除浮动,触发hasLayout */
zoom: 1;
}
.center {
width: 100%;
background-color: #6737c6;
}
.left {
width: 200px;
background-color: #369b42;
/* 3.使用负margin 和 realtive 把左右两边放到位置 */
margin-left: -100%;
left: -200px;
}
.right {
width: 150px;
background-color: #d68419;
margin-right: -150px;
/* 除了 margin-left -150 也可以使用margin-left */
/* margin-left: -150px;
right: -150px; */
}
.footer {
width: 100%;
height: 50px;
background-color: #9dc4c3;
}
</style>
</head>
<body>
<header class="header box">header</header>
<section class="section clearfix">
<div class="center column box">中间部分</div>
<div class="left column box">左边部分</div>
<div class="right column box">右边部分</div>
</section>
<footer class="footer box">footer</footer>
</body>
</html>
注意: 在第3步可能难以理解,那是因为margin-left= 负数元素宽度,因为3个元素都是浮动的,左边就跟中间部分重合了。右边部分没有右marigin之后,浏览器就看不见它了,就安排到父容器的右边padding位置了