How to center image vertically?

前端 未结 5 1318
南方客
南方客 2021-01-14 12:55

I am looking for a way to center the image vertically, something that is similar to text-align center. text-align center is kinda efficient since y

5条回答
  •  隐瞒了意图╮
    2021-01-14 13:47

    The solution you've got works for old browsers but in the near future (right now has like the 70% of the browser support) you can do this, a much simpler and elegant solution:

    .container img{
      width: 100%;
      height: auto;
    }
    @supports(object-fit: cover){
        .container img{
          height: 100%;
          object-fit: cover;
          object-position: center center;
        }
    }
    

提交回复
热议问题