第一种方式:使用flex布局
div{
width: 300px;
height:300px;
border: 1px red solid;
display: flex;
justify-content: center;
align-items: center;
}
第二种方式:使用margin:auto
div{
width: 300px;
height:300px;
border: 1px red solid;
position: relative;
}
img{
position: absolute;
left: 0;
margin: auto;
top: 0;
right: 0;
bottom: 0;
}
第三种方式:明确水平垂直居中的宽高
div{
width: 300px;
height:300px;
border: 1px red solid;
position: relative;
}
img{
margin-left: 140px;
margin-top: 140px;
}
第四种方式:使用table-cell
div{
width: 300px;
height:300px;
border: 1px red solid;
display: table-cell;
vertical-align: middle;
text-align: center;
}
第五种方式:使用transform
div{
width: 300px;
height:300px;
border: 1px red solid;
position: relative;
}
img{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}