简体   繁体   English

在最后一个flexbox行中对齐元素

[英]Aligning elements in last flexbox row

I know this question has been asked several times here, but unfortunately, none of the solutions really works and maybe there's a better way of achieving what I need in the meanwhile. 我知道这个问题在这里已经问过几次了,但是不幸的是,没有一个解决方案真正起作用,并且也许有一种更好的方式可以同时满足我的需求。

So, given the following code, you will see that the first row fits 6 elements and the second row fits 2. 因此,给出以下代码,您将看到第一行适合6个元素,第二行适合2个元素。

 .thumbnails { display: flex; margin: 0; padding: 0; list-style-type: none; flex-flow: row wrap; width: 640px; height: 400px; justify-content: space-between; } .thumbnail { width: 100px; height: 100px; background: #ccc; border: 1px solid #000; } 
 <ul class="thumbnails"> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> </ul> 

jsFiddle 的jsfiddle

What I would like to achieve is have the elements of the first row fill the space as in the code, but the elements in the second row should line up based on the first line. 我想实现的是让第一行的元素填充代码中的空间,但是第二行的元素应基于第一行排列。

Using an after pseudo-element with flex: auto like in the following code will screw up the spacing between the two elements in the last row. 在下面的代码中使用带有flex: auto的after伪元素会破坏最后一行中两个元素之间的间距。

 .thumbnails { display: flex; margin: 0; padding: 0; list-style-type: none; flex-flow: row wrap; width: 640px; height: 400px; justify-content: space-between; } .thumbnails::after { content: ""; flex: auto; } .thumbnail { width: 100px; height: 100px; background: #ccc; border: 1px solid #000; } 
 <ul class="thumbnails"> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> </ul> 

jsFiddle 的jsfiddle

So does flex-grow: 1 : flex-grow: 1

 .thumbnails { display: flex; margin: 0; padding: 0; list-style-type: none; flex-flow: row wrap; width: 640px; height: 400px; justify-content: space-between; } .thumbnails::after { content: ""; flex-grow: 1; } .thumbnail { width: 100px; height: 100px; background: #ccc; border: 1px solid #000; } 
 <ul class="thumbnails"> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> </ul> 

jsFiddle 的jsfiddle

And so does margin-right: auto : margin-right: auto

 .thumbnails { display: flex; margin: 0; padding: 0; list-style-type: none; flex-flow: row wrap; width: 640px; height: 400px; justify-content: space-between; } .thumbnails::after { content: ""; margin-right: auto; } .thumbnail { width: 100px; height: 100px; background: #ccc; border: 1px solid #000; } 
 <ul class="thumbnails"> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> <li class="thumbnail"></li> </ul> 

jsFiddle 的jsfiddle

Is there any other way I can achieve what I need than to use dummy elements or fixed margins between the items? 除了在项目之间使用伪元素或固定边距之外,还有什么其他方法可以实现我所需的功能?

I would like to remain flexible because I don't know how many items will be available and what size they have. 我想保持灵活性,因为我不知道有多少可用的物品以及它们的大小。

It appears you've covered most, if not all, methods for last-row alignment available in current flexbox. 看来您已经介绍了当前Flexbox中可用的大多数(如果不是全部)最后一行对齐方法。

Hopefully, a future iteration of the spec will include alignment properties such as last-row , last-column , only-child-in-a-row , etc. 希望规范的未来迭代将包括对齐属性,例如last-rowlast-columnlast-row only-child-in-a-row等。

In the meanwhile, we need to hack it with the methods you've listed. 同时,我们需要使用您列出的方法对其进行破解。

There are also these options to consider: (The second option is mostly FYI, as most browsers haven't completed implementation.) 还需要考虑以下选项:(第二个选项主要是FYI,因为大多数浏览器尚未完成实现。)

  • Desandro Masonry 德桑德罗·石工

    Masonry is a JavaScript grid layout library. Masonry是一个JavaScript网格布局库。 It works by placing elements in optimal position based on available vertical space, sort of like a mason fitting stones in a wall. 它通过根据可用的垂直空间将元素放置在最佳位置来工作,就像砌墙的石匠一样。

    source: http://masonry.desandro.com/ 资料来源: http : //masonry.desandro.com/

  • CSS Grid Layout Module Level 1 CSS网格布局模块级别1

    This CSS module defines a two-dimensional grid-based layout system, optimized for user interface design. 这个CSS模块定义了一个基于二维网格的布局系统,该系统针对用户界面设计进行了优化。 In the grid layout model, the children of a grid container can be positioned into arbitrary slots in a predefined flexible or fixed-size layout grid. 在网格布局模型中,可以将网格容器的子级放置在预定义的灵活或固定大小的布局网格中的任意插槽中。

    source: https://drafts.csswg.org/css-grid/ 来源: https : //drafts.c​​sswg.org/css-grid/

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

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