几个实现两栏布局的方法(左侧固定,右侧自适应)
一、position(定位)
<!doctype html>
<html>
<head>
<meta http-equiv="ContentType" content="text/html;charset=utf-8">
<title>两栏布局</title>
<style>
.container{
position: relative;
}
.left{
position: absolute;
width: 200px;
background-color:brown;
height: 300px;
}
.right{
width: 100%;
background-color: blue;
height: 300px;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
</div>
<div class="right"></div>
</body>
</html>
二、float(浮动)
<!doctype html>
<html>
<head>
<meta http-equiv="ContentType" content="text/html;charset=utf-8">
<title>两栏布局</title>
<style>
.left{
float: left;
width: 200px;
background-color:brown;
height: 300px;
}
.right{
width: 100%;
background-color: blue;
height: 300px;
}
</style>
</head>
<body>
<div class="left"></div>
<div class="right"></div>
</body>
</html>
浮动元素会对后一个元素产生影响,对前一个不会,后一个元素会围着float元素。解决办法:clear属性或BFC。
三、flex(弹性盒子)
<!doctype html>
<html>
<head>
<meta http-equiv="ContentType" content="text/html;charset=utf-8">
<title>两栏布局</title>
<style>
.container {
display: flex;
}
.left {
width: 200px;
background-color: brown;
height: 300px;
}
.right {
flex: 1;
background-color: blue;
height: 300px;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
四、margin-left(类似双飞翼布局)
<!doctype html>
<html>
<head>
<meta http-equiv="ContentType" content="text/html;charset=utf-8">
<title>两栏布局</title>
<style>
.left {
float: left;
width: 200px;
background-color: brown;
height: 300px;
margin-left: -100%;
}
.right {
float: left;
width: 100%;
background-color: blue;
height: 300px;
}
.inner {
height: 300px;
padding: 0 0 0 200px;
}
</style>
</head>
<body>
<div class="right"></div>
<div class="left">
<div class="inner"></div>
</div>
</div>
</body>
</html>