[英]Wrap items for last row in flex layout with space between alignment
获得所需内容的最快方法是在最后一个可见元素之后添加一个空元素:
<!-- 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>
如果您不使用flexbox可以,请display: grid
更符合您要查找的内容,您可以在其中更严格地定义网格大小:
display: grid;
grid-template-columns: repeat(4, 25%);
grid-gap: /* gap between your items */
您可以使用“填充”元素。 列表末尾需要3个不可见的元素。 每当您的行中断填充程序时,都有助于保持正确的大小和空间。
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>
我只是在这里猜测您的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>
您可以通过使用简单的边距并根据需要将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.