Rendering Plaintext as HTML maintaining whitespace – without pre
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
HTML collapses ordinary whitespace by default, which means multiple spaces, tabs, and line breaks do not render the way plain text originally looked. If you want to display plain text in HTML without using the pre element, you need to preserve both formatting and safety yourself.
The good news is that modern CSS already solves most of the whitespace problem. The important part is choosing the right white-space mode and making sure the text is escaped before it is inserted into the page.
Use CSS Instead of Manual Space Replacement
The simplest approach is to render the text in a normal container and apply a whitespace-preserving CSS rule:
white-space: pre-wrap preserves sequences of spaces and newline characters while still allowing long lines to wrap. That is often exactly what people want when they say "like plain text, but not inside pre."
If you want to preserve spaces but collapse line breaks differently, other white-space modes may fit better. In most plain-text rendering cases, though, pre-wrap is the best default.
Insert Text Safely
A very common mistake is to use innerHTML with user-provided text. That is dangerous because any HTML-looking content inside the text will be interpreted as markup instead of displayed literally.
Use textContent instead:
This does two important things at once:
- it escapes the content automatically,
- it preserves the original text characters so CSS can render the whitespace correctly.
That means you do not need to replace spaces with or line breaks with br elements just to display plain text faithfully.
When You Might Still Transform Newlines
Sometimes you are rendering text into a component or email template where CSS support is limited. In that case, converting newlines into explicit br elements can still be useful, but it should be done after escaping the text.
For example:
This approach preserves line breaks, but it still does not preserve repeated spaces unless you add CSS such as white-space: pre-wrap or manually convert spaces. That is one reason the CSS-first approach is usually cleaner.
Choosing the Right white-space Value
A quick rule of thumb:
- '
normalcollapses spaces and newlines,' - '
prepreserves everything but disables wrapping,' - '
pre-wrappreserves whitespace and still wraps long lines,' - '
pre-linepreserves line breaks but collapses repeated spaces.'
If your goal is "render plain text naturally in a layout," pre-wrap is typically the best match. It preserves author formatting without forcing horizontal scrolling for long lines.
Common Pitfalls
- Using
innerHTMLfor raw text. That can create security bugs and broken formatting. - Replacing spaces manually with
everywhere. That usually makes wrapping and maintenance worse. - Forgetting tabs. If tab alignment matters, set
tab-sizeexplicitly. - Choosing
prewhen line wrapping is required by the layout. - Converting newlines to
brelements without escaping the original text first.
Summary
- The easiest way to preserve plaintext whitespace without
preiswhite-space: pre-wrap. - Use
textContentto insert raw text safely. - Add
tab-sizeif the source text includes tabs. - Only convert newlines manually when CSS-based rendering is not an option.
- Safe plaintext rendering is a combination of whitespace handling and HTML escaping.
Related reading
- Repeat a string in JavaScript a number of times
- Repeatedly prompt user until resolved using nodeJS async-await
- Replace an image with Ajax on seaside
- Replace carets with HTML superscript markup using Java
- Retrying a failed async/promise function?
- Return multiple values in JavaScript?
- Return value from asynchronous OR synchronous JavaScript request
- Return value from nested callback instead of the parent function
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.