如何在多行 Flexbox 布局中指定换行符?

2024-05-11

有没有办法在多行弹性框中进行换行?

例如,在每个第三项之后中断这个代码笔 https://codepen.io/asvirskyi/pen/bdbLNz.

.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>

Like

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

最简单、最可靠的解决方案是将柔性物品插入正确的位置。如果它们足够宽(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 and a ::after伪元素。这意味着您只能强制换行 2 次。

为了解决这个问题,您可以在 Flex 项目内而不是在 Flex 容器中生成伪元素。这样您就不会被限制为 2 个。但是这些伪元素不会是弹性项目,因此它们无法强制换行。

但幸运的是,CSS 显示 L3 http://www.w3.org/TR/css-display-3/已经介绍了display: contents http://www.w3.org/TR/css-display-3/#valdef-display-outside-contents(目前仅Firefox 37支持):

该元素本身不生成任何框,但它的子元素和 伪元素仍然照常生成盒子。出于以下目的 框生成和布局,该元素必须被视为已经存在 被文档中的子元素和伪元素替换 树。

所以你可以申请display: contents到弹性容器的子级,并将每个容器的内容包装在另一个包装器中。然后,弹性项目将是那些附加的包装器和子元素的伪元素。

.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>

或者,根据旧版本的规范 https://www.w3.org/TR/2014/WD-css-flexbox-1-20140925/#algo-line-break, Flexbox 允许通过使用强制中断break-before https://developer.mozilla.org/en-US/docs/Web/CSS/break-before, break-after https://developer.mozilla.org/en-US/docs/Web/CSS/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 https://www.w3.org/TR/css-text-4/#wrap-before:

.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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在多行 Flexbox 布局中指定换行符? 的相关文章

  • 手机上的网页滚动条可以隐藏吗?

    我正在尝试在移动设备上隐藏滚动条 一切在桌面上看起来都很好 没有滚动条 但是当我检查某些 Android 设备 ipad 时 我仍然可以看到灰色 细小的拇指 可以从浏览器中隐藏预定义的滚动条 我的代码 在桌面上运行良好 body webki
  • 使用仅 CSS 菜单正确显示第三层子菜单

    我正在开发一个新网站 并且 css 菜单遇到问题 通过示例更容易解释 这是该网站的链接 http www webau net CSFF index asp http www webau net CSFF index asp 在 Home 父
  • CSS 关键帧动画的随机“起点”

    我有一个带有垂直滚动背景图像的框列表 keyframes movie 0 background position 50 5 50 background position 50 95 0 background position 50 5 mo
  • CSS 中“!important”的反义词是什么?

    我正在构建一个 jQuery 插件 它使用 CSS 样式向嵌套 DIV 标签添加颜色和边距 由于我宁愿将插件保留为单个文件 因此我的代码使用 jQuery 将这些颜色和边距作为 CSS 属性直接添加到 DIV 中 css 方法 这些属性的值
  • 使用 Javascript 增加 div 中的数字

    我对 Javascript 很陌生 所以我认为这是一个愚蠢的错误 function upvote var score parseInt document getElementById voteScore innerHTML score sc
  • CSS 中的图像路径支持 CDN

    我正在尝试将我们的图像部署到 CDN 目前 CSS 具有我们网站上图像的相对路径 这些路径需要支持 CDN 图像位置 有人对我如何做到这一点有建议吗 或者是否有人有关于部署到 CDN 的好教程 这就是我最终完成此任务的方式 我用SASS h
  • 旋转后变换比例

    我有个问题 我正在将 div 旋转 45 度 然后我想在新的 y 轴上缩放它http jsfiddle net P37g5 2 http jsfiddle net P37g5 2 y 轴现在不是 45 度吗 我不确定我是否正确理解了这个问题
  • 图像上的文本和背景颜色叠加

    我正在尝试创建一个与图像大小完全匹配的纯色叠加层 并在该叠加层上显示文本 如果可能的话 我想仅使用 HTML 和 CSS 来完成此操作 图像可以是任何大小 并且将调整大小以适合其父容器并居中 像这样的东西 不起作用 HTML div cla
  • iOS 10.3 safari text-align:调整波斯语/阿拉伯语内容的错误

    在更新到 10 3 之前 我的网站在 iPhone 的所有移动浏览器上都能正常运行和查看 从我将 iOS 更新到 10 3 后 我的内容页面遇到了一个问题 内容被包装在 div CSS 属性为text align justify 我的内容在
  • jQuery mouseover 显示隐藏的 div 并显示 div(如果鼠标仍在 div 上)

    我的鼠标悬停和鼠标移出功能有问题 当我将鼠标悬停在链接上时 它会显示隐藏的 div 当我将鼠标移出 div 时 它会隐藏该 div 问题是 如果我将鼠标悬停在链接上 然后将鼠标移动到不在 div 上方的其他位置 div 不会消失 如果我使用
  • 打开一个新的浏览器窗口/iframe 并在 TEXTAREA 中从 HTML 创建新文档?

    我正在尝试使用 HTML5 的新离线功能编写一个 Web 应用程序 在此应用程序中 我希望能够编辑一些 HTML 完整文档 而不是片段
  • 如何在严格模式下设置元素样式属性?

    Given body document getElementsByTagName body 0 iframe document createElement iframe iframe src protocol settings script
  • 图像在 IE8 中不显示

    在我的网站上 http appliedcodingtech com site factory automation photos http appliedcodingtech com site factory automation phot
  • 继承子节点的高度(A内的IMG)

    我的电脑上有一个漂亮的 CSS a 标签 放一个 img 链接内部使图像粘在链接外部 a 块 尽管它仍然可以点击 但看起来很愚蠢 改变 a to display block or float left修复了这个问题 但有一个不良的情况not
  • 如何在 django 表单中设置自定义 HTML 属性?

    我有一个 Django 表单 它是页面的一部分 假设我有一个字段 search input forms CharField u Search word required False 我只能通过模板访问它 form search input
  • Internet Explorer 10,最大 div 大小为 1.533.917 像素

    我需要制作一个非常大的 div 以百万像素为单位 搜索我发现这个线程证明 IE 可以管理最多 10 000 000 px 确定最大可能的 DIV 高度 https stackoverflow com questions 7719273 de
  • SVG 沿圆弧添加文本

    我正在尝试绘制 SVG 径向饼图 如下所述 色卡 https stackoverflow com a 18210763 1395178 现在我尝试将文本与圆弧一起添加到每个切片 我试图展示Text 1具有与 M 和 A 值完全相同的 x y
  • 如何正确编码 mailto 链接?

    我正在生成一些 HTML 并且我想生成 XSS 和数据库内容安全的mailto关联 这里使用的正确编码是什么 这个怎么样 myLiteral Text string Format mailto 0 Content Type text htm
  • HTML 和 CSS 的基本编码标准 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想知道它们是否是像 PSR 01 这样的 HTML 和 CSS 基本编码标准 我尝试谷歌搜索和搜索 但没有找到 我建议看看类似的东西
  • 从 Google Chrome 打印时的页码

    我看过这个答案 page bottom left content counter page counter pages 很多次 但它从来没有为我输出任何内容到页面 即使它应该工作 我尝试过 创造性 的方法来在底部获取页码 但我永远无法让它可

随机推荐