如果文本是一行,则CSS换行符,但如果超过1行则不会中断

问题描述 投票:-3回答:1

我正在尝试在方案1中生成CSS换行符,但不会在方案2中生成。在这两种情况下,文本都是从数据库中动态获取的,所以我不知道第一句话的先验性。

场景1:

This is the first sentence of a block.
This is the second sentence of the block, which is wide.

场景2:

This is the first sentence of the next example that
breaks. This is the second sentence of the block, which
is wide.

这只能用CSS吗?我目前通过在窗口调整大小上运行的JS代码来解决这个问题,但这很慢。

编辑:澄清这应该适用于动态文本长度。

css responsive-design line-breaks
1个回答
0
投票

只需在第一个场景中设置要阻止的句子,并在第二个场景中为文本容器设置固定宽度,如下所示:

.scenario1 {
  width: 100%;
}

.scenario1 span {
  display: block;
}

.scenario2 {
  width: 400px;
}
<div class="scenario1"><span>This is the first sentence of a block.</span> 
<span>This is the second sentence of the block, which is wide.</span></div>
<br />
<div class="scenario2">This is the first sentence of the next example that breaks. This is the second sentence of the block, which is wide.</div>
© www.soinside.com 2019 - 2024. All rights reserved.