How can I show HTML snippets on a webpage without needing to replace each < with < and > with >?
In other words, is there a tag for don’t render HTML until you hit the closing tag?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, there is not. In HTML proper, there’s no way short of escaping some characters:
&as&<as<(Incidentally, there is no need to escape
>but people often do it for reasons of symmetry.)And of course you should surround the resulting, escaped HTML code within
<pre><code>…</code></pre>to (a) preserve whitespace and line breaks, and (b) mark it up as a code element.All other solutions, such as wrapping your code into a
<textarea>or the (deprecated)<xmp>element, will break.1XHTML that is declared to the browser as XML (via the HTTP
Content-Typeheader! — merely setting aDOCTYPEis not enough) could alternatively use a CDATA section:But this only works in XML, not in HTML, and even this isn’t a foolproof solution, since the code mustn’t contain the closing delimiter
]]>. So even in XML the simplest, most robust solution is via escaping.1 Case in point: