![]() |
VOOZH | about |
The CSS white-space property is used to control how text and spaces are displayed within an element. It helps manage text wrapping, spacing, and line breaks for better content layout.
Here are detailed examples, along with code, to help you understand each value better:
The normal value is the default setting for the white-space property. It collapses consecutive white spaces into a single space and allows text to wrap as needed. White spaces are collapsed, and text wraps to the next line when necessary
Syntax:
white-space: normal;The nowrap value prevents text from wrapping to the next line, even when it overflows the container. Consecutive white spaces are still collapsed into a single space. It Collapses white spaces and keeps all text on a single line, preventing line breaks.
Syntax:
white-space: nowrap; The pre value preserves all white spaces and line breaks exactly as they appear in the source code. Text will not wrap and will display exactly as written, emulating the behavior of the <pre> HTML tag. It Preserves white spaces and line breaks as they are in the source code without wrapping text.
Syntax:
white-space: pre;The pre-line value collapses multiple spaces into a single space, preserves line breaks, and allows text to wrap within the container as needed. It collapses consecutive spaces into one, preserves line breaks, and wraps text when necessary.
Syntax:
white-space: pre-line;The pre-wrap value preserves all white spaces and line breaks, while also allowing text to wrap within the container as needed. It preserves white spaces and line breaks, and wraps text when necessary.
Syntax:
white-space: pre-wrap;The initial value resets the white-space property to its default value, as specified by CSS. It behaves as if the property was not set, typically white-space: normal. It reverts the white-space property to its default value as defined by CSS specifications.
Syntax:
white-space: initial;The inherit value sets the white-space property to the same value as its parent element. It ensures the child element adopts the white-space behavior of its parent.
Syntax:
white-space: inherit;Here is a brief explanation of each property value for the white-space property:
| Value | Description |
|---|---|
| normal | Default. Sequences of whitespace are collapsed into a single space, and text wraps when necessary. |
| nowrap | Collapses whitespace normal, but the text will never wrap to the next line, continuing on the same line until a <br> tag is encountered. |
| pre | Whitespace is preserved, and text will only wrap on line breaks. This mimics the behavior of the <pre> HTML tag. |
| pre-line | Collapses multiple spaces into a single space, but text will wrap when necessary, respecting newlines in the source code. |
| pre-wrap | Whitespace is preserved, and text will wrap when necessary. |
| break-spaces | Preserves whitespace, wraps text, and allows spaces to wrap to the next line. This is similar to pre-wrap, but with additional support for wrapping spaces. |
| initial | Sets the property to its default value (normal). |
| inherit | Inherits the white-space value from the parent element. |
Note: The white-space property is universally supported across modern browsers, ensuring consistent behavior in handling text and spacing across different platforms. No additional browser-specific workarounds are needed.