HTML <pre> Tag
The <pre> tag displays preformatted text, preserving spaces, line breaks, and indentation exactly as written. It's commonly used to display code, ASCII art, or any text where formatting is important. The text inside <pre> is displayed in a monospace font.
Whitespace Preservation
The <pre> tag preserves whitespace and line breaks. Multiple spaces are kept, and line breaks are respected. This is different from regular HTML where multiple spaces collapse into one and line breaks are ignored. The <pre> tag is perfect for displaying code snippets, formatted text, or structured data.
Common Use Cases
Common use cases include: displaying code snippets, showing formatted text with specific spacing, ASCII art, poetry with line breaks, and structured data. The <pre> tag renders text in a monospace font by default, making it ideal for code. You can combine it with <code> for semantic code display.
Best Practices
Use <pre> when you need to preserve exact formatting. Combine with <code> for semantic code display. Be aware that long lines may overflow - consider using CSS to handle overflow. Use <pre> for code examples, formatted output, or any content where whitespace matters.
