html - 在 flex 网格中按比例调整图像大小

标签 html css flexbox

我正在尝试创建 3 列布局,其中中间列的宽度由百分比定​​义,左右列包含图像,当我更改中间列的宽度时我需要按比例缩放。

HTML

<div class="wrapper">
  <img src="http://www.tinaandersonoc.com/wp-content/uploads/2013/01/brick_wallS.jpg" alt="" />

  <div class="dynamic"></div>

  <img src="http://www.tinaandersonoc.com/wp-content/uploads/2013/01/brick_wallS.jpg" alt="" />
</div>

CSS

div.wrapper {
  margin: 20px 0;
  display: box;
  display: -webkit-flex;
  display: flex;
  width: 600px;
}

div.wrapper img {
  max-width: 100%;
  width: auto;
  height: auto;
  box-flex: 1;
}

div.dynamic {
  background-color: red;
  height: 20px;
  width: 80%;
}

我创建此代码笔是为了更好地理解问题 - http://codepen.io/anon/pen/yOJrxJ

我需要在我的 CSS 中添加什么才能让这两个图像按比例缩放(它应该是方形图像,而不是这个“面条​​”)。

最佳答案

如果你想保持比例,图像不应该是 flex-child ,将它们包裹在任何标签中

div.wrapper {
  margin: 20px 0;
  display: box;
  display: -webkit-flex;
  display: flex;
  width: 80%;
  margin:auto;
}
div.wrapper b {/* b or span or div or whatever (b is short to write ) */
  flex: 1;
}
div.wrapper b img {
  width: 100%;
  display: block;
}
div.dynamic {
  background-color: red;
  width: 80%;
}
<div class="wrapper">
  <b><img src="http://www.tinaandersonoc.com/wp-content/uploads/2013/01/brick_wallS.jpg" alt="" /></b>

  <div class="dynamic">image should not be flex-child , wrap them in any tag</div>

  <b><img src="http://www.tinaandersonoc.com/wp-content/uploads/2013/01/brick_wallS.jpg" alt="" /></b>
</div>

关于html - 在 flex 网格中按比例调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35951831/

相关文章:

jquery - 我如何格式化(主要是更改大小)表单中的下拉菜单(选择)?

CSS 定位不适用于 IE 11 和 Firefox

javascript - 使两个文本区域并排共享一个灵活的边框

javascript - PHP:将变量传递给 JavaScript 方法

jquery - 如何将 Django 日期格式更改为 dd/mm/yy?

html - 导航栏列表项未显示且导航栏未固定在顶部

css - 同一行上有 3 个 div,但移动设备将第 3 行移动到新行

html - 哪种 HTML DocType?

html - 接受从 0 到 60 的数字,但不允许前导零

Javascript Marquee 文本被剪裁为大字体