【发布时间】:2017-01-08 03:55:26
【问题描述】:
我有一个静态高度容器。
在里面我有一个div 元素,里面有一个img 元素。
下面有一个p标签,它是动态填充文本的。
当文本较长且p 标签变大时,我希望上面的图像缩小以在容器内腾出空间。
我一直在尝试使用flex 和flex-shrink 来做到这一点。这就是我所拥有的:
http://codepen.io/niper1/pen/yaBxvx
#main {
width: 100px;
height: 200px;
border: 1px solid #c3c3c3;
display: -webkit-flex; /* Safari */
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#main div {
flex-shrink: 3;
}
img {
height: 100%;
width: auto;
max-width: 100%;
}
p {
flex-shrink: 0;
}
<div id="main">
<div style="background-color:lightblue;">
<img src="http://fakeimg.pl/250x100/">
</div>
<p>Test text Test text Test text Test text Test text Test text Test text Test text Test text Test text Test text Test text Test text Test text Test text N</p>
</div>
如您所见,img 标记并未缩小以腾出足够的空间。这可能与flex-box有关还是我必须寻找不同的路线?
【问题讨论】: