代码之家  ›  专栏  ›  技术社区  ›  now_world

缩放图像以适应屏幕而不更改其纵横比

  •  0
  • now_world  · 技术社区  · 5 年前

    height 应该是 100% (和 width: auto; ). 但如果图像比它的高宽 width 应该是 100% (和 height: auto; ).

    我能用CSS做这个吗?

    如果 高度 宽度 两者都是 而不是像这样挤在一起:

    enter image description here

    如果 宽度 auto

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  0
  •   evan-leigh    5 年前

    你可以用 object-fit: cover ,这样可以确保图像不会丢失其纵横比。也可以使用百分比值,如 width: 70% height: auto 因此图像在调整大小时会缩小