我已经使用了CSS弹性框布局,如下所示:

在此处输入图像描述

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

在此处输入图像描述

问题是图像没有调整大小,保持与原始图像的纵横比不变.

Is it possible to use pure CSS and the flex box layout to let the images be resized if the screen gets smaller?

这是我的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相关问答推荐

如何防止SVG图标出现断行?

如何使div按钮链接到另一个网页

输入宽度在表格显示上不起作用FLEX溢出隐藏Firefox

如何在css中将包含多行文本的多个p-tag放在一起(并排)?

禁用与行分开的边框折叠span

对称渐变作为背景

Firefox和Chrome在文字装饰和文字阴影方面的不同优先顺序

轨道上的居中范围滑块拇指(Webkit)

在每个列宽由带有固定值的minmax()函数定义的网格中,CSS如何为列分配空间?

一切停止工作后,添加不透明度(在tailwind )

旧文本淡出,但新文本不会淡入,而是突然出现

Chatbox底部显示新消息,并向上推送旧消息

使用bash从html表格中提取表格

如何 Select 表格';TMS Web Core中的body html元素?

在DIV上应用梯度模糊未如预期工作

使用flex wrap时,如何设置另一个元素的宽度与换行后的元素对齐?

如何让一个 div 始终贴在容器的边缘?