White-space - Property CSS

white-space

Summary of characteristics of the white-space property

Quick description
Defines how multiple spaces and carriage returns are restored.
Status
Standard
Applies to
A tous les éléments contenant du texte.
Usable for
HTML
Predefined values
normal | pre | pre-wrap | pre-line | nowrap | break-spaces
Percentages
Not applicable.
Initial value
normal
Inherited by default
Yes.
Animation type
Discrète : during an animation, the white-space property passes from one value to another without transition.
W3C Module
CSS Text Module
References (W3C)
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: CR (Candidate Recommendation)

Document status: REC (Recommendation)

Document status: DEPR (Old specification, declared obsolete or superseded)

Syntax diagram of white-space.

white-space - Syntax diagramSyntax diagram of the white-space CSS property. See stylescss.free.fr for details. normal normal pre pre nowrap nowrap pre-wrap pre-wrap break-spaces break-spaces pre-line pre-linewhite-space:;white-space:;
Syntax diagram of the white-space property
The links in the diagram provide access to more details

Description of the white-space property.

The white-space property defines how to represent successive spaces and newlines. Traditionally, HTML ignores successive spaces and causes the automatic line breaks of the text between words. This solution allows the author to present the HTML code (indentation) without disturbing the result displayed by the browser. However, this operation can be annoying in some cases: presentation of computer code, vertical alignment of columns, etc.

By default, browsers apply the following rules to the HTML code:

  • Line breaks in HTML are replaced with a space.
  • Tabs are replaced with a space.
  • The successive spaces are replaced by a single space.

As we can see in the examples below, this operation is perfect for classic text but unusable for example to display computer code. The white-space property solves this problem.

Code HTML
Result displayed by the browser
div It is common to insert spaces, line breaks ⇥ or tabs to render ⇥ the code more readable. /div



It is common to insert spaces, line breaks or tabs to render the code more readable.
div // Access control : ⇥ if( pwd != '' ) { ⇥ ⇥ exit(); ⇥ } /div




// Access control : if( pwd != '' ) { exit(); }

New definition of white-space (text module level 4).

A new definition is being studied, but not yet stabilized (see Text CSS Module - Level 4). white-space will become a shorthand property to group the values of the following three properties:

The current values of white-space will not change but will have an equivalent in the detailed properties, according to the table below.

 white-space  white-space-collapse  text-wrap-mode 
normal collapse wrap
pre preserve nowrap
pre-wrap preserve wrap
pre-line preserve-breaks wrap

Here is a summary of the effect of the different values for white-space :

white-space Line Feed in Source Space and tab (2) Automatic Line Wrap (3) Space at the end of the line Other separator at the end of the line
normal Collapse (1) Collapse Allowed Remove Hang (4)
pre Preserved Preserved Forbidden Preserved Preserved
nowrap Preserved (1) Preserved Forbidden Remove Hang (4)
pre-wrap Preserved Preserved Allowed Hang (4) Hang (4)
break-spaces Preserved Preserved Allowed Wrap (5) Wrap (5)
pre-line Preserved Collapsed Allowed Remove Hang (4)
(1) Line breaks are replaced by a space.
(2) These are multiple spaces or tab characters that can be reduced to a single space.
(3) These are the automatic line breaks that occur when the text reaches the right margin (in latin languages).
(4) Characters may spill over into the margin. Since these are most often invisible spaces or characters, this is not a problem.
(5) Spaces or other separator characters can be returned to the beginning of the next line.

Values for white-space.

    The following examples are all based on text written like this in the source code:

    divThis text is written with three spaces in the source code. /div
  • white-space: normal;

    Default. Successive spaces are displayed as single spaces. Automatic line breaks are based on the width of the element (when the line of text is larger than the width of the element). Line breaks in the source code of the document are replaced by a space. The br tags are respected.

    This text is written with three spaces in the source code.
  • white-space: nowrap;

    Successive spaces are displayed as single spaces.
    Automatic line breaks are not performed, and the wraps in the HTML are ignored (The only line break possible is therefore the forced return with a BR tag).

    This means that text can overflow to the right of the element if the lines are longer than the width of the element.

    This text is written with three spaces in the source code.
  • white-space: pre;

    Multiple spaces and line breaks are done as they appear in the HTML code.

    No automatic line breaks are added. The text can therefore overflow to the right of the element. It may also not start against the left edge if there is a tab or spaces at the beginning of the line in the HTML.

    This text is written with three spaces in the source code.
  • white-space: pre-line;

    Successive spaces are displayed as single spaces. Line breaks are done as they appear in the HTML code, or based on the width of the element.

    This text is written with three spaces in the source code.
  • white-space: pre-wrap;

    The successive spaces are preserved. Automatic line breaks are based on the width of the element.

    This text is written with three spaces in the source code.
  • white-space: break-spaces;

    As with the pre-wrap value, successive spaces are preserved, and automatic line breaks are made, but break-spaces allows line breaks between successive spaces: which may causes empty lines (adjust the width of the element).

    This text is written with three spaces in the source code.
  • white-space: initial; (normal) white-space: inherit; white-space: revert; white-space: revertLayer; white-space: unset;

    Common values ​​are presented on these pages: initial, inherit, revert, revert-layer, unset.

Animation of the white-space property.

The animation below goes through all the values in white-space.

This text contains multiple spaces and returns line in the code.

Simulator.

The text below has three spaces between each word and two line break, materialized by the symbol ⮐. It was entered in the HTML code like this:

p This text has been entered ⮐ with three spaces between words ⮐ and two line breaks. /p

To highlight the difference between pre-wrap and break-spaces, adjust gently the width of the element. break-spaces allows the line break between spaces: you will see spaces at the beginning of the next line, which never happens with pre-wrap that doesn't cut than after the sequences of spaces.

white-space :
This text has been entered with three spaces between each word and two line break.

Browsers compatibility.

Column 1
Support for the white-space property in its original syntax (up to and including version 3).
Column 2
Support for the display property as a summary property.

Notes:

(1) Accepts values only white-space-collapse and text-wrap-mode, not white-space-trim.

1
white-space
property
2
New
values
Estimated overall support.
96%
0%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

KaiOS Browser

Opéra Mobile

Chrome

Edge

Opéra

Safari

Safari sur IOS

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Samsung Internet

Firefox

Baidu Browser

QQ Browser

UC Browser pour Androïd

Opéra mini

white-space property historic.

  • Cascading Style Sheets specification - Version 1

    First definition of the white-space property, accepting the values normal, pre and nowrap.
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    Adding new values for the white-space property: pre-wrap, pre-line and inherit.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • CSS Text Module Level 3

    Adding break-spaces value to white-space property value.
    WD
    January 27, 1999
    Working Draft.
    CR
    December 22, 2020
    Candidate Recommendation.
    PR
    REC
  • CSS Text Module Level 4

    Since this level of the CSS specification, white-space is a shorthand property grouping the values of white-space-collapse, text-wrap-mode properties and white-space-trim.
    WD
    September 22, 2015
    Working Draft.
    CR
    PR
    REC

On text management, see also.

The white-space property is described in the CSS Text Module specification, as well as everything related to text handling (alignment, spacing, hyphenation, overflow, etc.). The following properties are also included in this specification module:

Properties:

hanging-punctuation
Langue française
Sets suspended punctuation (characters allowed to overflow into the margin).
hyphenate-character
Langue française
Defines the character that will be used as a hyphenation mark.
hyphenate-limit-chars
Langue française
Sets the minimum number of characters to allow hyphenation of a word.
hyphenate-limit-last
Langue française
Defines whether hyphenation is allowed at the end of a column, page, and so on.
hyphenate-limit-lines
Langue française
Sets the maximum number of successive lines ending in a hyphenation
hyphenate-limit-zone
Langue française
Sets the maximum number of spaces to allow or disallow hyphenation.
hyphens
Langue française
Defines whether words can be hyphenated at the end of a line. The hyphenation of the words gives a more pleasant layout but can be annoying in some cases.
letter-spacing
Sets the letter Spacing.
line-break
Langue française
Defines line breaks for the CJK languages (Chinese, Japanese, Korean).
line-padding
Langue française
Adds a space at the beginning and end of lines.
overflow-wrap
Langue française
Word breaking for CJK languages and a few others.
tab-size
Langue française
Définit la taille des caractères de tabulation.
text-align
Langue française
Specifies how the element's text should be aligned.
text-align-all
Langue française
Defines the alignment of the lines of a text (synonym for text-align).
text-align-last
Langue française
Sets the alignment of the last line of paragraphs.
text-autospace
Langue française
Defines the space between adjacent characters (usable for CJK languages).
text-group-align
Langue française
text-indent
Langue française
text-justify
text-space-collapse
Langue française
Defines how to handle spaces and multiple spaces in text.
text-space-trim
Langue française
Defines whether spaces at the beginning and end of the element's text should be kept or removed.
text-spacing
Langue française
text-spacing-trim
Langue française
Sets the spacing around CJK punctuation characters.
text-transform
Langue française
text-wrap
Langue française
Defines the handling of text wraps.
text-wrap-mode
Defines whether newlines are allowed in a container.
text-wrap-style
Langue française
Defines how line breaks will be made.
white-space-collapse
Langue française
Defines how successive spaces and newlines should be treated.
white-space-trim
Langue française
word-boundary-detection
Langue française
word-boundary-expansion
Langue française
word-break
Langue française
word-space-transform
Langue française
Normalize the spacing characters (CJK writings).
word-spacing
Langue française
word-wrap
Langue française
Word breaking for CJK languages and a few others.
wrap-after
Langue française
Allows or does not allow line breaks after the box.
wrap-before
Langue française
Autorise ou non les sauts de lignes avant la boite.
wrap-inside
Langue française
Allows or does not allow line breaks in the box.