Discover how to keep your text looking exactly right on your webpage without extra hassle!
Why Preformatted text in HTML? - Purpose & Use Cases
Imagine you want to show a poem or a piece of code on your webpage exactly as it looks, with all the spaces and line breaks preserved.
If you just type it normally in HTML, the browser will ignore extra spaces and line breaks, making your carefully formatted text look messy and hard to read.
The <pre> tag keeps all spaces, tabs, and line breaks exactly as you type them, so your text appears just like you want it.
This is spaced text on multiple lines.
<pre>This is spaced text on multiple lines.</pre>
You can display text with exact formatting, like code snippets or poems, making your content clear and easy to understand.
Showing a block of computer code on a tutorial website where indentation and line breaks matter for understanding.
Browsers ignore extra spaces and line breaks by default.
The <pre> tag preserves all formatting exactly.
This helps display code, poems, or any text needing precise layout.
Practice
<pre> tag do in HTML?Solution
Step 1: Understand the purpose of <pre> tag
The <pre> tag is used to display preformatted text, meaning it keeps all spaces and line breaks exactly as typed.Step 2: Compare with other options
Other options describe different HTML tags or functions unrelated to <pre>.Final Answer:
Displays text exactly as typed, preserving spaces and line breaks -> Option BQuick Check:
<pre> preserves spacing = A [OK]
- Thinking <pre> formats text like <p>
- Confusing <pre> with <code> tag
- Assuming <pre> adds styling like bold or links
Solution
Step 1: Identify the correct HTML tag for preformatted text
The correct tag is <pre> to show text exactly as typed.Step 2: Check other options for validity
<preformatted> is not a valid tag, <code> is for code but does not preserve spaces, <p> is for paragraphs without preserving spaces.Final Answer:
<pre>This is preformatted text</pre> -> Option CQuick Check:
Correct tag for preformatted text = B [OK]
- Using <preformatted> which is invalid
- Confusing <code> with <pre>
- Using <p> which does not preserve spaces
Line 1 Line 2 indented Line 3
Solution
Step 1: Understand <pre> tag behavior
The <pre> tag preserves all spaces and line breaks exactly as typed in the code.Step 2: Analyze the given code
The text has line breaks and indentation spaces before 'Line 2 indented'. These will appear exactly as shown.Final Answer:
Line 1 Line 2 indented Line 3 (with spaces and line breaks preserved) -> Option AQuick Check:
<pre> preserves spaces and lines = D [OK]
- Thinking spaces are collapsed like normal text
- Assuming line breaks are ignored
- Confusing <pre> with <p> or <div>
<pre> Hello World </pre>
Solution
Step 1: Examine the code structure
The code shows <pre> tags inside another <pre> block, which means nested preformatted text.Step 2: Understand <pre> nesting effect
Nested <pre> tags cause the inner tags to be shown as text, not as HTML tags, which is usually unintended.Final Answer:
The <pre> tags are inside another <pre>, causing nested preformatted blocks -> Option DQuick Check:
Nested <pre> shows tags as text = A [OK]
- Thinking nested <pre> tags are valid HTML structure
- Assuming spaces inside <pre> are errors
- Ignoring that tags inside <pre> show as text
Solution
Step 1: Identify the requirement for exact spacing and line breaks
Poetry needs spaces and line breaks preserved exactly as typed.Step 2: Choose the HTML tag that preserves formatting
The <pre> tag preserves all spaces and line breaks, making it ideal for poems.Step 3: Evaluate other options
Multiple <p> tags break text into paragraphs but don't preserve spaces; <div> and <span> need extra CSS and don't preserve spaces by default.Final Answer:
Use the <pre> tag to wrap the poem text -> Option AQuick Check:
Exact spacing needs <pre> = C [OK]
- Using <p> which ignores extra spaces
- Relying on CSS alone without <pre>
- Using <span> which is inline and ignores line breaks
Ready to prove it?
Pass the test and this pattern moves unknown
