CSS / リファレンス / white-space:空白と改行の扱い

CSSのwhite-spaceは、連続する空白やソースの改行を残すか、ボックスの幅に合わせて自動で折り返すかを指定します。通常の段落にはnormal、インデントや改行を残すログなどにはpre-wrapを使います。改行だけ残すならpre-lineが適しています。

空白と自動折り返しの扱いを選ぶ

初期値はnormalで、親要素から継承されます。ここでいうソースの改行は、HTML内のテキストに含まれる改行文字です。<br>による明示的な改行は、nowrapでも有効です。各値の定義はCSS white-spaceの仕様を参照してください。

値 連続する空白とタブ ソースの改行 自動折り返し
normal まとめる まとめる 許可
nowrap まとめる まとめる なし
pre 保持 保持 なし
pre-wrap 保持 保持 許可
pre-line まとめる 保持 許可
break-spaces 保持し行末でも幅を取る 保持 保持した空白の直後でも可能
.copy { white-space: normal; }
.log { white-space: pre-wrap; }
.code-line { white-space: pre; overflow-x: auto; }

短いボタンラベルを1行にする場合は、そのラベルだけにnowrapを設定し、狭い画面で収まるか確認します。コードなどで元の行の長さを保つには、.code-lineのように表示領域を横にスクロールできるようにします。

同じテキストで6つの値を比べる

以下のコード全体をUTF-8でwhite-space-example.htmlに保存し、ブラウザーで開きます。最初の区画では、AとBの間に空白4個、Bの後に改行、CとDの間にタブが入っています。外部CSSやJavaScriptは不要です。

<!doctype html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CSS white-spaceの比較</title>
  <style>
    body { margin: 1rem; color: #20242b; background: #fff;
      font: 1rem/1.55 system-ui, sans-serif; }
    main { max-width: 58rem; margin-inline: auto; }
    .samples { display: grid; gap: 1rem;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr)); }
    figure { margin: 0; min-width: 0; }
    figcaption { margin-bottom: .35rem; overflow-wrap: anywhere; }
    .sample { width: 22ch; max-width: 100%; overflow: auto;
      font: 1rem/24px ui-monospace, monospace; tab-size: 4;
      background: #edf4f8; outline: 1px solid #60727b; }
    .sample:focus-visible { outline: 3px solid #005fcc; }
    .narrow { width: 4ch; }
    .anywhere { overflow-wrap: anywhere; }
    .normal { white-space: normal; }
    .nowrap { white-space: nowrap; }
    .pre { white-space: pre; }
    .pre-wrap { white-space: pre-wrap; }
    .pre-line { white-space: pre-line; }
    .break-spaces { white-space: break-spaces; }
  </style>
</head>
<body>
  <main>
    <h1>CSS white-spaceの比較</h1>
    <h2>同じソースで6つの値を比較</h2>
    <div class="samples">
      <figure>
        <figcaption id="mode-normal-label"><code>normal</code></figcaption>
        <div id="mode-normal" class="sample normal">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-nowrap-label"><code>nowrap</code></figcaption>
        <div id="mode-nowrap" class="sample nowrap" tabindex="0" role="region" aria-labelledby="mode-nowrap-label">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-pre-label"><code>pre</code></figcaption>
        <div id="mode-pre" class="sample pre" tabindex="0" role="region" aria-labelledby="mode-pre-label">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-pre-wrap-label"><code>pre-wrap</code></figcaption>
        <div id="mode-pre-wrap" class="sample pre-wrap">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-pre-line-label"><code>pre-line</code></figcaption>
        <div id="mode-pre-line" class="sample pre-line">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
      <figure>
        <figcaption id="mode-break-spaces-label"><code>break-spaces</code></figcaption>
        <div id="mode-break-spaces" class="sample break-spaces">A    B
C	D E F G H I J K L M N O P Q</div>
      </figure>
    </div>
    <h2>行末にある6個の空白</h2>
    <div class="samples">
      <figure>
        <figcaption id="end-pre-wrap-label"><code>pre-wrap</code></figcaption>
        <div id="end-pre-wrap" class="sample narrow pre-wrap">AB&#32;&#32;&#32;&#32;&#32;&#32;</div>
      </figure>
      <figure>
        <figcaption id="end-break-spaces-label"><code>break-spaces</code></figcaption>
        <div id="end-break-spaces" class="sample narrow break-spaces">AB&#32;&#32;&#32;&#32;&#32;&#32;</div>
      </figure>
    </div>
    <h2>空白のない長い文字列</h2>
    <div class="samples">
      <figure>
        <figcaption id="token-0-label"><code>overflow-wrap: normal</code></figcaption>
        <div id="token-0" class="sample normal" tabindex="0" role="region" aria-labelledby="token-0-label">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
      </figure>
      <figure>
        <figcaption id="token-1-label"><code>overflow-wrap: anywhere</code></figcaption>
        <div id="token-1" class="sample normal anywhere">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
      </figure>
      <figure>
        <figcaption id="token-2-label"><code>nowrap + anywhere</code></figcaption>
        <div id="token-2" class="sample nowrap anywhere" tabindex="0" role="region" aria-labelledby="token-2-label">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</div>
      </figure>
    </div>
  </main>
</body>
</html>

normalでは空白の幅が縮み、ソースの改行でCが次の行に移ることはありません。nowrapは自動折り返しも無効にします。pre-lineではCの前の改行を残し、空白とタブをまとめます。preとpre-wrapはどちらも空白と改行を残しますが、長い行を折り返すのはpre-wrapです。

行末の空白でpre-wrapとbreak-spacesを比べる

pre-wrapで保持した行末の空白は、行に使える幅の外側にはみ出すことがあります。break-spacesでは、その空白も幅を取り、各空白の後で折り返せます。詳しくはCSSの空白処理規則を参照してください。

2つ目の区画では幅を約4文字分にして、ABの後に空白を6個入れています。&#32;は空白1個を表すHTMLの文字参照です。pre-wrapの箱は1行の高さですが、break-spacesでは行末の空白で2行になります。空白を少しずつ削って何行になるかを確かめ、元に戻します。

値 行数 プレビュー
pre-wrap 1
break-spaces 2

はみ出す長い文字列に対処する

折り返しを許可していても、分割できる箇所がない長い識別子ははみ出すことがあります。3つ目の区画では、overflow-wrap: normalは文字列を1行に保ち、anywhereは必要に応じて途中で折り返します。nowrapの場合はanywhereを加えても折り返しは再開しません。word-wrapはoverflow-wrapの旧名です。単語内の通常の分割規則を変える場合はword-breakを参照してください。

.message {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  min-width: 0;
}

上の.messageのルールをテキストを含む要素に適用します。フレックスやグリッドのアイテムが縮まない場合は、そのアイテムにmin-width: 0を試してください。幅とスクロールははみ出しと長いテキストの解説、旧名の使い方はword-wrapで確認できます。

元のテキストと計算済みスタイルを確認する

開発者ツールで、テキストノードに空白と改行が残っているか確認します。このプロパティは表示を変えるためのもので、エディターが削除した文字は戻せません。次に計算済みのwhite-spaceとoverflow-wrapを調べ、親のnowrapが継承されていないか確認します。

2キーワード構文に対応するブラウザーでは、white-space: preserve wrapはpre-wrapと同じです。preserveはwhite-space-collapse、wrapはtext-wrap-modeの値です。古いブラウザーにも対応するなら、1キーワードの指定から試せます。