三栏布局
浮动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{
height: 300px;
}
.layout.float .left{
float:left;
width:300px;
background-color:red;
}
.layout.float .right{
float:right;
width:300px;
background-color:blue;
}
.layout.float .center {
background-color:orange;
}
</style>
</head>
<body>
<section class="layout float">
<!-- 利用浮动解决布局时,center必须在最后。 -->
<article class="left-right-center">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h1>浮动布局</h1>
1.这是浮动布局解决方案
2.这是浮动布局解决方案
</div>
</article>
</section>
</body>
</html>
定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{
height: 300px;
}
.layout.absolute .left-center-right>div{
position: absolute;
}
.layout.absolute .left{
left: 0;
width: 300px;
background-color: red;
}
.layout.absolute .center {
left: 300px;
right: 300px;
background-color: orange;
}
.layout.absolute .right{
right: 0;
width: 300px;
background-color: blue;
}
</style>
</head>
<body>
<section class="layout absolute">
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>绝对定位</h1>
1.这是绝对定位布局解决方案
2.这是绝对定位布局解决方案
</div>
<div class="right"></div>
</article>
</section>
</body>
</html>
圣杯布局和双飞翼布局
圣杯布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body {
min-width: 600px; /* 2*sub + extra */
}
.container {
padding-left: 210px;
padding-right: 190px;
}
.main {
float: left;
width: 100%;
height: 300px;
background-color: rgba(255, 0, 0, .5);
}
.sub {
position: relative;
left: -210px;
float: left;
width: 200px;
height: 300px;
margin-left: -100%;
background-color: rgba(0, 255, 0, .5);
}
.extra {
position: relative;
right: -190px;
float: left;
width: 180px;
height: 300px;
margin-left: -180px;
background-color: rgba(0, 0, 255, .5);
}
</style>
</head>
<body>
<div class="container">
<div class="main"></div>
<div class="sub"></div>
<div class="extra"></div>
</div>
</body>
</html>
双飞翼布局(淘宝使用的布局方式)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.main-wrapper {
float: left;
width: 100%;
}
.main {
height: 300px;
margin-left: 210px;
margin-right: 190px;
background-color: rgba(255, 0, 0, .5);
}
.sub {
float: left;
width: 200px;
height: 300px;
margin-left: -100%;
background-color: rgba(0, 255, 0, .5);
}
.extra {
float: left;
width: 180px;
height: 300px;
margin-left: -180px;
background-color: rgba(0, 0, 255, .5);
}
</style>
</head>
<body>
<div class="main-wrapper">
<div class="main"></div>
</div>
<div class="sub"></div>
<div class="extra"></div>
</body>
</html>
- 俩种布局方式都是把主列放在文档流最前面,使主列优先加载。
- 两种布局方式在实现上也有相同之处,都是让三列浮动,然后通过负外边距形成三列布局。
- 两种布局方式的不同之处在于如何处理中间主列的位置:圣杯布局是利用父容器的左、右内边距定位;双飞翼布局是把主列嵌套在div后利用主列的左、右外边距定位。
更多
- table
- flex
- 网格布局
最为推荐的还是上面的淘宝用的布局方式
左边定宽,右边自适应
浮动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{
height: 300px;
}
.left{
float:left;
width: 200px;
background-color: orange;
}
.right{
width:100%;
background-color: cadetblue;
margin-left: 200px; /*等于左边栏宽度可有可无 */
}
</style>
</head>
<body>
<!-- 左边定宽 -->
<div class="left">Left</div>
<!-- 右边自适应 -->
<div class="right">Right</div>
</body>
</html>
flex
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body{
display: flex;
}
div{
height: 300px;
}
.left{
width: 200px;
background-color: orange;
}
.right{
flex:1;
background-color: cadetblue;
}
</style>
</head>
<body>
<!-- 左边定宽 -->
<div class="left">Left</div>
<!-- 右边自适应 -->
<div class="right">Right</div>
</body>
</html>
定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div{
height: 300px;
}
.left{
position: absolute;
left:0;
width: 200px;
background-color: orange;
}
.right{
position: absolute;
left:200px;
width:100%;
background-color: cadetblue;
}
</style>
</head>
<body>
<!-- 左边定宽 -->
<div class="left">Left</div>
<!-- 右边自适应 -->
<div class="right">Right</div>
</body>
</html>
更多
- 使用负margin
- table
摘自:https://www.jianshu.com/p/01747a08bcbf
--
作者:刘伟波
链接:http://www.liuweibo.cn/p/239
来源:刘伟波博客
本文原创版权属于刘伟波 ,转载请注明出处,谢谢合作
发表评论: