简体   繁体   English

Flexbox 画廊带包装

[英]Flexbox Gallery With Wrapping

I am trying to create a responsive image gallery for my website using a flexbox. However, I am trying to make it wrap to the next line and stay in order, which I am having trouble with.我正在尝试使用 flexbox 为我的网站创建一个响应式图片库。但是,我试图让它换行到下一行并保持顺序,我遇到了麻烦。 For example:例如:

Desktop桌面
1,2,3 1,2,3
4,5,6 etc 4,5,6等
Tablet药片
1,2 1,2
3,4 etc 3,4等
Mobile移动的
1 1个
2 etc 2等

Please see the example code I included.请参阅我包含的示例代码。

 .row { display: flex; flex-wrap: wrap; }.column { flex: 33%; max-width: 33%; }.column img { vertical-align: middle; width: 100%; } @media screen and (max-width: 800px) {.column { flex: 50%; max-width: 50%; } } @media screen and (max-width: 600px) {.column { flex: 100%; max-width: 100%; } }
 <div class="row"> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/112.png"> <figcaption>Caption 1</figcaption> </figure> <figure> <img src="https://img.webnots.com/2014/04/42.png"> <figcaption>Caption 4</figcaption> </figure> </div> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/22.png"> <figcaption>Caption 2</figcaption> </figure> <figure> <img src="https://img.webnots.com/2014/04/52.png"> <figcaption>Caption 5</figcaption> </figure> </div> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/32.png"> <figcaption>Caption 3</figcaption> </figure> <figure> <img src="https://img.webnots.com/2014/04/62.png"> <figcaption>Caption 6</figcaption> </figure> </div> </div>

The first thing I would do is nest it in a container .我要做的第一件事是将它嵌套在一个container Flex works in order of the markup so I switched your order back to the correct numerical order. Flex 按标记顺序工作,因此我将您的顺序切换回正确的数字顺序。 Then, what I would do is nest each of your figure 's in their own div.然后,我要做的是将每个figure嵌套在它们自己的 div 中。 I just used your column div to demonstrate.我只是用你的column div 来演示。 Then remove the media queries and let flex do its thing.然后删除media queries并让 flex 做它的事情。

The main issue was you were trying to split them up in rows of 3 but had 2 nested per div, and were trying to set max-width to the div.主要问题是您试图将它们分成 3 行,但每个 div 嵌套了 2 个,并且试图将max-width设置为 div。 To have a row of 3 they should be nested in their own div's with width: 33%;要一行 3,它们应该嵌套在自己的 div 中, width: 33%;

EDIT ~ I made the min-width: 200px;编辑〜我做了min-width: 200px; because your image renders at about 160px.因为您的图像呈现在大约 160 像素处。 200px would be a good breaking point on mobile devices. 200px将是移动设备上的一个很好的突破点。 Check your dev tools for mobile.检查您的移动开发工具。

 .row { display: flex; flex-wrap: wrap; }.column { min-width: 200px; width: 33.33%; }.container { margin: auto; width: 100%; }
 <div class="container"> <div class="row"> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/112.png"> <figcaption>Caption 1</figcaption> </figure> </div> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/42.png"> <figcaption>Caption 2</figcaption> </figure> </div> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/22.png"> <figcaption>Caption 3</figcaption> </figure> </div> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/52.png"> <figcaption>Caption 4</figcaption> </figure> </div> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/32.png"> <figcaption>Caption 5</figcaption> </figure> </div> <div class="column"> <figure> <img src="https://img.webnots.com/2014/04/62.png"> <figcaption>Caption 6</figcaption> </figure> </div> </div> </div>

Your HTML elements are set up incorrectly.您的 HTML 元素设置不正确。 To achieve what you want, you shouldn't put in the same element "first" and "fourth".为了实现你想要的,你不应该把“第一”和“第四”放在同一个元素中。 While it looks good on desktop, you won't be able to put element "second" or "third" between them.虽然它在桌面上看起来不错,但您无法将元素“第二”或“第三”放在它们之间。 You will only be able to change the position of those elements inside of the.您将只能更改 position 里面的那些元素。 Very basic HTML and CSS stuff.非常基本的 HTML 和 CSS 的东西。

I would advise you to rename class "column" to "cell" and put them in order:我建议您将 class“列”重命名为“单元格”并按顺序排列:

 <div class="row"> <div class="cell"> <figure> <img src="https://img.webnots.com/2014/04/112.png"> <figcaption>Caption 1</figcaption> </figure> </div> <div class="cell"> <figure> <img src="https://img.webnots.com/2014/04/112.png"> <figcaption>Caption 2</figcaption> </figure> </div> <div class="cell"> <figure> <img src="https://img.webnots.com/2014/04/112.png"> <figcaption>Caption 3</figcaption> </figure> </div> <div class="cell"> <figure> <img src="https://img.webnots.com/2014/04/112.png"> <figcaption>Caption 4</figcaption> </figure> </div> <div class="cell"> <figure> <img src="https://img.webnots.com/2014/04/112.png"> <figcaption>Caption 5</figcaption> </figure> </div> <div class="cell"> <figure> <img src="https://img.webnots.com/2014/04/112.png"> <figcaption>Caption 6</figcaption> </figure> </div> </div>

And of course in your CSS file change the.column to.cell当然,在您的 CSS 文件中,将 .column 更改为 .cell

This way you will achieve your wanted responsive look on tablet and mobile.通过这种方式,您将在平板电脑和移动设备上实现您想要的响应式外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM