text-align:center
即可 例如margin:0 auto
即可, 如果有absolute参考第七条fit-content
,并且配合margin:
.parent{
width:fit-content;
margin:0 auto;
}
.son{
display: flex;
justify-content: center;
}
.son{
position:absolute;
left:50%;
transform:translate(-50%,0);
}
(position:relative)
.son{
position:absolute;
width:固定;
left:50%;
margin-left:-0.5宽度;
}
(position:relative)
.son{
position:absolute;
width:固定;
left:0;
right:0;
margin:0 auto;
}
line-height
等于父元素高度.parent::after, .son{
display:inline-block;
vertical-align:middle;
}
.parent::after{
content:'';
height:100%;
}
.parent{
display:flex;
align-items: center;
}
(position:relative)
.son{
position:absolute;
top:50%;
transform: translate(0,-50%);
}
(position:relative)
.son{
position:absolute;
top:50%;
height:固定;
margin-top:-0.5高度;
}
(position:relative)
.son{
position:absolute;
height: 固定;
top: 0;
bottom: 0;
margin: auto 0;
}
#container {
position: relative;
}
#center {
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
}
#container {
position: relative;
height:100px;//必须有个高度
}
#center {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;//注意此处的写法
}
#container {
position: relative;
}
#center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#container {//直接在父容器设置即可
height: 100vh;//必须有高度
display: flex;
justify-content: center;
align-items: center;
}
#container {
height: 100vh;//必须有高度
display: grid;
}
#center {
margin: auto;
}