white-space

  • Revision slug: CSS/white-space
  • Revision title: white-space
  • Revision id: 357527
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{CSSRef}}

Summary

The white-space CSS property is used to to describe how whitespace inside the element is handled.

  • {{Xref_cssinitial()}} {{cssinitial("white-space")}}
  • Applies to all elements
  • {{Xref_cssinherited()}} yes
  • Media {{Xref_cssvisual()}}
  • {{Xref_csscomputed()}} as specified

Syntax

Formal syntax: {{csssyntax("white-space")}}

Values

normal
Sequences of whitespace are collapsed. Newline characters in the source are handled as other whitespace. Breaks lines as necessary to fill line boxes.
nowrap
Collapses whitespace as for normal, but suppresses line breaks (text wrapping) within text.
pre
Sequences of whitespace are preserved, lines are only broken at newline characters in the source and at <br> elements.
pre-wrap
Sequences of whitespace are preserved. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.
pre-line
Sequences of whitespace are collapsed. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.

The following table summarizes the behavior of various white-space values:

  New lines Spaces and tabs Text wrapping
normal Collapse Collapse Wrap
nowrap Collapse Collapse No wrap
pre Preserve Preserve No wrap
pre-wrap Preserve Preserve Wrap
pre-line Preserve Collapse Wrap

Examples

code { 
  white-space: pre; 
}

Specifications

Specification Status Comment
{{SpecName('CSS3 Text', '#white-space', 'white-space')}} {{Spec2('CSS3 Text')}} Precises the breaking algorithms. Formally makes white-space a shorthand property.
{{SpecName('CSS2.1', 'text.html#white-space-prop', 'white-space')}} {{Spec2('CSS2.1')}} Initial specification.

Browser compatibility

{{CompatibilityTable()}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (normal and nowrap) 1.0 {{CompatGeckoDesktop("1.0")}} 5.5 [*] 4.0 1.0 (85)
pre 1.0 1.0 6.0 4.0 1.0 (85)
pre-wrap 1.0 {{CompatGeckoDesktop("1.0")}} — {{CompatGeckoDesktop("1.9.1")}}{{property_prefix("-moz")}}
{{CompatGeckoDesktop("1.9")}}
8.0 8.0 3.0 (522)
pre-line 1.0 {{CompatGeckoDesktop("1.9.1")}} 8.0 9.5 3.0 (522)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

[*] Internet Explorer 5.5+ supports {{Cssxref("word-wrap")}}: break-word;
The following code allows line breaking inside pre elements:

pre {
  word-wrap: break-word;      /* IE 5.5-7 */
  white-space: -moz-pre-wrap; /* Firefox 1.0-2.0 */
  white-space: pre-wrap;      /* current browsers */
}

See also

  • {{CSS_Reference:Text()}}

Revision Source

<div>
  {{CSSRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code>white-space</code> CSS property is used to to describe how whitespace inside the element is handled.</p>
<ul class="cssprop">
  <li><dfn>{{Xref_cssinitial()}}</dfn> {{cssinitial("white-space")}}</li>
  <li><dfn>Applies to</dfn> all elements</li>
  <li><dfn>{{Xref_cssinherited()}}</dfn> yes</li>
  <li><dfn>Media</dfn> {{Xref_cssvisual()}}</li>
  <li><dfn>{{Xref_csscomputed()}}</dfn> as specified</li>
</ul>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<a href="/en-US/docs/CSS/Value_definition_syntax" title="CSS/Value_definition_syntax">Formal syntax</a>: {{csssyntax("white-space")}}</pre>
<h3 id="Values" name="Values">Values</h3>
<dl>
  <dt>
    <code>normal</code></dt>
  <dd>
    Sequences of whitespace are collapsed. Newline characters in the source are handled as other whitespace. Breaks lines as necessary to fill line boxes.</dd>
  <dt>
    <code>nowrap</code></dt>
  <dd>
    Collapses whitespace as for<code> normal</code>, but suppresses line breaks (text wrapping) within text.</dd>
  <dt>
    <code>pre</code></dt>
  <dd>
    Sequences of whitespace are preserved, lines are only broken at newline characters in the source and at <code>&lt;br&gt;</code> elements.</dd>
  <dt>
    <code>pre-wrap</code></dt>
  <dd>
    Sequences of whitespace are preserved. Lines are broken at newline characters, at <code>&lt;br&gt;</code>, and as necessary to fill line boxes.</dd>
  <dt>
    <code>pre-line</code></dt>
  <dd>
    Sequences of whitespace are collapsed. Lines are broken at newline characters, at <code>&lt;br&gt;</code>, and as necessary to fill line boxes.</dd>
</dl>
<p>The following table summarizes the behavior of various<code> white-space </code>values:</p>
<table class="standard-table">
  <thead>
    <tr>
      <th>&nbsp;</th>
      <th>New lines</th>
      <th>Spaces and tabs</th>
      <th>Text wrapping</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>normal</th>
      <td>Collapse</td>
      <td>Collapse</td>
      <td>Wrap</td>
    </tr>
    <tr>
      <th>nowrap</th>
      <td>Collapse</td>
      <td>Collapse</td>
      <td>No wrap</td>
    </tr>
    <tr>
      <th>pre</th>
      <td>Preserve</td>
      <td>Preserve</td>
      <td>No wrap</td>
    </tr>
    <tr>
      <th>pre-wrap</th>
      <td>Preserve</td>
      <td>Preserve</td>
      <td>Wrap</td>
    </tr>
    <tr>
      <th>pre-line</th>
      <td>Preserve</td>
      <td>Collapse</td>
      <td>Wrap</td>
    </tr>
  </tbody>
</table>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: css">
code { 
  white-space: pre; 
}</pre>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{SpecName('CSS3 Text', '#white-space', 'white-space')}}</td>
      <td>{{Spec2('CSS3 Text')}}</td>
      <td>Precises the breaking algorithms. Formally makes <code>white-space</code> a shorthand property.</td>
    </tr>
    <tr>
      <td>{{SpecName('CSS2.1', 'text.html#white-space-prop', 'white-space')}}</td>
      <td>{{Spec2('CSS2.1')}}</td>
      <td>Initial specification.</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable()}}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support (<code>normal</code> and <code>nowrap</code>)</td>
        <td>1.0</td>
        <td>{{CompatGeckoDesktop("1.0")}}</td>
        <td>5.5 [*]</td>
        <td>4.0</td>
        <td>1.0 (85)</td>
      </tr>
      <tr>
        <td><code>pre</code></td>
        <td>1.0</td>
        <td>1.0</td>
        <td>6.0</td>
        <td>4.0</td>
        <td>1.0 (85)</td>
      </tr>
      <tr>
        <td><code>pre-wrap</code></td>
        <td>1.0</td>
        <td>{{CompatGeckoDesktop("1.0")}} — {{CompatGeckoDesktop("1.9.1")}}{{property_prefix("-moz")}}<br />
          {{CompatGeckoDesktop("1.9")}}</td>
        <td>8.0</td>
        <td>8.0</td>
        <td>3.0 (522)</td>
      </tr>
      <tr>
        <td><code>pre-line</code></td>
        <td>1.0</td>
        <td>{{CompatGeckoDesktop("1.9.1")}}</td>
        <td>8.0</td>
        <td>9.5</td>
        <td>3.0 (522)</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>[*] Internet Explorer 5.5+ supports {{Cssxref("word-wrap")}}<code>: break-word;</code><br />
  The following code allows line breaking inside<code> pre </code>elements:</p>
<pre class="brush: css">
pre {
  word-wrap: break-word;      /* IE 5.5-7 */
  white-space: -moz-pre-wrap; /* Firefox 1.0-2.0 */
  white-space: pre-wrap;      /* current browsers */
}</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>{{CSS_Reference:Text()}}</li>
</ul>
Revert to this revision