Mrs. Yanti’s notice on the market board: set white-space and overflow-wrap, then a Range per character shows the spaces merged, the Enters kept, the lines the…
Why it matters
Pick wrong and text shows up differently from what was meant: a price label splits in two, a buyer’s order lines merge into one paragraph, or a price list lined up with spaces falls apart. pre-wrap is the answer for chat, textareas and code; nowrap for short labels.
The key idea
white-space is two decisions: what happens to spaces and Enters, and whether it may wrap
Common mistakes
A short label "breaks" onto a new line when it must stay one line Use white-space: nowrap. Careful: text longer than its container overflows sideways, so keep it for short labels and buttons (or pair it with text-overflow: ellipsis).
A user’s lines merge into one paragraphnormal turns Enters into spaces. Use pre-wrap so Enters and spaces show as typed while long lines still wrap.
A long URL or hash still overflows although white-space is set white-space only governs spaces and wrapping at spaces; it does not break a word with no spaces. You need overflow-wrap: anywhere (or break-word).
Try it in this playground
Pick nowrap, then narrow the paper Spaces still merge, but the text no longer wraps and sticks out of the paper. Mr. Karim’s red tape grows.
Pick pre Zero spaces dropped and every Enter becomes a line, exactly as typed. But no wrapping.
Pick pre-line Enters kept, but double spaces still merge: see the red numbers on the paper and the struck dots in the source.
Type your own text, with Enters and double spaces Every character you type is marked: kept, dropped, or turned into a line.