html - 调整窗口大小时图像高度不会调整

标签 html css flexbox

我希望图像仅在视口(viewport)大小小于 768 像素时显示。

当小于 768px 时,图像应跨越窗口的整个宽度且不应扭曲(即高度应随宽度按比例变化)。

但是,现在我无法在调整窗口大小时更改高度。

我已经尝试将显示更改为 block 并指定 width:100%height:auto

HTML:

<div class="row col-12" id="mobileimg">
    <img src="images/img.png" alt="">
</div>

CSS:

#mobileimg {
    display:none;
}

@media only screen and (max-width: 768px) {

#mobileimg {
    display:flex;
    margin-top:20px;
}
}

谢谢!

最佳答案

您需要设置图像本身的尺寸。这使得图像响应。试试这个:

#mobileimg {
  display: none;
}

@media screen and (max-width: 768px) {
  #mobileimg {
    display: flex;
    margin-top: 20px;
  }
  img {
    width: 100%;
    height: 100%;
  }
}
<div class="row col-12" id="mobileimg">
  <img src="http://lorempixel.com/400/200/" alt="">
</div>

jsFiddle demo

关于html - 调整窗口大小时图像高度不会调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35400410/

相关文章:

html - CSS,文本装饰 : Overline

javascript - 重置 Bootstrap 模式

html - 在 'input' 元素之前或之后生成 CSS 内容

javascript - 共享 worker 确实共享变量

html - 为什么 Chrome 以两行显示这个简单的 Bootstrap header ?

javascript - 使用 jQuery 更改父元素样式

javascript - 溢出 :auto not working even with height set

html - flex 和溢出 : weird behaviour

javascript - 使用 flex display 时避免挤压元素

css - 水平居中 flex div