==transform 居中==
.wrap{ //父
position: relative;
border: 1px solid red;
width: 300px;
height: 300px;
}
.child { //子
position: absolute;
width: 100px;
height: 100px;
background-color: green;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
absolute +margin auto
.child {
position: absolute;;
top: 0;
left: 0;
right: 0;
bottom: 0;
absolute + calc
这种方式也要求居中元素的宽高必须固定
.wp {
position: relative;
}
.box {
position: absolute;;
top: calc(50% - 50px); // 50% 为top百分值 50px为 子级 一半 宽都
left: calc(50% - 50px);
absolte + 负margin
/* 定位代码 */
.father {
position: relative;
}
.son {
position: absolute;;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
lineheigth
.father {
line-height: 300px;
text-align: center;
font-size: 0px;
}
.son {
font-size: 16px;
display: inline-block;
vertical-align: middle;
line-height: initial;
text-align: left; /* 修正文字 */
}
flex
.father {
display: flex;
justify-content: center;
align-items: center;
}
grid
.father {
display: grid;
}
.son {
align-self: center;
justify-self: center;
}
==总结==