【问题标题】:Make image responsive maintaining aspect ratio, with padding either side使图像响应保持纵横比,两边都有填充
【发布时间】:2019-04-01 02:40:56
【问题描述】:

这里是工作笔的链接:https://codepen.io/Adam0410/pen/OBqRRN

HTML

<div id="imgWrapper">
  <img src="https://thestoryengine.co/wp-content/uploads/2017/11/The-crossroads-Section-images-02.png">
</div>

CSS

#imgWrapper {
  display: flex;
  justify-content: center;

  width: 100%;

  padding: 0 20px;
}

#imgWrapper img {
  width: 100%;
  max-width: 1660px;
  height: auto;
}

我不希望图像超过其原始宽度(因此是最大宽度),但我希望它缩小到视口的宽度,同时保持其纵横比。目前,高度不随宽度缩放,这是为什么呢?

【问题讨论】:

    标签: html css responsive-design responsive-images


    【解决方案1】:

    为什么你需要 display flex 到 imgWrapper? 将其更改为阻止,它将按预期工作。如果您需要 flex 将其添加到其他包装器。

    #imgWrapper {
      display: block;
    }
    

    【讨论】:

    • 我需要它是 flex 的,这样当它不占据屏幕的整个宽度时,图像就会在桌面上居中
    【解决方案2】:

    如何将vm 单元用于您的#imgWrapper 使其始终响应:

    #imgWrapper {
     display: flex;
     justyfy-content: center;
     vm: 100%;
     padding: 0 20px;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多