方法1:
左边左浮动 右边又浮动 中间的写在html结构的最后加左右的margin
<!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>
body {
margin: 0px;
padding: 0px;
overflow: hidden;
min-width: 950px;
}
.left {
width: 300px;
background-color: red;
float: left;
height: 200px;
}
.right {
width: 300px;
background-color: blue;
float: right;
height: 200px;
}
.main {
background-color: gray;
margin: 0 300px;
height: 200px;
}
</style>
</head>
<body>
<div class="left">left</div>
<div class="right">right</div>
<div class="main">main</div>
</body>
</html>
方法2:
与方法1类似 用定位代替margin
<!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>
body {
margin: 0px;
padding: 0px;
overflow: hidden;
position: relative;
min-width: 950px;
}
.left {
width: 300px;
height: 200px;
background-color: red;
float: left;
}
.right {
width: 300px;
background-color: blue;
float: right;
height: 200px;
}
.main {
background-color: gray;
position: absolute;
left: 300px;
right: 300px;
height: 200px;
}
</style>
</head>
<body>
<div class="left">left</div>
<div class="main">main</div>
<div class="right">right</div>
</body>
</html>
方法3: 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>
.flex {
display: flex;
}
.left,
.right {
width: 300px;
height: 200px;
background-color: red;
}
.right {
background-color: blue;
}
.main {
background-color: #ccc;
flex: 1;
}
</style>
</head>
<body>
<div class="flex">
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
</div>
</body>
</html>
方法4:双飞翼布局
<!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>
body::after {
display: block;
content: '';
clear: both;
}
.mainCounter {
width: 100%;
float: left;
height: 200px;
}
.main {
margin:0 300px;
background-color: #ccc;
height: 200px;
}
.left,.right{
width: 300px;
height: 200px;
float: left;
background-color: red;
height: 200px;
}
.left {
margin-left: -100%;
}
.right {
background-color:blue;
margin-left:-300px;
}
</style>
</head>
<body>
<div class="mainCounter"><div class="main">中间</div></div>
<div class="left">左边栏</div>
<div class="right">右边栏</div>
</body>
</html>
方法5:圣杯布局
<!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>
.container {
box-sizing: border-box;
width: 100%;
height: 200px;
padding: 0 300px;
min-width: 950px;
}
.main {
width: 100%;
float: left;
height: 200px;
background-color: #ccc;
}
.left,
.right {
float: left;
width: 300px;
height: 200px;
background-color: red;
position: relative;
}
.left {
margin-left: -100%;
left: -300px;
}
.right {
margin-left: -300px;
right: -300px;
}
</style>
</head>
<body>
<div class="container">
<div class="main">main</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
</body>
</html>