Center and crop image with CSS(使用 CSS 居中和裁剪图像)
问题描述
我需要使用 CSS 对图像进行居中和裁剪.我已关注
1
- 当我在 portrait
模式下使用设备时显示
2
- 当我在 landscape
模式下使用设备时
运行时代码:
你可以通过
img {适合对象:封面;}
它与 background-size:cover
的作用相同,但它用于 img
标签而不是背景图像
参考
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:
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
这篇关于使用 CSS 居中和裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 CSS 居中和裁剪图像


- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01