方法一、 用disition方法 通过justify
.a {
width: 100px;
height: 100px;
background: red;
display: flex;
justify-content: center;
align-items: center;
}
.b {
width: 50px;
height: 50px;
background: pink;
}
方法二、
.a {
width: 100px;
height: 100px;
background: red;
position: relative;
}
.b {
width: 50px;
height: 50px;
background: pink;
position: absolute;
top: 50%;
left: 50%;
margin-top: -25;
margin-left: -25px;
}
方法三、
.a {
width: 100px;
height: 100px;
background: red;
position: relative;
}
.b {
width: 50px;
height: 50px;
background: pink;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
方法四、css3新属性设置垂直居中。margin:auto设置水平居中
.a {
width: 100px;
height: 100px;
background: red;
display: table-cell;
vertical-align: middle;
}
.b {
width: 50px;
height: 50px;
background: pink;
margin: auto;
}
方法五、
.a {
width: 100px;
height: 100px;
background: red;
position: relative;
}
.b {
width: 50px;
height: 50px;
background: pink;
position: absolute;
top: 50%;
left: 50;
transform: translate(-50%, -50%);
}
网友评论