Junior
What are the features of spaces in HTML?
sobes.tech AI
Answer from AI
HTML ignores repeated spaces and line breaks, displaying them as a single space. The exception is content inside <pre> tags or elements with the style white-space: pre;, where spaces and line breaks are preserved.
Non-breaking space is used to prevent line breaks between words and to ensure visibility of consecutive spaces.
Table of white-space behavior:
| Value | Description |
|---|---|
normal |
Consecutive spaces and line breaks are collapsed into a single space. Text wraps as needed. |
nowrap |
Consecutive spaces and line breaks are collapsed into a single space. Text does not wrap. |
pre |
Consecutive spaces and line breaks are preserved. Text wraps only at newline characters. |
pre-wrap |
Consecutive spaces are preserved. Text wraps at newline characters and as needed. |
pre-line |
Consecutive spaces are collapsed into one. Line breaks are preserved. Text wraps as needed. |
Example of using non-breaking space:
<!-- Additional space between words, which will be displayed -->
<p>This is normal text.</p>
<!-- Prevent line break -->
<p>Date: 12 October</p>
Example of using <pre> tag:
<pre>
This text
will preserve formatting
with spaces and line breaks.
</pre>
Example of using CSS white-space property:
<div style="white-space: pre-wrap;">
This text will preserve spaces
and will wrap
as needed.
</div>