Overview - Preformatted text
What is it?
Preformatted text in HTML is a way to display text exactly as it is typed, including spaces, line breaks, and tabs. It uses the
tag to preserve the original formatting of the text. This is useful when you want to show code, poetry, or any text where spacing matters. The browser does not change the spacing or line breaks inside this tag.
Why it matters
Without preformatted text, browsers collapse multiple spaces and ignore line breaks, making it hard to show text that relies on exact spacing. This would make displaying code snippets, ASCII art, or formatted data confusing or unreadable. Preformatted text solves this by keeping the text's original shape, helping readers understand the content as intended.
Where it fits
Before learning about preformatted text, you should understand basic HTML tags and how browsers handle whitespace. After this, you can learn about styling preformatted text with CSS or how to display code snippets with syntax highlighting.