tags:

views:

378

answers:

3

how do you write html code so the user and see it on a webpage (like a how-to for html)

+6  A: 

encode your html entities:

< … &lt;
> … &gt;
& … &amp;
" … &quot;
(' … &apos; xml, not html. see comments)

you might also want to use

<pre><code>
here comes your preformatted and escaped &lt;html&gt;-code
</code></pre>

to have your code monospaced and preserve whitespaces

knittl
doesn't <verbatim> give you the same? i thought <pre> will still expand any html tags found within?
Rob Wells
' isn't supported by IE because it isn't a valid character entity in HTML. The list of HTML character entities is at http://www.w3.org/TR/html401/sgml/entities.html and doesn't include it. It is specified in XML 1.0: http://www.w3.org/TR/2008/REC-xml-20081126/#syntax
NickFitz
Hmm, SO manages to escape ' in comments but not in posts...
NickFitz
@Rob: What do you mean by "expand"? In general, there is *no* section of html that is not escaped - not even script tags or style tags (browsers will generally try to to repair missing escaping as best they can though - and a long time ago).
Eamon Nerbonne
@ rob wells: there is no such tag as `<verbatim>`@ nickfitz: see <http://www.w3.org/TR/2008/REC-xml-20081126/#sec-predefined-ent>. looks like ' :P
knittl
@knittl: if you're programmatically encoding illegal codepoints, it is possibly simpler to use numeric entity references, whether decimal or hexadecimal - and you won't run into details like apos being html 5 or xml but not html 4.
Eamon Nerbonne
@ eamon: using builtin functions like php’s htmlspecialchars solves the problem perfectly :) (but yeah, i got html and xml confused)
knittl
@knittl: HTML is not XML, and ' is not an HTML entity. _XHTML_ allows ' because it _is_ XML, but IE doesn't support XHTML, so it doesn't support ' See the XHTML 1.0 spec: http://www.w3.org/TR/xhtml1/#C_16
NickFitz
nickfitz, i already got it ;)
knittl
@knittl: Oops, so I see - sorry to nag ;-)
NickFitz
A: 

You have to use HTML character entities &lt; and &gt; in place of the < and > symbols so they aren't interpreted as HTML tags.

Jonathan Patt
A: 

To avoid issues with &apos;, use &#39;.

Imagist