This question already has answers here:
How to specify line breaks in a multi-line flexbox layout?
(10个答案)
3年前关闭。
有一个flexbox网格。
如何将.new-string及其后续元素转移到新行?
如果要保留标记样式,则必须通过JavaScript插入以下
(10个答案)
3年前关闭。
有一个flexbox网格。
.flex {
display: flex;
flex-wrap: wrap;
border: 2px solid red;
}
.item {
width: 50px;
height: 50px;
margin: 5px;
border: 2px solid blue;
}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item new-string"></div>
</div>
如何将.new-string及其后续元素转移到新行?
最佳答案
如果查看this great answer,您会注意到,唯一的跨浏览器方式(没有2个换行符限制)是插入100%
-width空块(“换行符”)。因此,对于类似的标记,它看起来像
.flex {
display: flex;
flex-wrap: wrap;
border: 2px solid red;
}
.item {
width: 50px;
height: 50px;
margin: 5px;
border: 2px solid blue;
}
.line-break {
width: 100%;
}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="line-break"></div>
<div class="item"></div>
<div class="item"></div>
<div class="line-break"></div>
<div class="item"></div>
<div class="line-break"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
如果要保留标记样式,则必须通过JavaScript插入以下
line-break
块:var items = document.querySelectorAll(".flex > .item.new-string");
for (var i = 0; i < items.length; i++) {
var lineBreak = document.createElement('div');
lineBreak.className = "line-break";
items[i].parentNode.insertBefore(lineBreak, items[i]);
}
.flex {
display: flex;
flex-wrap: wrap;
border: 2px solid red;
}
.item {
width: 50px;
height: 50px;
margin: 5px;
border: 2px solid blue;
}
.line-break {
width: 100%;
}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item new-string"></div>
<div class="item"></div>
<div class="item new-string"></div>
<div class="item new-string"></div>
<div class="item"></div>
<div class="item"></div>
</div>
关于html - Flexbox元素换行到新行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45086899/
10-11 23:33