html - 在 CSS FlexBox 布局中自动调整图像大小并保持纵横比?

标签 html css flexbox

我使用了如下所示的 CSS flexbox 布局:

enter image description here

如果屏幕变小,它会变成这样:

enter image description here

问题是图像没有调整大小以保持原始图像的纵横比。

如果屏幕变小,是否可以使用纯 CSS 和 flex box 布局让图像调整大小?

这是我的 html:

<div class="content">
  <div class="row"> 
    <div class="cell">
      <img src="http://i.imgur.com/OUla6mK.jpg"/>
    </div>
    <div class="cell">
      <img src="http://i.imgur.com/M16WzMd.jpg"/>
    </div>
  </div>
</div>

我的 CSS:

.content {
    background-color: yellow;    
}

.row {
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;

    -webkit-box-orient: horizontal; 
    -moz-box-orient: horizontal;
    box-orient: horizontal;
    flex-direction: row;

    -webkit-box-pack: center;
    -moz-box-pack: center;
    box-pack: center;
    justify-content: center;

    -webkit-box-align: center;
    -moz-box-align: center;
    box-align: center;  
    align-items: center;

    background-color: red;

}

.cell {
    -webkit-box-flex: 1;
    -moz-box-flex: 1;
    box-flex: 1;
    -webkit-flex: 1 1 auto;
    flex: 1 1 auto; 

    padding: 10px;
    margin: 10px;

    background-color: green;
    border: 1px solid red;
    text-align: center;

}

最佳答案

img {max-width:100%;} 是一种方法。只需将它添加到您的 CSS 代码中即可。

http://jsfiddle.net/89dtxt6s/

关于html - 在 CSS FlexBox 布局中自动调整图像大小并保持纵横比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21103622/

相关文章:

CSS - flex 、最大宽度

javascript - 表单提交时的多个输入值

python - 我可以修改漂亮的 soup 标签中的文本而不将其转换为字符串吗?

javascript - 修复导航栏子菜单离开屏幕

html - 如何删除内联/内联 block 元素之间的空间?

css - React Native中的Flexbox : How to align 2 items at the center of the screen?

javascript - 在顶部弹出 Div?

html - 我怎样才能适应 iframe 容器全高

javascript - 带有 bootstrap 的 Smoothie 图表

css - 伪类(第 n 个 child 或第一个 child )不起作用