![65c6569efa794475057792402dac8583.gif](https://i-blog.csdnimg.cn/blog_migrate/1b74631b1bd0ea3af4e7668f722ae93e.gif)
![b39d62db5e5f223f42c3bf1642afdd4f.png](https://i-blog.csdnimg.cn/blog_migrate/5b4e08455bcc33c2666e2298b59908a3.png)
左右布局
将内部的子元素加浮动,父元素清除浮动即可。
代码:
DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 300px;
height: 300px;
background-color: #999;}
.smallOne{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.smallTwo{
width: 100px;
height: 100px;
background-color: red;
float: right;}
.clearfix{
content: "";
display: block;
clear: both;}style>head><body><div class="big clearfix"><div class="smallOne">div><div class="smallTwo">div>div>body>html>
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
效果:
左中右布局
浮动 清除浮动
代码:
DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 300px;
height: 300px;
background-color: #999;}
.smallOne{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.smallTwo{
width: 100px;
height: 100px;
background-color: green;
float: left;}
.smallThree{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.clearfix{
content: "";
display: block;
clear: both;}style>head><body><div class="big clearfix"><div class="smallOne">div><div class="smallTwo">div><div class="smallThree">div>div>body>html>
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
效果:
水平居中
内联元素居中:
text-align: center;
代码:
DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 200px;
height: 100px;
background-color: #999;
text-align: center;}style>head><body><div class="big"><span>我在这里span>div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
效果图:
块级元素居中:
margin:0 auto
代码:
DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 200px;
height: 100px;
background-color: #999;
text-align: center;}
.small{
width: 100px;
height: 20px;
background-color: red;
margin: 0 auto;}style>head><body><div class="big"><div class="small">我在这里div>div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
效果图:
垂直居中
父元素高度确定的单行文本
设置 height = line-height
代码:
DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 200px;
height: 100px;
background-color: #aaa;
line-height: 100px;}style>head><body><div class="big">
这里是文本!div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
效果图:
块级元素垂直居中
利用父元素相对定位,子元素绝对定位,并且处置、水平方向个偏移50%,子元素利用负值偏移自身宽度、长度的一半,这种方式同样适用于水平居中。
代码:
DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
height: 200px;
width: 200px;
background-color: red;
position: relative;}
.small{
height: 50px;
width: 50px;
background-color: black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);}style>head><body><div class="big"><div class="small">div>div>body>html>
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
效果图:
![650bf3468e7d74e52ca5dfe46f91a15d.gif](https://i-blog.csdnimg.cn/blog_migrate/de692c65043474b2e660ce2511cb53f6.gif)
更多精彩请继续关注小编了解哦!
计算机毕业设计(源程序+论文+开题报告+文献综述+翻译+答辩稿)
联系QQ:2932963541进行咨询
![51d5965f1067a107e6770c91f27c8375.png](https://i-blog.csdnimg.cn/blog_migrate/8a92ebf7aa56144d1fe5b741f93b13d6.png)
![26f8616408b0fa6d651e8ae1f38c671b.png](https://i-blog.csdnimg.cn/blog_migrate/d54298e54acd409a05b879dd15971a18.jpeg)
![9083a9014e2e06e17d387521f2dc5e52.png](https://i-blog.csdnimg.cn/blog_migrate/c72b0a3baccc59b0b4c8058d443ee938.png)