gpt4 book ai didi

html - 如何在多行 flexbox 布局中指定换行符?

转载 作者:IT老高 更新时间:2023-10-28 11:06:24 25 4
gpt4 key购买 nike

有没有办法在多行flexbox中换行?

例如,在 this CodePen 中的每个第 3 项之后中断.

.container {
background: tomato;
display: flex;
flex-flow: row wrap;
align-content: space-between;
justify-content: space-between;
}
.item {
width: 100px;
height: 100px;
background: gold;
border: 1px solid black;
font-size: 30px;
line-height: 100px;
text-align: center;
margin: 10px;
}
.item:nth-child(3n) {
background: silver;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>

喜欢

.item:nth-child(3n){
/* line-break: after; */
}

最佳答案

最简单、最可靠的解决方案是在正确的位置插入 flex 元素。如果它们足够宽(width: 100%),它们将强制换行。

.container {
background: tomato;
display: flex;
flex-flow: row wrap;
align-content: space-between;
justify-content: space-between;
}
.item {
width: 100px;
background: gold;
height: 100px;
border: 1px solid black;
font-size: 30px;
line-height: 100px;
text-align: center;
margin: 10px
}
.item:nth-child(4n - 1) {
background: silver;
}
.line-break {
width: 100%;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="line-break"></div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="line-break"></div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="line-break"></div>
<div class="item">10</div>
</div>

但这很丑陋而且没有语义。相反,我们可以在 flex 容器中生成伪元素,并使用 order 将它们移动到正确的位置。

.container {
background: tomato;
display: flex;
flex-flow: row wrap;
align-content: space-between;
justify-content: space-between;
}
.item {
width: 100px;
background: gold;
height: 100px;
border: 1px solid black;
font-size: 30px;
line-height: 100px;
text-align: center;
margin: 10px
}
.item:nth-child(3n) {
background: silver;
}
.container::before, .container::after {
content: '';
width: 100%;
order: 1;
}
.item:nth-child(n + 4) {
order: 1;
}
.item:nth-child(n + 7) {
order: 2;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
</div>

但是有一个限制:flex 容器只能有一个 ::before 和一个 ::after 伪元素。这意味着您只能强制换行 2 次。

为了解决这个问题,您可以在 flex 元素中而不是在 flex 容器中生成伪元素。这样你就不会被限制为 2 个。但是那些伪元素不会是 flex 元素,所以它们将无法强制换行。

但幸运的是,CSS Display L3介绍了display: contents (目前仅支持 Firefox 37):

The element itself does not generate any boxes, but its children andpseudo-elements still generate boxes as normal. For the purposes ofbox generation and layout, the element must be treated as if it hadbeen replaced with its children and pseudo-elements in the documenttree.

因此,您可以将 display: contents 应用于 flex 容器的子容器,并将每个容器的内容包装在一个额外的包装器中。然后, flex 元素将是那些额外的包装器和子元素的伪元素。

.container {
background: tomato;
display: flex;
flex-flow: row wrap;
align-content: space-between;
justify-content: space-between;
}
.item {
display: contents;
}
.item > div {
width: 100px;
background: gold;
height: 100px;
border: 1px solid black;
font-size: 30px;
line-height: 100px;
text-align: center;
margin: 10px;
}
.item:nth-child(3n) > div {
background: silver;
}
.item:nth-child(3n)::after {
content: '';
width: 100%;
}
<div class="container">
<div class="item"><div>1</div></div>
<div class="item"><div>2</div></div>
<div class="item"><div>3</div></div>
<div class="item"><div>4</div></div>
<div class="item"><div>5</div></div>
<div class="item"><div>6</div></div>
<div class="item"><div>7</div></div>
<div class="item"><div>8</div></div>
<div class="item"><div>9</div></div>
<div class="item"><div>10</div></div>
</div>

或者,根据 an old version of the spec , Flexbox 允许使用 break-before 强制中断, break-after或他们的旧 CSS 2.1 别名:

.item:nth-child(3n) {
page-break-after: always; /* CSS 2.1 syntax */
break-after: always; /* CSS 3 syntax */
}

但是这些强制换行符只适用于 Firefox,我认为它们不应该按照当前规范工作。新提议的方法(未在任何地方实现)是 wrap-before or wrap-after :

.item:nth-child(3n) {
wrap-after: flex; /* New proposed syntax */
}

.container {
background: tomato;
display: flex;
flex-flow: row wrap;
align-content: space-between;
justify-content: space-between;
}
.item {
width: 100px;
background: gold;
height: 100px;
border: 1px solid black;
font-size: 30px;
line-height: 100px;
text-align: center;
margin: 10px
}
.item:nth-child(3n) {
page-break-after: always;
break-after: always;
wrap-after: flex;
background: silver;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>

关于html - 如何在多行 flexbox 布局中指定换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29732575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com