This following CSS white-space
property values preserve new lines:
pre
;pre-wrap
;pre-line
;break-spaces
.
To demonstrate this, consider for example, the following:
<div> <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec justo cursus, hendrerit tortor non, consectetur nisl. Ut sollicitudin congue lorem quis egestas. Aenean tincidunt pharetra odio, a lacinia nulla semper quis.</span> </div>
div { background: lightgoldenrodyellow; } span { background: lightpink; }
Based on the white-space
property value specified, this will produce the following output:
white-space : |
Example Output |
---|---|
pre |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec justo cursus, hendrerit tortor non, consectetur nisl. Ut sollicitudin congue lorem quis egestas.
Aenean tincidunt pharetra odio, a lacinia nulla semper quis.
|
pre-wrap |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec justo cursus, hendrerit tortor non, consectetur nisl. Ut sollicitudin congue lorem quis egestas.
Aenean tincidunt pharetra odio, a lacinia nulla semper quis.
|
pre-line |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec justo cursus, hendrerit tortor non, consectetur nisl. Ut sollicitudin congue lorem quis egestas.
Aenean tincidunt pharetra odio, a lacinia nulla semper quis.
|
break-spaces |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec justo cursus, hendrerit tortor non, consectetur nisl. Ut sollicitudin congue lorem quis egestas.
Aenean tincidunt pharetra odio, a lacinia nulla semper quis.
|
From the examples above you can see that:
- New lines are preserved;
- Spaces and tabs are collapsed when
white-space
property value is set topre-line
; - Spaces and tabs are preserved in case of
pre
,pre-wrap
andbreak-spaces
; - Text wraps (except on preserved spaces and tabs) when using
pre-wrap
; - Text wraps (including on preserved spaces and tabs) when (the opportunity exists) using
break-spaces
.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.