Wang's blog Wang's blog
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Wang Mings

跟随大神,成为大神!
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • CSS

    • 常用css
    • sass用法
    • viewport详解
    • css各种布局
      • 圣杯布局
      • 单列布局
        • 1.1 水平居中
        • 子元素 margin:0 auto;
        • 父元素:relative;子元素:absolute;left:50%;margin-left:-宽度的一半
        • 父元素:relative;子元素:absolute;left:50%;transform:translate(-50%,0);
        • 弹性盒子:父元素:display:flex;justify-content:center;
        • 1.2 垂直居中
        • vertical-align:center;
        • 父元素:position:relative;子元素:positon:absolute;top:50%;transform:translate(0,-50%);
        • 父元素:position:relative;子元素:positon:absolute;top:50%;margin-top:-子元素高度的一半;
        • 父元素:display:flex;align-items:center;
        • 1.3 水平垂直居中
        • 父元素:display:table-cell;vertical-align:middle;text-align:center;子元素;display:inline-block;
        • 父元素:position:relative;子元素:position:absolute?50%;left:50%;margin-left:宽度的一半;margin-top:高度的一半;或者 transform:translate(-50%,-50%);
        • 父元素{display:flex;justify-content:center;align-items:center;}
      • 2.多列布局
        • 2.1 左侧定宽,右侧自适应 第一种
        • 2.1 左侧定宽,右侧自适应 第二种
        • 2.2 右侧定宽左侧自适应
        • 2.3 左边两列定宽,右侧自适应
    • a标签小妙用
    • flex布局语法
    • CSS-function汇总
    • CSS变量的使用
    • css使用小技巧
    • css重置样式表
    • CSS之animation动画
    • 隐藏元素的方法
    • 项目上使用微标
    • CSS之transition过渡
    • flex布局案例-基础
    • flex布局案例-骰子
    • css模块化实现教程
    • Flex布局教程语法篇
    • 粘性定位sticky的用途
    • flex布局案例-圣杯布局
    • flex布局案例-网格布局
    • css实现滚动视觉差效果
    • flex布局案例-输入框布局
    • 伪元素表单控件默认样式重置
    • 水平垂直居中的几种方式-案例
    • 从box-sizing属性入手,了解盒子模型
    • 文字在一行或多行时超出显示省略号
    • 如何根据系统主题自动响应CSS深色模式
    • 「css技巧」使用hover和attr()定制悬浮提示-demo
    • 「布局技巧」图片未加载前自动撑开元素高度
  • Npm

  • Vue

  • HTML

  • Node

  • Yaml

  • React

  • 框架

  • 规范

  • Electron

  • JS演示

  • VuePress

  • JavaScript

  • TypeScript

  • 微信小程序

  • TypeScript-axios

  • 前端
  • CSS
wangmings
2022-07-19
目录

css各种布局

# css各种布局

# 圣杯布局

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title>圣杯布局</title>

		<style type="text/css">
			.grail {
				background-color: #ffffff;
				font-size: 14px;
			}
			
			#hd,
			#ft {
				padding: 20px 3px;
				background-color: #03a9f4;
				text-align: center;
                color: #ffffff;
			}
			
			.bd-lft,
			.bd-rgt,
			.bd-3-lr,
			.bd-3-ll,
			.bd-3-rr {
				margin: 10px 0;
				min-width: 400px;
			}
			
		  .grail	.main {
				background-color: #03a9f4;
				color: #ffffff;
			}
			
			.aside,
			.aside-1,
			.aside-2 {
				background-color: #00bcd4;
				color: #ffffff;
			}
			
			.grail p {
				margin: 0;
				padding: 20px;
				text-align: center;
			}
			/* 左侧栏固定宽度,右侧自适应 */
			
			.bd-lft {
				zoom: 1;
				overflow: hidden;
				padding-left: 210px;
			}
			
			.bd-lft .aside {
				float: left;
				width: 200px;
				margin-left: -100%;
				/*= -100%*/
				position: relative;
				left: -210px;
				/* = -parantNode.paddingLeft */
				_left: 0;
				/*IE6 hack*/
			}
			
			.bd-lft .main {
				float: left;
				width: 100%;
			}
			/* 右侧栏固定宽度,左侧自适应 */
			
			.bd-rgt {
				zoom: 1;
				overflow: hidden;
				padding-right: 210px;
			}
			
			.bd-rgt .aside {
				float: left;
				width: 200px;
				margin-left: -200px;
				/* = -this.width */
				position: relative;
				right: -210px;
				/* = -parantNode.paddingRight */
			}
			
			.bd-rgt .main {
				float: left;
				width: 100%;
			}
			/* 左中右 三栏自适应 */
			
			.bd-3-lr {
				zoom: 1;
				overflow: hidden;
				padding-left: 210px;
				padding-right: 210px;
			}
			
			.bd-3-lr .main {
				float: left;
				width: 100%;
			}
			
			.bd-3-lr .aside-1 {
				float: left;
				width: 200px;
				margin-left: -100%;
				position: relative;
				left: -210px;
				_left: 210px;
				/*IE6 hack*/
			}
			
			.bd-3-lr .aside-2 {
				float: left;
				width: 200px;
				margin-left: -200px;
				position: relative;
				right: -210px;
			}
			/* 都在左边,右侧自适应 */
			
			.bd-3-ll {
				zoom: 1;
				overflow: hidden;
				padding-left: 420px;
			}
			
			.bd-3-ll .main {
				float: left;
				width: 100%;
			}
			
			.bd-3-ll .aside-1 {
				float: left;
				width: 200px;
				margin-left: -100%;
				position: relative;
				left: -420px;
				_left: 0px;
				/*IE6 hack*/
			}
			
			.bd-3-ll .aside-2 {
				float: left;
				width: 200px;
				margin-left: -100%;
				position: relative;
				left: -210px;
				_left: 210px;
				/*IE6 hack*/
			}
			/* 都在右边,左侧自适应 */
			
			.bd-3-rr {
				zoom: 1;
				overflow: hidden;
				padding-right: 420px;
			}
			
			.bd-3-rr .main {
				float: left;
				width: 100%;
			}
			
			.bd-3-rr .aside-1 {
				float: left;
				width: 200px;
				margin-left: -200px;
				position: relative;
				right: -210px;
			}
			
			.bd-3-rr .aside-2 {
				float: left;
				width: 200px;
				margin-left: -200px;
				position: relative;
				right: -420px;
			}
		</style>

	</head>

	<body>
    <div  class="grail">
      <div id="hd">头部</div>

      <div class="bd-lft">
        <div class="main">
          <p>主内容栏自适应宽度</p>
        </div>

        <div class="aside">
          <p>侧边栏固定宽度</p>
        </div>
      </div>

      <div class="bd-rgt">
        <div class="main">
          <p>主内容栏自适应宽度</p>
        </div>

        <div class="aside">
          <p>侧边栏固定宽度</p>
        </div>
      </div>

      <div class="bd-3-lr">
        <div class="main">
          <p>主内容栏自适应宽度</p>
        </div>

        <div class="aside-1">
          <p>侧边栏1固定宽度</p>
        </div>

        <div class="aside-2">
          <p>侧边栏2固定宽度</p>
        </div>
      </div>

      <div class="bd-3-ll">
        <div class="main">
          <p>主内容栏自适应宽度</p>
        </div>

        <div class="aside-1">
          <p>侧边栏1固定宽度</p>
        </div>

        <div class="aside-2">
          <p>侧边栏2固定宽度</p>
        </div>
      </div>

      <div class="bd-3-rr">
        <div class="main">
          <p>主内容栏自适应宽度</p>
        </div>

        <div class="aside-1">
          <p>侧边栏1固定宽度</p>
        </div>

        <div class="aside-2">
          <p>侧边栏2固定宽度</p>
        </div>
      </div>
      <div id="ft">底部</div>
    </div>
	</body>
</html>
<script>
</script>
<style>
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

# 单列布局

# 1.1 水平居中

父元素 text-align:center; 子元素:inline-block;

  • 优点:兼容性好;
  • 不足:需要同时设置子元素和父元素
<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  width: 500px;
  height: 200px;
  text-align: center;
  border:1px solid #ccc;
  background-color: #fff;
}
.child {
  display: inline-block;
  width: 300px;
  height: 100px;
  background-color: red;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 子元素 margin:0 auto;

  • 优点:兼容性好
  • 缺点:需要指定宽度
<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  width: 500px;
  height: 400px;
  background: red;
}
.child {
  margin: 0 auto;
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11

# 父元素:relative;子元素:absolute;left:50%;margin-left:-宽度的一半

  • 优点:兼容性好
  • 缺点:需要知道子元素的宽度
<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  position: relative;
  top: 0;
  left: 0;
  width: 500px;
  height: 400px;
  background: red;
}
.child {
  position: absolute;
  top: 0;
  left: 50%;
  margin-left: -150px;
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 父元素:relative;子元素:absolute;left:50%;transform:translate(-50%,0);

  • 优点:不需要知道子元素的宽度
  • 缺点:兼容性差 (新时代的你们,现在新的浏览器支持了,放心用了)
<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  position: relative;
  top: 0;
  left: 0;
  width: 500px;
  height: 400px;
  background: red;
}
.child {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, 0);
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 弹性盒子:父元素:display:flex;justify-content:center;

  • 优点:简单
  • 缺点:兼容性差 (新时代的你们,现在新的浏览器支持了,放心用了)
<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  display: flex;
  justify-content: center;
  width: 500px;
  height: 400px;
  background: red;
}
.child {
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12

# 1.2 垂直居中

# vertical-align:center;

<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  width: 500px;
  height: 400px;
  background: red;
  display: table-cell;
  vertical-align: middle;
}
.child {
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12

# 父元素:position:relative;子元素:positon:absolute;top:50%;transform:translate(0,-50%);

<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  position: relative;
  top: 0;
  left: 0;
  width: 500px;
  height: 400px;
  background: red;
}
.child {
  position: absolute;
  top: 50%;
  left: 0;
  transform: translate(0, -50%);
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 父元素:position:relative;子元素:positon:absolute;top:50%;margin-top:-子元素高度的一半;

<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  position: relative;
  top: 0;
  left: 0;
  width: 500px;
  height: 400px;
  background: red;
}
.child {
  position: absolute;
  top: 50%;
  left: 0;
  margin-top: -50px;
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 父元素:display:flex;align-items:center;

<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  width: 500px;
  height: 400px;
  background: red;
  display: flex;
  align-items: center;
}
.child {
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12

# 1.3 水平垂直居中

# 父元素:display:table-cell;vertical-align:middle;text-align:center;子元素;display:inline-block;

<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  width: 500px;
  height: 400px;
  background: red;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
.child {
  width: 300px;
  height: 100px;
  background: blue;
  display: inline-block;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 父元素:position:relative;子元素:position:absolute?50%;left:50%;margin-left:宽度的一半;margin-top:高度的一半;或者 transform:translate(-50%,-50%);

<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  width: 500px;
  height: 400px;
  background: red;
  position: relative;
  left: 0;
  right: 0;
}
.child {
  width: 300px;
  height: 100px;
  background: blue;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 父元素{display:flex;justify-content:center;align-items:center;}

<div class="parent">
  <div class="child"></div>
</div>
1
2
3
.parent {
  width: 500px;
  height: 400px;
  background: red;
  display: flex;
  justify-content: center;
  align-items: center;
}
.child {
  width: 300px;
  height: 100px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 2.多列布局

# 2.1 左侧定宽,右侧自适应 第一种

left{float:left;width:100px;} .right{margin-left:100px;}

<div class="left">left</div>
<div class="right">right</div>
1
2
* {
  margin: 0;
  padding: 0;
}
.left {
  height: 400px;
  width: 300px;
  background: red;
  float: left;
}
.right {
  height: 400px;
  margin-left: 300px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 2.1 左侧定宽,右侧自适应 第二种

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="right-fix">
    <div class="right">
      right
    </div>
  </div>
</div>
1
2
3
4
5
6
7
8
9
10
* {
  margin: 0;
  padding: 0;
}
.left {
  width: 300px;
  height: 400px;
  float: left;
  background: red;
}
.right-fix {
  width: 100%;
  margin-left: -300px;
  float: right;
}
.right {
  margin-left: 300px;
  height: 400px;
  background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

.left{width:宽度值;float:left;} .right{overflow:hidden;}

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
/*设置overflow:hidden;创建BFC。根据BFC特性,BFC不会与float box 重叠。*/

* {
  margin: 0;
  padding: 0;
}
.left {
  width: 300px;
  height: 400px;
  float: left;
  background: red;
}
.right {
  height: 400px;
  background: blue;
  overflow: hidden;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

table 实现

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
* {
  margin: 0;
  padding: 0;
}
.parent {
  display: table;
  table-layout: fixed;
  width: 100%;
}
.left {
  width: 300px;
  height: 400px;
  background: red;
  display: table-cell;
}
.right {
  height: 400px;
  background: blue;
  display: table-cell;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

flex 实现

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
* {
  margin: 0;
  padding: 0;
}
.parent {
  display: flex;
  width: 100%;
}
.left {
  width: 300px;
  height: 400px;
  background: red;
}
.right {
  height: 400px;
  background: blue;
  flex: 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 2.2 右侧定宽左侧自适应

float margin 实现

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
* {
  margin: 0;
  padding: 0;
}
.left {
  width: 100%;
  height: 400px;
  background: red;
  float: left;
  margin-right: -300px;
}
.right {
  height: 400px;
  background: blue;
  width: 300px;
  float: right;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

table 实现

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
* {
  margin: 0;
  padding: 0;
}
.parent {
  width: 100%;
  display: table;
  table-layout: fixed;
}
.left {
  width: 100%;
  height: 400px;
  background: red;
  display: table-cell;
}
.right {
  height: 400px;
  background: blue;
  width: 300px;
  display: table-cell;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

flex 实现

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
* {
  margin: 0;
  padding: 0;
}
.parent {
  width: 100%;
  display: flex;
}
.left {
  flex: 1;
  background: red;
}
.right {
  height: 400px;
  background: blue;
  width: 300px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 2.3 左边两列定宽,右侧自适应

float margin 实现

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="center">
    center
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
9
10
11
* {
  margin: 0;
  padding: 0;
}
.parent {
  width: 100%;
}
.left,
.center {
  background: red;
  float: left;
  width: 300px;
  height: 400px;
}
.center {
  background: yellow;
}
.right {
  height: 400px;
  background: blue;
  margin-left: 600px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

float overflow 实现

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="center">
    center
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
9
10
11
* {
  margin: 0;
  padding: 0;
}
.parent {
  width: 100%;
}
.left,
.center {
  background: red;
  float: left;
  width: 300px;
  height: 400px;
}
.center {
  background: yellow;
}
.right {
  height: 400px;
  background: blue;
  overflow: hidden;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

table 实现

<div class="parent">
  <div class="left">
    left
  </div>
  <div class="center">
    center
  </div>
  <div class="right">
    right
  </div>
</div>
1
2
3
4
5
6
7
8
9
10
11
* {
  margin: 0;
  padding: 0;
}
.parent {
  width: 100%;
  display: table;
  table-layout: fixed;
}
.left,
.center {
  background: red;
  display: table-cell;
  width: 300px;
  height: 400px;
}
.center {
  background: yellow;
}
.right {
  height: 400px;
  background: blue;
  display: table-cell;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
编辑 (opens new window)
viewport详解
a标签小妙用

← viewport详解 a标签小妙用→

最近更新
01
theme-vdoing-blog博客静态编译问题
09-16
02
搜索引擎
07-19
03
友情链接
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式