<div id="father">
<div id="son">
我是关键节点
div>
div>
已知宽高
#father {
width: 500px;
height: 300px;
background-color: aqua;
position: relative;
}
#son {
width: 100px;
height: 100px;
background-color: aquamarine;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
#father {
width: 500px;
height: 300px;
background-color: aqua;
position: relative;
}
#son {
width: 100px;
height: 100px;
background-color: aquamarine;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
未知宽高
#father {
width: 500px;
height: 300px;
background-color: aqua;
position: relative;
}
#son {
background-color: aquamarine;
position: absolute;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
}
#father {
width: 500px;
height: 300px;
background-color: aqua;
display: flex;
justify-content: center;
align-items: center;
}
#son {
background-color: aquamarine;
}
#father {
width: 500px;
height: 300px;
background-color: aqua;
display: grid;
}
#son {
background-color: aquamarine;
justify-self: center;
align-self: center;
}