CSS实现几种常见布局
- 两列左窄右宽型布局
<!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>两列左窄右宽型布局</title>
<style>
* {
margin: 0;
padding: 0;
}
.top,
.banner,
.main,
.footer {
width: 960px;
margin: 0 auto;
text-align: center;
border: 1px dashed #ccc;
background-color: #eee;
margin-bottom: 8px;
}
.top {
height: 80px;
}
.banner {
height: 150px;
}
.main {
height: 500px;
}
.left {
width: 360px;
height: 500px;
background-color: pink;
float: left;
}
.right {
width: 592px;
height: 500px;
background-color: purple;
float: right;
}
.footer {
height: 120px;
}
</style>
</head>
<body>
<div class="top">top</div>
<div class="banner">banner</div>
<div class="main">
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="footer">footer</div>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3812906418fee0f4650a7260a4f48b9b.png)
- 伸缩三等分布局
<!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>伸缩布局</title>
<style>
section {
width: 80%;
height: 200px;
border: 1px solid pink;
margin: 100px auto;
display: flex;
min-width: 500px;
flex-direction: column;
}
section div {
height: 100%
}
section div:nth-child(1) {
flex: 1;
background-color: pink;
}
section div:nth-child(2) {
flex: 1;
background-color: purple;
}
section div:nth-child(3) {
flex: 1;
background-color: blue;
}
</style>
</head>
<body>
<section>
<div>1</div>
<div>2</div>
<div>3</div>
</section>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/90c7b7a44669e6d8301d641180d9320f.png)
- 一列固定宽度且居中
<!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>常规布局</title>
<style>
* {
margin: 0;
padding: 0;
}
.top,
.banner,
.main,
.footer {
width: 960px;
margin: 0 auto;
text-align: center;
margin-bottom: 10px;
border: 1px dashed #ccc;
}
.top {
height: 80px;
background-color: pink;
}
.banner {
height: 120px;
background-color: purple;
}
.main {
height: 500px;
background-color: hotpink;
}
.footer {
height: 150px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="top">top</div>
<div class="banner">banner</div>
<div class="main">main</div>
<div class="footer">footer</div>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3dc17ac4eb9cbaf032da0fbbda2a4c6b.png)
- 通栏平均分布型
<!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>通栏平均分布型</title>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.top {
height: 60px;
background-color: #000;
}
.banner {
width: 960px;
height: 400px;
background-color: skyblue;
margin: 20px auto;
border-radius: 15px;
}
.main {
width: 960px;
height: 200px;
margin: 0 auto;
}
.main ul li {
width: 240px;
height: 200px;
background-color: pink;
float: left;
}
.main ul li:nth-child(even) {
background-color: pink;
}
.footer {
height: 100px;
background-color: #000;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="top">top</div>
<div class="banner">banner</div>
<div class="main">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
<div class="footer">footer</div>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6e3b15ede37d99507f0392365bac852f.png)