
图片缩放不变形且内容完整显示不裁剪的方法
如何让图片按照等比例完整显示,既不裁剪也不留白?这是个常见的问题,尤其是当图片比例是固定的情况下。
使用 %ignore_a_1%bject-fit: contain 会在图片两侧留下空白,而 object-fit: cover 又会导致图片被裁剪。
这里有两种解决方法:
1. 使用 img 标签
`

`
`.image-container {
width: 100%;
padding-top: calc(100% / (16 / 3)); / 16:3 aspect ratio /
position: relative;
overflow: hidden;
}`
Shakker
多功能AI图像生成和编辑平台
103 查看详情
`.image-container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover; / Ensures the image covers the container /
}`
2. 使用背景图
`.image-container {
width: 100%;
padding-top: calc(100% / (16 / 3)); / 16:3 aspect ratio /
background-image: url(‘your-image.jpg’);
background-size: cover; / Ensures the image covers the container /
background-position: center; / Centers the image /
background-repeat: no-repeat; / Prevents the image from repeating /
}`
通过这两种方法的运用,你可以确保图片在不变形的情况下,以完整的比例显示在容器中,从而达到满屏的效果。
以上就是如何让图片等比例完整显示,既不裁剪也不留白?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1154735.html
微信扫一扫
支付宝扫一扫