文章目录
- CSS 常见布局
- 单列布局
- 单列布局(不通栏)
- 单列布局(通栏)
- 双列布局
- float+overflow:hidden
- flex
- grid
CSS 常见布局
单列布局
单列布局(不通栏)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>单列布局(不通栏)</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.header {
margin: 0 auto;
max-width: 960px;
height: 100px;
background-color: orangered;
}
.content {
margin: 0 auto;
max-width: 960px;
height: 400px;
background-color: seagreen;
}
.footer {
margin: 0 auto;
max-width: 960px;
height: 100px;
background-color: slateblue;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="content"></div>
<div class="footer"></div>
</body>
</html>
单列布局(通栏)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>单列布局(通栏)</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.header {
margin: 0 auto;
height: 100px;
background-color: orangered;
overflow: hidden;
}
.nav {
margin: 10px auto 0;
max-width: 800px;
background-color: darkgray;
height: 50px;
}
.content {
margin: 0 auto;
max-width: 960px;
height: 400px;
background-color: seagreen;
}
.footer {
margin: 0 auto;
height: 100px;
background-color: slateblue;
}
</style>
</head>
<body>
<div class="header">
<div class="nav"></div>
</div>
<div class="content"></div>
<div class="footer"></div>
</body>
</html>
双列布局
双列布局,一列固定宽度,另一列填充用剩余空间。
float+overflow:hidden
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>双列布局(float+overflow:hidden)</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.container {
height: 500px;
background-color: palevioletred;
overflow: hidden;
zoom: 1;
}
.left {
width: 200px;
height: 100%;
background-color: lightgreen;
float: left;
margin-right: 20px;
}
.right {
height: 100%;
background-color: lightblue;
overflow: hidden;
zoom: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
flex
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>双列布局(flex)</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.container {
height: 500px;
background-color: palevioletred;
display: flex;
}
.left {
width: 200px;
height: 100%;
background-color: lightgreen;
margin-right: 20px;
}
.right {
height: 100%;
background-color: lightblue;
flex: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
grid
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>双列布局(grid)</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.container {
height: 500px;
background-color: palevioletred;
display: grid;
grid-template-columns: auto 1fr;
grid-gap: 20px;
}
.left {
width: 200px;
background-color: lightgreen;
}
.right {
background-color: lightblue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>