一般的两栏布局,指的是左侧固定,右侧自适应。
可以利用浮动,左侧浮动,右侧设置 margin-left;右侧也可以设置 overflow: hidden,触发 BFC,因为触发 BFC 的元素不会和浮动元素重叠。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
.outer {
height: 100%;
}
.left {
width: 200px;
height: 100%;
background-color: deeppink;
float: left;
}
.right {
height: 100%;
background-color: pink;
margin-left: 200px;
}
</style>
</head>
<body>
<div class="outer">
<div class="left">left</div>
<div class="right">right</div>
</div>
</body>
</html>
利用 flex 布局,父级设置 flex,左侧宽度写死,右侧设置 flex为1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } html, body { height: 100%; } .outer { height: 100%; display: flex; } .left { width: 200px; height: 100%; background-color: deeppink; } .right { height: 100%; background-color: pink; flex: 1; } </style> </head> <body> <div class="outer"> <div class="left">left</div> <div class="right">right</div> </div> </body> </html>
利用定位,给父元素设置相对定位,左侧设置 absolute,width 写死,右侧设置 margin-left 即可
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } html, body { height: 100%; } .outer { height: 100%; position: relative; } .left { width: 200px; height: 100%; background-color: deeppink; position: absolute; } .right { height: 100%; background-color: pink; margin-left: 200px; } </style> </head> <body> <div class="outer"> <div class="left">left</div> <div class="right">right</div> </div> </body> </html>