トップページの中の プロパティの中の Wのプロパティ一覧の中の white-space

white-space

  • 最終更新:2009年11月12日 16:56

white-spaceプロパティは、半角スペースやタブなどの空白類文字をどのように扱うか指定します。

対応ブラウザ
  • IE6一部対応
  • IE7一部対応
  • IE8対応
  • Firefox3.5対応
  • Opera10対応
  • Safari4対応
  • Chrome3対応

プロパティの解説

normal|pre|nowrap|pre-wrap|pre-line|inherit
初期値
normal
適用される要素
全ての要素
継承
します
パーセント値
N/A
メディア
visual

値の解説

normal
連続する空白類文字は1つの半角スペースにまとめて、行ボックスの端まで行くと必要の応じて自動的な改行をします。
任意の位置で改行する場合は、br要素などで行います。
pre
連続する空白類文字の削除を禁止して、自動的な改行はしません。
ソース内の改行位置やbr要素が有る場合はその位置で改行されます。
nowrap
normalと同様に連続する空白類文字を1つの半角スペースにまとめますが、自動的な改行はしません。
任意の位置で改行する場合は、ソース内の改行位置やbr要素を使います。
pre-wrap
preと同様に連続する空白類文字の削除を禁止しますが、行ボックスの端まで行くと自動的な改行がされます。
br要素などで任意の位置で改行する事も可能です。
pre-line
normalと同様ですが、ソース内の改行位置でも改行されます。

値にpreまたはnowrapを指定した場合、自動改行が一切行われないため、一行で長いテキストを表示するとボックスからはみ出して表示されます。
このボックスから溢れた領域の処理(スクロールさせるのか消すのか等)はoverflowプロパティで指定します。

尚、IE6, 7はCSS2.1から追加されたpre-wrappre-lineには対応していないようで、normalと同様の表示になります。

white-spaceのサンプル

p {
  margin: 0 0 20px;
  padding: 10px;
  width: 400px;
  overflow: auto;
  border: 1px solid #666;
}

p.sample01 {
  white-space: normal;
  background: #eee;
}

p.sample02 {
  white-space: pre;
  background: #fee;
}

p.sample03 {
  white-space: nowrap;
  background: #efe;
}

p.sample04 {
  white-space: pre-wrap;
  background: #eef;
}

p.sample05 {
  white-space: pre-line;
  background: #eff;
}
<p class="sample01">This<br />
 value prevents user agents from collapsing sequences of white space. 
Lines are only broken at newlines in the source, 
    or at occurrences of "\A" in generated<br /> content.</p>

XHTMLはclass名の数字が違うだけで他は同じです。
このサンプルを今見ているブラウザで表示すると以下のようになります。

サンプルのブラウザ上の表示

normal

This
value prevents user agents from collapsing sequences of white space. Lines are only broken at newlines in the source, or at occurrences of "\A" in generated
content.

pre

This
value prevents user agents from collapsing sequences of white space. Lines are only broken at newlines in the source, or at occurrences of "\A" in generated
content.

nowrap

This
value prevents user agents from collapsing sequences of white space. Lines are only broken at newlines in the source, or at occurrences of "\A" in generated
content.

pre-wrap

This
value prevents user agents from collapsing sequences of white space. Lines are only broken at newlines in the source, or at occurrences of "\A" in generated
content.

pre-line

This
value prevents user agents from collapsing sequences of white space. Lines are only broken at newlines in the source, or at occurrences of "\A" in generated
content.

このページの上部へ