【问题标题】:Forcing text to span N lines强制文本跨越 N 行
【发布时间】:2017-11-11 01:13:46
【问题描述】:

我有一个宽度可变的 div(响应式)。文本内容由用户动态创建,因此行长可能因内容而异。有没有办法让里面的文本恰好重叠 3 行?理想情况下不使用 javascript 或 php(尽管如果 bootstrap 与 js 解决方案捆绑在一起,我并不强烈反对)

所以

<div>Here is some example text that regardless of the div width is always three lines</div>

将呈现为

Here is some example text that 
regardless of the div width
is always three lines

无论 div 是跨越页面的整个宽度还是仅仅几个 rems。

And
another
example

我不想截断文本或添加省略号。如果文本从 div 溢出,但仍然堆叠成三行,这是可以接受的。如果它有所作为,我正在使用 bootstrap 4 beta。

【问题讨论】:

  • 只需将 div 的高度设置为行高的 3 倍并添加溢出隐藏 - jsfiddle.net/uo30mwev
  • 不幸的是,我认为你不能用 CSS 做到这一点......

标签: html css bootstrap-4


【解决方案1】:

如果你可以像这样在 DIV 上设置一个类:

<div class="UserDynamic">Here is some example text that regardless of the div width is always three lines</div>

然后添加到 CSS:

div.UserDynamic{height:3em}

或者,如果您无法为用户生成的位设置 DIV,则将整个用户区域包含在单个 div 中,如下所示:

<div class="UserDynamics"><!--user content appears in this div-->
    <div>Here is some example text that regardless of the div width is always three lines</div>

    <div>Here is more example text that regardless of the div width is always three lines</div>
</div>

在 CSS 中:

div.UserDynamics div{height:3em}

【讨论】:

  • div 似乎随着文本展开,将所有内容都放在一行上
  • 在 CSS 中添加“width:6em”。这将使其包含大多数单词,尽管较长的单词可能会导致出现水平滚动条,并且您最终可能会在同一行上出现 2 个或更多较短的单词。这是否可以接受取决于您的设计用例!
猜你喜欢
  • 2011-08-18
  • 2016-05-13
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 2011-09-23
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多