Is the following valid HTML5?
<p>1 < 2</p>
<p>2 > 1</p>
Or must this always be written using HTML5 entities like this?
<p>1 < 2</p>
<p>2 > 1</p>
Can someone help me answer this question with references to the HTML5 specification that clearly spells out whether or not it is valid to write <
and >
(spaces around the symbols) in HTML?
2
Answers
This is valid
The first line below is TOLERATED in all browsers
but is in principle an
invalid-first-character-of-tag-name
errorThe w3org parser will flag the
<
as invalid.Here are other related issues
and
and
Restrictions
>
in intended text content is and always had been safe and valid in HTML, even without spacing.<
is technically invalid when it does not constitute tag in context where tags are expected. It switches parser state to state that expect valid tag name. Like all errors in HTML, it is recoverable, so the moment parser sees anyBad character after <
, it rolls back to text content and adds the<
and that "bad character" into it, and proceeds further. So it is displayed the same way if it was encoded as<
Testing
in https://validator.w3.org/nu/#textarea yelds:
N.B. in
title
andtextarea
<
is OK, since there cannot be any nested non-text nodes (not even comments).