White-space - Property CSS
Summary of characteristics of the white-space property
normal | pre | pre-wrap | pre-line | nowrap | break-spacesnormalDiscrète: during an animation, the white-space property passes from one value to another without transition.Syntax diagram of white-space.
white-space propertyThe 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.
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:
white-space-collapse: allows or does not allow the removal of multiple spaces.
white-space-trim: allows or does not allow the removal of spaces at the beginning or end of the text.
text-wrap-mode: allows or not automatic line breaks.
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) |
Values for white-space.
The following examples are all based on text written like this in the source code:
div
This 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-wrapvalue, successive spaces are preserved, and automatic line breaks are made, butbreak-spacesallows 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.
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.
Browsers compatibility.
white-space property in its original syntax (up to and including version 3).display property as a summary property.Notes:
(1) Accepts values only white-space-collapse and text-wrap-mode, not white-space-trim.
white-spaceproperty
values
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
This first level of the CSS specification describes the mechanism of cascading style sheets. Authors and readers can add styles to documents. Mainly intended for formatting HTML documents, CSS allows defining colors, fonts, text formatting, spacing (margins), etc. The CSS language is easy to understand, readable, and easy to write; it uses common terms from desktop publishing.
Regardingwhite-space. First definition of thewhite-spaceproperty, accepting the valuesnormal,preandnowrap.November 17, 1995Working Draft.November 12, 1996Proposed for recommendation.December 17, 1996Recommendation .September 13, 2018Old specification, declared obsolete. -
Cascading Style Sheets Level 2 - Revision 1 Specification
This level 2 of the specification describes the mechanism of cascading styles in CSS. This language is used to format HTML or XML documents. It supports media-specific style sheets so that authors can adapt the presentation of their documents for visual browsers, auditory devices, printers, braille devices, portable devices, etc.
Regardingwhite-space. Adding new values for thewhite-spaceproperty:pre-wrap,pre-lineandinherit.November 04, 1997Working Draft.March 24, 1998Proposed for recommendation.May 11, 1998Recommendation . -
CSS Text Module Level 3
This level 3 specification module defines the typographic controls of CSS, that is to say the rules for transforming source text into formatted text, with line breaks. Many properties control the alignment and justification of text, hyphenation rules, spacing of characters or words, grouping of consecutive spaces, etc.
These rules are often language-dependent, so it is advisable for authors to specify the language in which the text is written (thelangattribute for HTML). It may even be necessary to specify the writing system used, particularly for Korean, Japanese, Mongolian, etc. languages. Otherwise, the typographic formatting will be less precise.Regardingwhite-space. Addingbreak-spacesvalue towhite-spaceproperty value.January 27, 1999Working Draft.December 22, 2020Candidate Recommendation. -
CSS Text Module Level 4
This level 4 of this specification module describes the properties that affect typographic rules: text alignment and justification, word or character spacing, hyphenation rules, etc.
Compared to level 3, several new properties have been added, particularly concerning line breaks, hyphenation, spacing before and/or after the text, etc.Regardingwhite-space. Since this level of the CSS specification,white-spaceis a shorthand property grouping the values ofwhite-space-collapse,text-wrap-mode propertiesandwhite-space-trim.September 22, 2015Working Draft.
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:












text-align).



text-spacing-trim and text-autospace. Groups character spacing information for CJK languages.
















