上一篇
🎨 CSS图片等比例缩放技巧(2025最新版) 🖼️
img { max-width: 100%; max-height: 100%; height: auto; /* 保持宽高比 */ }
📌 适用于普通图片,自动适应容器且不变形(来源:腾讯云 2025-08-19)。
img { object-fit: contain; /* 完整显示,留白 */ /* 或 */ object-fit: cover; /* 裁剪填充,无留白 */ }
💡 兼容现代浏览器,图片保持比例不拉伸(来源:CSS-Tricks 2025-08更新)。
.container { background-size: cover; /* 裁剪填充 */ background-position: center; background-repeat: no-repeat; }
🖌️ 背景图适配容器,推荐cover
或contain
(来源:MDN 2025-08文档)。
.box { aspect-ratio: 16/9; /* 16:9比例 */ width: 100%; }
🚀 一行代码搞定容器比例,支持图片/视频/卡片(来源:CSDN 2025-08-01)。
.ratio-box { position: relative; padding-top: 56.25%; /* 16:9计算:9/16=0.5625 */ height: 0; } .ratio-box img { position: absolute; top: 0; width: 100%; height: 100%; }
🔍 传统但有效,适合需要兼容旧浏览器的场景(来源:Smashing Magazine 2025-08案例)。
@media (max-width: 768px) { img { aspect-ratio: 1/1; } /* 移动端正方形 */ }
<img src="image.jpg" loading="lazy" alt="示例">
width
和height
,优先用比例属性。srcset
适配不同分辨率。📅 资料来源:CSDN(2025-08-01)、腾讯云(2025-08-19)、MDN(2025-08-24)、CSS-Tricks(2025更新)。
本文由 业务大全 于2025-08-27发表在【云服务器提供商】,文中图片由(业务大全)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://vds.7tqx.com/wenda/745600.html
发表评论