Steven Steven - 6 months ago 23
HTML Question

Display HTML code in HTML

Is there a way to show HTML code-snippets on a webpage without needing to replace each < with

&lt;
and > with
&gt;
?

In other words, is there some tag which simply says "Don't render HTML until you hit the closing tag"?

Answer

In HTML proper, there’s no way short of escaping the characters. However, in properly declared XHTML (which is XML, and needs to be declared to the browser as such; in particular it’s not enough to just put in a DOCTYPE), you can use a CDATA section:

<![CDATA[Your <code> here]]>

But this only works in XML, not in HTML.

In HTML, the only solution guaranteed to work everywhere is to escape the code (< and & as &lt; and &amp;, respectively) manually.

Comments