有什么方法可以将项目包装成行,行之间有间距,最后一行之间没有大间隙吗?
<div fxFlex="row wrap" fxLayoutAlign="space-around">
<my-item *ngFor="let item of items"></my-item>
</div>
实际行为:
答案 0 :(得分:1)
获得所需内容的最快方法是在最后一个可见元素之后添加一个空元素:
<!-- 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 */
答案 1 :(得分:1)
您可以使用“填充”元素。列表末尾需要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>
答案 2 :(得分:1)
我只是在这里猜测您的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>