JSDM

HTML

 
1
<h3>未知宽高的水平垂直居中</h3>
2
<h4>1、display:table-cell</h4>
3
<div class="box box1">
4
  <span>我是垂直<br>居中元素</span>
5
</div>
6
<h4>2、display:flex</h4>
7
<div class="box box2">
8
  <span>我是垂直居中元素</span>
9
</div>
10
11
<h4>3、translate(-50%,-50%)</h4>
12
<div class="box box3">
13
  <span>我是垂直居中元素</span>
14
</div>
15
<h4>4、display:inline-block</h4>
16
<div class="box box4">
17
  <span>我是垂直居中元素</span>
18
</div>
19
<h4>5、margin:auto</h4>
20
<div class="box box5">
21
  <span>我是垂直居中元素</span>
22
</div>
23
<h4>6、display:-webkit-box</h4>
24
<div class="box box6">
25
  <span>我是垂直居中元素</span>
26
</div>

CSS

xxxxxxxxxx
56
 
1
.box{
2
  width:200px; 
3
  height:200px; 
4
  background:red;  
5
}
6
.box1{
7
  text-align:center; 
8
  display:table-cell;
9
  vertical-align:middle;
10
}
11
.box2{
12
  display:flex;
13
  justify-content:center;
14
  align-items:center; 
15
  text-align:center;
16
}
17
.box3{ position:relative;}
18
.box3 span{
19
  position:absolute; 
20
  left:50%; 
21
  top:50%; 
22
  -webkit-transform:translate(-50%,-50%); 
23
  width:100%; 
24
  text-align:center;
25
}
26
.box4{
27
  text-align:center; 
28
  font-size:0;
29
}
30
.box4 span{
31
  vertical-align:middle; 
32
  display:inline-block; 
33
  font-size:16px;
34
}
35
.box4:after{
36
  width:0;
37
  height:100%;
38
  display:inline-block;
39
  vertical-align:middle;
40
}
41
.box4:after{
42
  content:''
43
}
44
.box5{ 
45
  display:flex; 
46
  text-align:center;
47
}
48
.box5 span{
49
  margin:auto;
50
}
51
.box6{
52
  display:-webkit-box;
53
  -webkit-box-pack: center;
54
  -webkit-box-align: center; 
55
  text-align:center;
56
}
? ?
? ?
必须是有效的URL
+ 添加另一个资源

JS

xxxxxxxxxx
1
 
1
必须是有效的URL
+ 添加另一个资源
Close

文件管理 点击文件查看URL

图片

  1. 暂无文件

CSS

  1. 暂无文件

JavaScript

  1. 暂无文件

其他

  1. 暂无文件
拖动文件到上面的区域或者:
加载中 ..................