Home >Web Front-end >CSS Tutorial >How to Preserve Spaces and Line Breaks When Rendering HTML Strings?
Question:
In an MVC3 application, a database-retrieved description containing spaces and newlines is rendered without preserving their formatting. How can this issue be resolved to ensure that spaces and linebreaks are displayed as intended?
Answer:
To preserve spaces and linebreaks in HTML, apply the CSS style white-space: pre-wrap; to the container element. This style forces the text to respect whitespace and line breaks, displaying them as they appear in the source.
Example:
<div>
This code would render the text with spaces and linebreaks preserved, as shown below:
This is some text with some extra spacing and a few newlines along with some trailing spaces and five leading spaces thrown in for good measure
The above is the detailed content of How to Preserve Spaces and Line Breaks When Rendering HTML Strings?. For more information, please follow other related articles on the PHP Chinese website!