<img>等标签在指定容器中水平垂直居中的几种实现方法

来源:互联网 发布:ubuntu系统安装iso 编辑:程序博客网 时间:2024/04/26 21:58

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;
  }

以上为自己总结,如有错误还望指正;还有更多方法多多联系。

原创粉丝点击