【问题标题】:Text not wrapping in CSS Grid文本未在 CSS 网格中换行
【发布时间】:2018-06-10 10:16:53
【问题描述】:

我一直在尝试使用 CSS Grid,并坚持寻找最有效的方法来让文本环绕 <div> 元素,该元素部分位于其他两个 <div/> 元素之上。基本上,如下图所示,我希望红色和蓝色 div 中的文本环绕已部分位于其他两个元素的列和行中的黄色 div。显然,这是一个基于网格的布局,所以我对典型的浮动不感兴趣。如何使用 CSS Grid 来实现这个效果?

这是我的css和html:

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr 1fr;
  grid-gap: 10px;
}

.red {
  background-color: red;
  grid-column: 1 / span 2;
  grid-row: 1 / span 4;
}

.blue {
  background-color: blue;
  grid-column: 3 / span 2;
  grid-row: 1 / span 4;
}

.yellow {
  background-color: yellow;
  grid-column: 2 / span 2;
  grid-row: 2 / 4;
}
<div class="grid">
  <div class="red">According to the Oxford English Dictionary, hello is an alteration of hallo, hollo,[1] which came from Old High German "halâ, holâ, emphatic imperative of halôn, holôn to fetch, used especially in hailing a ferryman."[5] It also connects the development
    of hello to the influence of an earlier form, holla, whose origin is in the French holà (roughly, 'whoa there!', from French là 'there').[6] As in addition to hello, halloo,[7] hallo, hollo, hullo and (rarely) hillo also exist as variants or related
    words, the word can be spelt using any of all five vowels.[8][9][10]</div>
  <div class="blue">The use of hello as a telephone greeting has been credited to Thomas Edison; according to one source, he expressed his surprise with a misheard Hullo.[11] Alexander Graham Bell initially used Ahoy (as used on ships) as a telephone greeting.[12][13]
    However, in 1877, Edison wrote to T.B.A. David, the president of the Central District and Printing Telegraph Company of Pittsburgh: Friend David, I do not think we shall need a call bell as Hello! can be heard 10 to 20 feet away. What you think? Edison
    - P.S. first cost of sender & receiver to manufacture is only $7.00.[14] By 1889, central telephone exchange operators were known as 'hello-girls' because of the association between the greeting and the telephone.[13][15] By 1889, central telephone
    exchange operators were known as 'hello-girls' because of the association between the greeting and the telephone.[13][15] </div>
  <div class="yellow">3</div>
</div>

【问题讨论】:

  • 如果一个网格单元包含在两个网格元素中,这并不意味着其中一个元素的内容具有优先权并在另一个网格元素中产生间隙。它们只会重叠。您正在寻找 CSS exclusions 模块,该模块目前仅在 IE 10+ 和 Edge 中实现,是浮点数的升级。
  • 尽管这项技术仍处于起步阶段(因此几乎没有得到支持),shape-outside 旨在准确解决您的问题以及更多问题
  • @feihcsim, shape-outside 目前与CSS shapes 一起使用,没有迹象表明它会与 CSS 网格兼容,而 CSS 排除很可能会兼容。
  • 我不认为这是一种用于换行文本的 hack,而是一种受网格规范当前潜力限制的合法布局选项。有 css 排除或类似 grid-text-wrap 的东西会打开相当多的设计选项,特别是在杂志/报纸布局中。
  • @Dog,很公平。在网格布局中拥有它肯定是一个很酷的功能。也许在未来的版本中。

标签: html css css-grid


【解决方案1】:

您可以使用exclusions,但这些仅受 Microsoft 的 Internet Explorer 和 Edge 支持,应谨慎使用(目前,我们必须在属性前加上 -ms- 才能使排除功能起作用,但这并不适用总是以成功告终)。简而言之,排除是内联流内容环绕的元素周围的区域。

当一个元素的 wrap-flow 属性的计算值不是 auto 时,它就会成为一个排除项,因此示例可能是 .p { wrap-flow: maximum }wrap-margin 属性设置排除区域周围的边距或偏移量。在您的情况下,您可以在 CSS 中放置 .yellow { wrap-flow: both } 以允许其他 div 中的文本环绕黄色 div。

wrap-flow: both 的示例

链接:Tutorialbrowser supportspecificationstutorial

相关标签:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2017-12-22
    • 2022-12-14
    • 1970-01-01
    • 2012-11-23
    • 2023-03-28
    • 2018-09-18
    相关资源
    最近更新 更多