Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

图片保持比例并最大化显示 #114

Closed
Dream4ever opened this issue Dec 15, 2020 · 0 comments
Closed

图片保持比例并最大化显示 #114

Dream4ever opened this issue Dec 15, 2020 · 0 comments
Labels
CSS The world is beautiful

Comments

@Dream4ever
Copy link
Owner

Dream4ever commented Dec 15, 2020

需求描述

在做一个页面的时候,对一批图片(缩略图)有如下要求:

  • 图片长边的尺寸为 160px
  • 图片保持原始比例显示

解决方案

.img-container>img {
  width: 160px;
  height: 160px;
  object-fit: contain;
}

属性值分析

object-fit 属性可以让图片在父容器中保持原始比例显示。

  • 属性值为 contain 时,图片会保持原始比例显示,同时长边与父元素尺寸相同。
  • 属性值为 none 时,图片会保持原始比例和原始尺寸显示。
  • 属性值为 scale-down 时,图片会保持原始比例显示,同时在属性值 containnone 之间选择最终尺寸更小的那个。

参考资料

@Dream4ever Dream4ever added the CSS The world is beautiful label Dec 15, 2020
@Dream4ever Dream4ever changed the title 正方形容器中的图片保持比例并最大化显示 图片保持比例并最大化显示 Dec 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS The world is beautiful
Projects
None yet
Development

No branches or pull requests

1 participant