Center and crop image with CSS

不打扰是莪最后的温柔 提交于 2019-12-23 02:29:12

问题


I need to Center and crop image with CSS. I have followed this article.But device UI output is somewhat different. Can you explain the behavior of this?

This is the use case:

We don’t want to actually crop - just display the middle of the image. Some of the docs people will upload will be docs so don’t want this to be stretched.

My question is I don't know why it transforms (1 image) landscape mode even though I got the image using portrait mode? Any explanation?

photo {
    .photo {
        position: relative;
        width: 100%;
        height: 200px;
        overflow: hidden;
        img {
            position: absolute;
            left: 50%;
            top: 50%;
            height: 100%;
            width: auto;
            -webkit-transform: translate(-50%, -50%);
            -ms-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
        }
        img.portrait {
            width: 100%;
            height: auto;
          }
    }
}
<div class="photo">
  <img [src]="data?.url class="portrait">
</div>

UI:

1 - It shows when I used the device in portrait mode

2 - when I used device in landscape mode

Runtime code:


回答1:


You can achieve it by

img {
    object-fit: cover;
}

It works the same as background-size: cover but it's used for img tags instead of background images

Reference



来源:https://stackoverflow.com/questions/47049861/center-and-crop-image-with-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!