单栏布局
一、header,content 和 footer 等宽的单列布局
对于第一种,先通过对 header,content,footer 统一设置 width:1000px;或者 max-width:1000px(这两者的区别是当屏幕小于 1000px 时,前者会出现滚动条,后者则不会,显示出实际宽度);然后设置 margin:0 auto
实现居中即可得到。
<!DOCTYPE html>
<html lang="en">
<!-- header,content和footer等宽的单列布局。 -->
<!-- max-width:560px;用最大宽度,当宽度变小时会以浏览器真是宽度为基准 -->
<!-- width: 560px; 定宽 用宽度,当宽度变小时会出现滚动条 -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>单列布局</title>
<style>
.header {
max-width: 1000px;
height: 100px;
background-color: yellowgreen;
margin: 0 auto;
}
.content {
max-width: 1000px;
height: 500px;
background-color: bisque;
margin: 0 auto;
}
.footer {
max-width: 1000px;
height: 80px;
background-color: burlywood;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="content"></div>
<div class="footer"></div>
</body>
</html>
二、header 与 footer 等宽,content 略窄的单列布局
<!DOCTYPE html>
<html lang="en">
<!-- header和footer等宽,content略窄的单列布局。 单列布局(通栏) -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.header {
margin: 0 auto;
max-width: 960px;
height: 100px;
background-color: cadetblue;
}
.content {
margin: 0 auto;
max-width: 800px;
height: 400px;
background-color: coral
}
.footer {
margin: 0 auto;
max-width: 960px;
height: 100px;
background-color: darkseagreen;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="content"></div>
<div class="footer"></div>
</body>
</html>
两栏自适应布局
两列自适应布局是指一列由内容撑开,另一列撑满剩余宽度的布局方式
1. float+overflow:hidden
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>两列自适应布局-float实现</title>
<style>
.parent {
overflow: hidden;
}
.left {
float: left;
background-color: darkseagreen;
}
.right {
overflow: hidden;
background-color: gold;
}
</style>
</head>
<body>
<div class="parent">
<div class="left">
LeftLeftLeft
</div>
<div class="right">
Right
<br> Right
</div>
</div>
</body>
</html>
注意:如果侧边栏在右边时,注意渲染顺序。即在 HTML 中,先写侧边栏后写主内容
这种写法不能实现两列等高布局,若要实现,则要在.left 、 .right中把padding-bottom设为足够大的值,并且设置margin--bottom为与padding-bottom的正值相抵消的值。
.left {
float: left;
padding-bottom: 999px;
margin-bottom: -999px;
background-color: darkseagreen;
}
.right {
overflow: hidden;
padding-bottom: 999px;
margin-bottom: -999px;
background-color: gold;
}
2. Flexbox 布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>两列自适应布局-flex实现</title>
<style>
.parent {
display: flex;
}
.left {
background-color: greenyellow;
}
.right {
flex: 1;
background-color: khaki;
}
</style>
</head>
<body>
<div class="parent">
<div class="left">
LeftLeftLeftLeftLeftLeft
</div>
<div class="right">
Right
<br> Right
</div>
</div>
</body>
</html>
这种方法可以实现两列等高布局。
3. grid布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>两列自适应布局-grid布局</title>
<style>
.parent {
display: grid;
grid-template-columns: auto 1fr;
}
.left {
background-color: khaki;
}
.right {
background-color: lightcoral;
}
</style>
</head>
<body>
<div class="parent">
<div class="left">
LeftLeft
</div>
<div class="right">
Right
<br> Right
</div>
</div>
</body>
</html>
这种方法可以实现两列等高布局。
三栏布局:左右两栏宽度固定,中间宽度自适应
1. float布局
- 左边一栏
float:left
,右边一栏float:right
- 注意:在 HTML 中,先写侧边两栏后写主内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>三栏布局-float布局实现</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 300px;
}
.left {
float: left;
width: 300px;
background-color: bisque;
}
.center {
background-color: cadetblue;
}
.right {
float: right;
width: 300px;
background-color: chartreuse;
}
</style>
</head>
<body>
<div class="left"></div>
<div class="right"></div>
<div class="center"></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">
<title>三栏布局-绝对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
position: relative;
}
.left {
position: absolute;
left: 0;
width: 300px;
height: 300px;
background-color: chartreuse;
}
.center {
position: absolute;
left: 300px;
right: 300px;
height: 300px;
background-color: coral;
}
.right {
position: absolute;
right: 0;
width: 300px;
height: 300px;
background-color: cornflowerblue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
</html>
3. flex布局
中间一栏设置flex:1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>三栏布局-flex布局</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
display: flex;
}
.left {
width: 300px;
height: 300px;
background-color: cornflowerblue;
}
.center {
flex: 1;
height: 300px;
background-color: darksalmon;
}
.right {
width: 300px;
height: 300px;
background-color: darkorange;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
</html>
4. table布局
父级元素设置display:table
,还要设置width:100%
; 子级元素设置display:table-cell
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>三栏布局-table布局</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
display: table;
width: 100%;
}
.container div {
display: table-cell;
height: 300px;
}
.left {
width: 300px;
background-color: darksalmon;
}
.center {
background-color: greenyellow;
}
.right {
width: 300px;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
</html>
5. grid布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>三栏布局-grid布局</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
display: grid;
width: 100%;
grid-template-rows: 300px;
grid-template-columns: 300px auto 300px;
}
.left {
background-color: darksalmon;
}
.center {
background-color: greenyellow;
}
.right {
background-color: lightblue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
</html>
品字布局
1. 全屏版:百分比width + float:left
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>品字布局2(全屏版)</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 100%;
height: 100px;
line-height: 100px;
text-align: center;
}
.div1 {
background-color: red;
}
.div2 {
float: left;
width: 50%;
background-color: blue;
}
.div3 {
float: left;
width: 50%;
background-color: green;
}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</body>
</html>
2. 非全屏居中版:float + margin-left + transform: translateX(-100%)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>品字布局1</title>
<style>
body {
overflow: hidden;
}
* {
margin: 0;
padding: 0;
}
div {
margin: 0 auto;
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
}
.div1 {
background-color: red;
}
.div2 {
float: left;
margin-left: 50%;
transform: translateX(-100%);
background-color: blue;
}
.div3 {
float: left;
transform: translateX(-100%);
background-color: green;
}
</style>
</head>
<body>
<div class="div1">1</div>
<div class="div2">2</div>
<div class="div3">3</div>
</body>
</html>
设置margin-left:50%
的效果如下:
最终效果图:
圣杯布局:左右宽度固定,中间宽度自适应
参考学习链接:http://47.98.159.95/my_blog/blogs/css/006.html
1. flex布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.header,.footer{
height:40px;
width:100%;
background:red;
}
.container{
display: flex;
}
.middle{
flex: 1;
background:yellow;
}
.left{
width:200px;
background:pink;
}
.right{
background: aqua;
width:300px;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="middle">中间部分</div>
<div class="right">右边</div>
</div>
<div class="footer">这里是底部</div>
</body>
</html>
2. float布局(全部float:left)
<!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>
*{
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.footer {
clear: both;
}
.container {
padding-left: 200px;
padding-right: 250px;
}
.container div {
position: relative;
float: left;
}
.middle {
width: 100%;
background: yellow;
}
.left {
width: 200px;
background: pink;
margin-left: -100%;
left: -200px;
}
.right {
width: 250px;
background: aqua;
margin-left: -250px;
left: 250px;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="middle">中间部分</div>
<div class="left">左边</div>
<div class="right">右边</div>
</div>
<div class="footer">这里是底部</div>
</body>
</html>
- 左边的盒子设置margin-left: -100%是将盒子拉上去,效果:
接着再向左移动200px来填充空下来的padding-left部分,效果:
- 右边的盒子设置margin-left: -250px后,盒子在该行所占空间为0,因此直接到上面的middle块中,效果:
然后向右移动250px, 填充父容器的padding-right部分,效果:
3. float布局(左边float: left, 右边float: right)
<!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>
*{
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.container{
overflow: hidden;
}
.middle {
background: yellow;
}
.left {
float: left;
width: 200px;
background: pink;
}
.right {
float: right;
width: 250px;
background: aqua;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="right">右边</div>
<div class="middle">中间部分</div>
</div>
<div class="footer">这里是底部</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>
*{
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.container{
min-height: 1.2em;
position: relative;
}
.container>div {
position: absolute;
}
.middle {
left: 200px;
right: 250px;
background: yellow;
}
.left {
left: 0;
width: 200px;
background: pink;
}
.right {
right: 0;
width: 250px;
background: aqua;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="right">右边</div>
<div class="middle">中间部分</div>
</div>
<div class="footer">这里是底部</div>
</body>
</html>
5. grid布局
<!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{
display: grid;
}
#header{
background: red;
grid-row:1;
grid-column:1/5;
}
#left{
grid-row:2;
grid-column:1/2;
background: orange;
}
#right{
grid-row:2;
grid-column:4/5;
background: cadetblue;
}
#middle{
grid-row:2;
grid-column:2/4;
background: rebeccapurple
}
#footer{
background: gold;
grid-row:3;
grid-column:1/5;
}
</style>
</head>
<body>
<div id="header">header</div>
<div id="left">left</div>
<div id="middle">middle</div>
<div id="right">right</div>
<div id="footer">footer</footer></div>
</body>
</html>
双飞翼布局
采用经典的float布局
<!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>
*{
margin: 0;
padding: 0;
}
.container {
min-width: 600px;
}
.left {
float: left;
width: 200px;
height: 400px;
background: red;
margin-left: -100%;
}
.center {
float: left;
width: 100%;
height: 500px;
background: yellow;
}
.center .inner {
margin: 0 200px;
}
.right {
float: left;
width: 200px;
height: 400px;
background: blue;
margin-left: -200px;
}
</style>
</head>
<body>
<article class="container">
<div class="center">
<div class="inner">双飞翼布局</div>
</div>
<div class="left"></div>
<div class="right"></div>
</article>
</body>
</html>