Issue
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?
Solution
is there a tag for don't render HTML until you hit the closing tag?
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.1
XHTML that is declared to the browser as XML (via the HTTP Content-Type
header! — merely setting a DOCTYPE
is not enough) could alternatively use a CDATA section:
<![CDATA[Your <code> here]]>
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:
textarea {border: none; width: 100%;}
<textarea readonly="readonly">
<p>Computer <textarea>says</textarea> <span>no.</span>
</textarea>
<xmp>
Computer <xmp>says</xmp> <span>no.</span>
</xmp>
Answered By - Konrad Rudolph
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.