css - 允许一个 flex 元素换行到多行

标签 css flexbox

我有无法更改的 HTML,类似于以下内容。我需要使用 Flexbox 来重新排序元素(并在最后一项之前强制“换行”)。我怎样才能使大多数 flex 元素被视为原子单元,但特别是那个 - #c 在这个例子中 - 允许与前一个元素在同一行开始并且然后像标准 display:inline 元素一样自动换行?

section {
   display: flex;
   flex-wrap: wrap;
   width: 450px;
   background:orange;
   padding:0.2em;
}
span {
   background:yellow;
   padding:2px 4px;
   border-radius:3px;
   border:1px solid rgba(0,0,0,50%);
   margin:0 3px;
}
#a { order:1 }
#b { order:2 }
#c {
   order:3;
   background:none;
   border:none;
}
section::before {
   content:'';
   order:4;
   width:100%;
}
#d { order:5 }
<section>
<span id="c">with an earthquake, birds and snakes and aeroplanes, and Lenny Bruce is not afraid</span>
<span id="a">That's great</span>
<span id="b">it starts</span>
<span id="d">— R.E.M.</span>
</section>

为了更加清楚,在上面的示例中,我希望“That's great it starts with an earthquake”都在同一行,但文本换行到下一行,最后是“— R.E.M.”被迫到自己的行上。

最佳答案

我认为您只能使用一些 hack 来近似它。可能没有通用的解决方案

section {
   display: flex;
   flex-wrap: wrap;
   width: 450px;
   background:orange;
   padding:0.2em;
}
span {
   background:yellow;
   padding:2px 4px;
   border-radius:3px;
   border:1px solid rgba(0,0,0,50%);
   margin:0 3px;
}
#a { order:1 }
#b { order:2 }
#c {
   order:3;
   background:none;
   border:none;
   /* added this*/
    margin-top: -1.2em;
    text-indent: 148px;
    /**/
}

section::before {
   content:'';
   order:4;
   width:100%;
}
#d { order:5 }
<section>
<span id="c">with an earthquake, birds and snakes and aeroplanes, and Lenny Bruce is not afraid</span>
<span id="a">That's great</span>
<span id="b">it starts</span>
<span id="d">— R.E.M.</span>
</section>

关于css - 允许一个 flex 元素换行到多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59384234/

相关文章:

html - Css nth-child 工作很奇怪

html - DIV 卡在页面左侧

css - 矩阵不等于 css 变换动画中的平移和旋转组合?

css - 如何设置从父元素到子元素的溢出值?

javascript - Bootstrap 模式中的 Google map 。平移/缩放鼠标手势不起作用

javascript - 配对显示: flex with display: block and display: none

javascript - 防止外部组件在顶部添加空白

html - 使用 IE11 时如何使 Flex 元素适合其父项的高度?

css - 使用 Flexbox 时 Material 选项卡无法正确收缩

html - 垂直居中作为 `flex-grow: 1` 子元素的 flex 元素?