简体   繁体   English

不同数量的 div 基于屏幕的最佳实践有多大?

[英]Different number of divs based on how large is the screen best practices?

I am having a problem regarding elements inside a flex box.我遇到了关于 flex 盒内元素的问题。 I am using flex: 1 1 auto with flex-flow: column wrap .我正在使用flex: 1 1 autoflex-flow: column wrap I want to show a number of divs whose size increases along with that of the screen.我想显示一些大小随着屏幕大小而增加的 div。

Using media queries would be so confusing and the code would be so large.使用媒体查询会很混乱,而且代码会很大。 I am searching for a way to achieve it without using media queries because the size of each div in the flex is about 200px each, I would need to make a lot of media queries incrementing from low to high resolutions.我正在寻找一种不使用媒体查询的方法来实现它,因为 flex 中每个 div 的大小约为 200px,我需要进行大量从低分辨率到高分辨率递增的媒体查询。

The min-width property is rather useful here. min-width 属性在这里非常有用。 I put fifteen divs to show the effect on multiple screen sizes.我放了 15 个 div 来显示对多种屏幕尺寸的影响。

 .flex-parent { display: flex; flex-wrap: wrap; /* this makes the divs wrap */ } .flex-child { margin: 3px; background-color: lightblue; min-width: 200px; /*prevents the flex-child from shrinking more than 200px */ height: 50px; flex: auto; /* this auto-adjusts the width */ /* Everything after this is just to align everything to the center */ padding-top: 20px; text-align: center; }
 <div class='flex-parent'> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> <div class='flex-child'>CHILD</div> </div>

Use flex-flow: row wrap .使用flex-flow: row wrap

If you want to show more div elements as the screen gets wider , you'll want to use row for flex-direction , not column .如果您想在屏幕变宽时显示更多div元素,您需要将row用于flex-direction ,而不是column With flex-direction: row , the flex items will be put into each line from left to right, like a line of text.使用flex-direction: row ,弹性项目将从左到右放入每一行,就像一行文本。 And the bigger the screen, the more items will fit.屏幕越大,适合的项目就越多。

If you want your flex items to grow and fill all the available space, use flex: auto .如果您希望您的 flex 项目增长并填充所有可用空间,请使用flex: auto This might mean the items end up with different sizes, because you can have a different number of items in each flex line.这可能意味着项目最终具有不同的尺寸,因为您可以在每个弹性行中拥有不同数量的项目。 If you want all of them to be the same size, you could set something like flex: 200px .如果您希望所有这些都具有相同的大小,您可以设置类似flex: 200px

 .flex-container { display: flex; flex-flow: row wrap; gap: 8px; } .flex-item { flex: auto; padding: 32px; background-color: bisque; }
 <div class="flex-container"> <div class="flex-item">One</div> <div class="flex-item">Two</div> <div class="flex-item">Three</div> <div class="flex-item">Four</div> <div class="flex-item">Five</div> <div class="flex-item">Six</div> <div class="flex-item">Seven</div> <div class="flex-item">Eight</div> <div class="flex-item">Nine</div> <div class="flex-item">Ten</div> <div class="flex-item">Eleven</div> <div class="flex-item">Twelve</div> </div>

If you want to arrange the div elements as columns (top to bottom), then your flex container needs to have a height set on it, for example height: 500px .如果您想将div元素排列为(从上到下),那么您的 flex 容器需要在其上设置一个height ,例如height: 500px This is so that the flex container can calculate how many flex items can fit into each column.这是为了让 flex 容器可以计算每列可以容纳多少 flex 项目。

 .flex-container { display: flex; flex-flow: column wrap; height: 100vh; gap: 8px; } .flex-item { flex: auto; padding: 32px; background-color: bisque; }
 <div class="flex-container"> <div class="flex-item">One</div> <div class="flex-item">Two</div> <div class="flex-item">Three</div> <div class="flex-item">Four</div> <div class="flex-item">Five</div> <div class="flex-item">Six</div> <div class="flex-item">Seven</div> <div class="flex-item">Eight</div> <div class="flex-item">Nine</div> <div class="flex-item">Ten</div> <div class="flex-item">Eleven</div> <div class="flex-item">Twelve</div> </div>

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

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