bflemi3
bflemi3

Reputation: 6790

Display CDATA as text in html

How can I display <!CDATA[ as text in an html document? I tried wrapping it in <pre> tags but that didn't work.

<! DOCTYPE html >
<html>
<body>
    <div>This should show <pre><!CDATA[</pre> when the page renders</div>
</body>
</html>

Upvotes: 0

Views: 2716

Answers (2)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201828

As others have written, you should escape “<” as &lt;. HTML markup like pre or code does not change the way content is parsed, with “<” as markup-significant. You can use e.g. code markup here, since the content is computer code and this markup may have some advantages, but that’s a different issue. Example: <code>&lt;!CDATA[</code>.

There is a half-secret markup, however: <xmp><!CDATA[</xmp>. Within an xmp element, everything is taken literally; only the end tag of this element is recognized. Such markup is regarded as very obsolete by many, and it has been dropped from HTML specs, but it actually keeps working.

Upvotes: 2

Quentin
Quentin

Reputation: 944246

Browsers do not support CDATA markers in text/html documents. Use character references instead (&lt;, &amp;, etc).

Upvotes: 4

Related Questions