[英]Wrap items for last row in flex layout with space between alignment
Is there any way to have wraped items in rows with space between alignment, where last row dont have big gap? 有什么方法可以将项目包装在行之间并在对齐之间留有间隔,最后一行没有大的间隙吗?
<div fxFlex="row wrap" fxLayoutAlign="space-around">
<my-item *ngFor="let item of items"></my-item>
</div>
Actual behaviour: 实际行为:
I need same "space-between" in last row compared to other rows. 与其他行相比,我需要在最后一行中使用相同的“间隔”。
The quickest way to get what you're looking for is to add an empty element after the last visible element: 获得所需内容的最快方法是在最后一个可见元素之后添加一个空元素:
<!-- your last 3 boxes -->
<div class="gray-box">
(your content)
</div>
<div class="gray-box">
(your content)
</div>
<div class="gray-box">
(your content)
</div>
<!-- an empty box - make sure .transparent has opacity: 0-->
<div class="gray-box transparent"></div>
If you're okay with not using flexbox, display: grid
is more along the lines of what you're looking for, where you can define grid sizes more strictly: 如果您不使用flexbox可以,请
display: grid
更符合您要查找的内容,您可以在其中更严格地定义网格大小:
display: grid;
grid-template-columns: repeat(4, 25%);
grid-gap: /* gap between your items */
You can use "filler" elements. 您可以使用“填充”元素。 You need 3 elements at the end of your list which are no visible.
列表末尾需要3个不可见的元素。 Everytime when your row breaks the filler helping to keep the right sizes and spaces.
每当您的行中断填充程序时,都有助于保持正确的大小和空间。
const addbutton = document.getElementsByClassName('add'); // referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); const addEl = () => { const newli = document.createElement('li'); newli.textContent = 'new flex child'; const pos = document.querySelector('li.filler'); pos.parentNode.insertBefore(newli, pos.previousElementSibling.nextSibling); } addbutton[0].addEventListener('click', addEl);
body { display: flex; } ul { display: flex; flex-flow: row wrap; justify-content: space-between; padding: 0; flex: 1; } ul>li { flex: 0 1 24%; background: #ccc; display: block; height: 40px; margin-bottom: 20px; } ul>li.filler { height: 0; padding: 0; } button { background: #333; color: white; border: none; padding: 5px; flex: 0 0 100px; height: 100px; margin: 10px; }
<button class="add">click here to add childs</button> <ul> <li>lorem</li> <li>lorem</li> <li>lorem</li> <li>lorem</li> <li>lorem</li> <li>lorem</li> <li class="filler"></li> <li class="filler"></li> <li class="filler"></li> </ul>
I'm just guessing on your html and css structure here, but I assume you have something like this: 我只是在这里猜测您的html和css结构,但是我假设您有这样的东西:
.wrapper { width: 1000px; display: flex; flex-wrap: wrap; justify-content: space-between; } .item { background: gray; width: 200px; height: 100px; border: 1px solid black; }
<div class="wrapper"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div>
You can keep the same spacing around each element by using a simple margin and setting the justify-content
attribute to either center
or flex-start
based on your needs. 您可以通过使用简单的边距并根据需要将
justify-content
属性设置为center
或flex-start
来在每个元素周围保持相同的间距。
.wrapper { width: 1000px; display: flex; flex-wrap: wrap; justify-content: flex-start; } .item { background: gray; width: 200px; height: 100px; border: 1px solid black; margin: 0 20px; }
<div class="wrapper"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.