1.flex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.father {
display: flex;
justify-content: center;
align-items: center;
width: 50px;
height:50px;
border:1px solid black;
}
.son{
width: 30px;
height: 30px;
border: 1px solid red;
}

<div class="father">
<div class="son"></div>
</div>

2.position

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.father {
position: relative;
width: 50px;
height: 50px;
border: 1px solid black;
}
.son {
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 30px;
height: 30px;
border: 1px solid red;
}

3.position+transform

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.father {
position: relative;
width: 50px;
height: 50px;
border: 1px solid black;
}
.son {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 30px;
height: 30px;
border: 1px solid red;
}

4.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
25
26
27
28
29
30
31
32
33
<style>
html,
body {
height: 100%;
width: 100%;
margin: 0;
}

.box {
display: table;
height: 100%;
width: 100%;
}

.content {
display: table-cell;
vertical-align: middle;
text-align: center;
}

.inner {
background-color: #000;
display: inline-block;
width: 200px;
height: 200px;
}
</style>

<div class="box">
<div class="content">
<div class="inner"></div>
</div>
</div>