2017-12-26 19 views
-3

シナリオ1ではCSS改行を作成しようとしていますが、シナリオ2では作成しません。どちらの場合もテキストはデータベースから動的に取得されます、私は先験的に最初の文章の長さを知りません。テキストが1行の場合はCSS改行が1行以上の場合は改行なし

シナリオ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コードを通じてこれを解決していますが、それは遅いです。

編集:これは動的なテキストの長さで機能するはずです。

答えて

0

単に設定文章は最初のシナリオでブロックされたので、同様に2番目のシナリオでは、テキストコンテナの固定の幅を設定する:

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

+0

申し訳ありませんが、ことを言及するのを忘れてしまった私はドン設計時に文章の長さを知ることはできません。投稿を更新しました。私はサーバー側で文を分割し、最初の文を別の範囲に入れることができます。 –

+0

確かに、最初のシナリオでは、各文の後に改行を作成したいのですか?しかし、2番目のシナリオでは、ある文章の後に、ある幅の直後に改行を入れたくありません。私は正しい? –

+0

最初の文全体が1行以下を占める場合は、最初の文の後に改行が必要です。複数の行が必要な場合は、私は休憩したくありません。 –

関連する問題