图片居中的几种方法

1. 父元素固定宽高,利用定位及设置子元素 margin 值为自身的一半。

2. 父元素固定宽高,子元素设置 position: absolute,margin:auto 平均分配margin

3. css3 属性 transform。子元素设置 position: absolute; left: 50%; top: 50%;transform: translate(-50%,-50%);即可。

4. 将父元素设置成 display: table, 子元素设置为单元格 display: table-cell。

5. 弹性布局 display: flex。设置 align-items: center; justify-content: center

posted @ 2022-05-30 11:01  RHCHIK  阅读(476)  评论(0编辑  收藏  举报