[英]Shrink images to fit in flexbox with wrapping rows
I want an arbitrary number of images to shrink to fit inside a fixed-width div. 我希望将任意数量的图像缩小以适合固定宽度的div。 It seems that flexbox is good for this purpose, but I can't seem to get it to work with wrapping multiple rows.
似乎flexbox可以达到这个目的,但是我似乎无法使它用于包装多行。
If I set flex-flow
to row wrap
and flex
to 1 1 auto
, the images are too big. 如果我将
flex-flow
设置为1 1 auto
row wrap
并将flex
为1 1 auto
,则图像太大。
.container { display: flex; flex-flow: row wrap; border: 1px solid black; width: 300px; height: 300px; } .image { flex: 1 1 auto; } img { max-width: 100%; }
<div class="container"> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> </div>
If I set flex-flow
to just row
or flex to 1 1 0
, the images don't wrap onto multiple lines: 如果我将
flex-flow
设置为row
或flex设置为1 1 0
,则图像不会换行:
.container { display: flex; flex-flow: row wrap; border: 1px solid black; width: 300px; height: 300px; } .image { flex: 1 1 0; } img { max-width: 100%; }
<div class="container"> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> </div>
How can I make the images expand enough to wrap as much as possible to fill the div, but not so much they break out of it? 如何使图像扩展到足以包裹div的程度,但又不能使其破裂呢?
If I set
flex-flow
torow wrap
andflex
to1 1 auto
, the images are too big.如果我将
flex-flow
设置为1 1 auto
row wrap
并将flex
为1 1 auto
,则图像太大。
With flex-basis: auto
your item will take the size of its content (the image, in this case). 使用
flex-basis: auto
您的项目将采用其内容的大小(在这种情况下为图片)。 That's what you're getting in your first demo: The intrinsic width of the image. 那就是您在第一个演示中得到的:图像的固有宽度。
If I set
flex-flow
to justrow
or flex to1 1 0
, the images don't wrap onto multiple lines.如果我将
flex-flow
设置为row
或flex设置为1 1 0
,则图像不会换行。
With flex-basis: 0
your item will ignore content size and distribute all space in the row evenly among items. 使用
flex-basis: 0
您的项目将忽略内容大小,并将行中的所有空间平均分配给项目。 That's what's happening in your second demo: It's creating three equal width items. 这就是第二个演示中正在发生的事情:它正在创建三个等宽的项目。 As a result, there is no reason to wrap.
结果,没有理由包装。
To control the size of flex items you need to define their flex-basis
. 要控制弹性项目的大小,您需要定义其
flex-basis
。 Here's a rough sketch: 这是一个粗略的草图:
.container { display: flex; flex-flow: row wrap; align-content: flex-start; /* pack wrapping lines to the top */ border: 1px solid black; width: 300px; height: 300px; } .image { flex: 0 0 30%; margin: 5px; } img { max-width: 100%; }
<div class="container"> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> <div class="image"> <img src="http://www.placebacon.net/400/300"> </div> </div>
Just for fun. 纯娱乐。
var container1 = document.querySelector('.container1'), container2 = document.querySelector('.container2'), img = container2.querySelectorAll('.container2 img'), width = 50, stop = false; expand(); function expand() { for (var i = 0; i < img.length; i++) { img[i].style.width = width + 'px'; } if (stop) return; if (container2.clientHeight < container1.clientHeight) { width++; setTimeout(function() { expand() }, 10); } else { width--; stop = true; expand(); } }
.container1 { border: 1px solid black; height: 300px; width: 300px; } .container2 { font-size: 0; }
<div class="container1"> <div class="container2"> <img src="http://www.placebacon.net/400/300"> <img src="http://www.placebacon.net/400/300"> <img src="http://www.placebacon.net/400/300"> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.