13. pre Tag
Introduction
Section titled “Introduction”The <pre> (preformatted) element preserves whitespace, line breaks, and formatting exactly as written in the HTML source. It’s commonly used for code blocks, ASCII art, or any content where exact formatting is important. The pre element also uses a monospace font by default.
Basic Usage
Section titled “Basic Usage”<pre> This text preserves spaces and line breaks.</pre>Common Use Cases
Section titled “Common Use Cases”Code Blocks
Section titled “Code Blocks”Displaying code:
<pre>function hello() { console.log("Hello, World!");}</pre>ASCII Art
Section titled “ASCII Art”<pre> /\_/\ ( o.o ) > ^ <</pre>Formatted Text
Section titled “Formatted Text”Preserving formatting:
<pre>Name: John DoeEmail: john@example.comPhone: 123-456-7890</pre>Styling
Section titled “Styling”Customize appearance:
<pre>Code here</pre>
<style> pre { background-color: #f4f4f4; padding: 1em; border-radius: 4px; overflow-x: auto; }</style>Best Practices
Section titled “Best Practices”Use for Code
Section titled “Use for Code”For code, consider using <code> with <pre>:
<pre><code>function example() { return "Hello";}</code></pre>