1.通过设置相对位置和绝对位置
父元素:position: relative;
img{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
2.通过设置transform
img{
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
3.通过设置display为table-cell
img{
display: table-cell;
text-align: center;
vertical-align: middle;
}
4.通过设置display的flex容器
img{
display: flex;
align-items: center;
justify-content: center;
}
以上为自己总结,如有错误还望指正;还有更多方法多多联系。
本文介绍了四种实现图片居中的CSS方法:使用相对和绝对定位、transform属性、display的table-cell属性及flex容器布局。每种方法都附带了具体的CSS代码示例。
1万+

被折叠的 条评论
为什么被折叠?



