1. 左侧固定右侧自适应的方法
- 定位法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.father{
width: 100%;
height: 300px;
background-color: yellow;
position: relative;
padding-left: 200px;
box-sizing: border-box;
}
.left{
position: absolute;
left: 0;
top: 0;
width: 200px;
height: 80%;
background-color: snow;
}
.right{
width: 100%;
height: 100%;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="father">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
- flex法(伸缩盒子)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.father{
width: 100%;
height: 500px;
background-color: lightblue;
display: flex;
}
.left{
width: 300px;
height: 90%;
background-color: rgb(175, 175, 49);
}
.right{
flex: 1;
background-color: lightgreen;
}
</style>
</head>
<body>
<div class="father">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
- bfc 法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.father{
width: 100%;
height: 500px;
background-color: rgb(125, 138, 196);
}
.left{
float: left;
width: 200px;
height: 80%;
background-color: lightblue;
}
.right{
overflow: hidden;
/* 注意点右侧不可设置宽度 */
height:450px;
background-color: yellow;
}
/* 利用 bfc 实现的左侧固定,右侧自适应
1--让左侧的盒子左浮动个,
2--让右侧的盒子 overflow:hidden;
注意点右边的盒子不可设置宽度。
bfc就素块级格式化上下文
特点:
1--成为了bfc的盒子,就是一个独立的渲染区域,子元素的布局不会影响的外面的其他元素。
应用: 清除浮动
解决塌陷的问题
2-- 成为了bfc 的盒子额,不能与浮动的元素重叠
用在:实现左侧固定右侧自适应。或者圣杯
触发bfc
1-- 浮动
2-- display:table /table-cell
3-- o=position:fixed/absolute
4-- overflow 非 visible (hidden auto scroll)
*/
</style>
</head>
<body>
<div class="father">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
2. 右侧固定左侧自适应的方法
- 定位法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.father{
width:100%;
height: 500px;
background-color: lightblue;
position: relative;
padding-right: 300px;
box-sizing: border-box;
}
.rigth{
position: absolute;
right: 0px;
top: 0px;
width: 300px;
height: 90%;
background-color: rgb(21, 128, 21);
}
.left{
height: 95%;
background-color:#ccc;
}
</style>
</head>
<body>
<div class="father">
<div class="left"></div>
<div class="rigth"></div>
</div>
</body>
</html>
- flex法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.father{
width:100%;
height: 500px;
background-color: lightblue;
}
.rigth{
float: right;
width: 300px;
height: 90%;
background-color: rgb(21, 128, 21);
}
.left{
height: 95%;
overflow: hidden;
background-color: lightyellow;
}
</style>
</head>
<body>
<div class="father">
<div class="rigth"></div>
<div class="left"></div>
</div>
</body>
</html>
- bfc法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.father{
width: 100%;
height: 500px;
background-color: skyblue;
}
.left{
height: 100%;
overflow: hidden;
background-color: yellow;
}
.right{
float: right;
width: 200px;
height: 70%;
background-color: #ccc;
}
</style>
</head>
<body>
<div class="father">
<!--注意先放了右边吖-->
<div class="right"></div>
<div class="left"></div>
</div>
</body>
</html>
3. 左右两边固定中间自适应的方法
- 定位法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.outer{
width: 100%;
height: 300px;
background-color: skyblue;
position: relative;
padding: 0px 200px;
box-sizing: border-box;
}
.left{
width: 200px;
height: 80%;
background-color: #fe2;
position: absolute;
left: 0;
top:0;
}
.right{
width: 200px;
height: 80%;
position: absolute;
background-color: #dae;
right: 0px;
top:0px;
}
.mid{
overflow: hidden;
height: 100%;
background-color: lightgreen;
}
</style>
</head>
<body>
<div class="outer">
<div class="left"></div>
<div class="mid"></div>
<div class="right"></div>
</div>
</body>
</html>
- flex法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box{
display: flex;
width: 100%;
height: 400px;
background-color: lightblue;
}
.left{
height: 80%;
width: 200px;
background-color: yellow;
}
.right{
float: left;
height: 80%;
width: 200px;
background-color: #ccc;
}
.center{
height: 200px;
background-color: lightpink;
flex: 1;
}
</style>
</head>
<body>
<div class="box">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
</html>
- bfc 法 (注意是左右先写浮动的元素,它们脱标了,后面的标准流的元素才会上去)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.outer{
width: 100%;
height: 300px;
background-color: #ad0;
}
.left{
width: 200px;
height: 90%;
background-color: rgb(230, 230, 115);
float: left;
}
.right{
width: 200px;
height: 90%;
background-color: lightgrey;
float: right;
}
.mid{
overflow: hidden;
height: 100%;
background-color: #348;
}
</style>
</head>
<body>
<div class="outer">
<div class="left"></div>
<div class="right"></div>
<div class="mid"></div>
</div>
</body>
</html>