82

I'm using <code> tag inside of a <pre> tag to show code on my blogger blog. Unfortunately it doesn't work with HTML tags. Is there any way to show "<div>" inside of <pre> or <code> tag without actually interpreting it as HTML? This is what I do right now:

<pre>
 <code>
 .class {        
   color:red;
 }
 // I would like HTML code inside this
 </code>
</pre>

Which works ok for everything except HTML. Any idea how to achieve this? Thanks.

Loolooii
  • 7,580
  • 14
  • 59
  • 87
  • 1
    possible duplicate of [How to escape < and > inside
     tags](http://stackoverflow.com/questions/42182/how-to-escape-and-inside-pre-tags)
    – Jukka K. Korpela Jul 08 '12 at 21:19
  • 1
    What you really want is a `` section; good luck waiting for the browser authors to catch up with 1988 in between adding kewl feetchers. – Toby Speight Aug 07 '15 at 15:55

10 Answers10

103

Unfortunately it doesn't work with HTML tags.

<code> means "This is code", <pre> means "White space in this markup is significant". Neither means "The content of this element should not be treated as HTML", so both work perfectly, even if they don't mean what you want them to mean.

Is there any way to show "<div>" inside of <pre> or <code> tag without actually interpreting it as HTML?

If you want to render a < character then use &lt;, with &gt; for > and &amp; for &.

You can't (in modern HTML) write markup and have it be interpreted as text.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • 7
    Thanks. I'm using this tool now to escape HTML characters: http://www.htmlescape.net/htmlescape_tool.html – Loolooii Jul 08 '12 at 21:16
  • 2
    @XedinUnknown — And [here](http://www.w3.org/TR/html-markup/Overview.html) it says that you shouldn't reference that document. If you are right then **every browser on the planet** is not standards-compliant. You are not right. [The specification](http://www.w3.org/TR/html5/grouping-content.html#the-pre-element) describes the content model of a pre element and it can contain elements. Compare to [textarea](http://www.w3.org/TR/html5/forms.html#the-textarea-element) which can only contain text. – Quentin Sep 10 '15 at 12:55
52

It seems like a readonly textarea does pretty much what you want.

<textarea readonly> <!-- html code --> </textarea>
showdev
  • 25,529
  • 35
  • 47
  • 67
Jason Wilkins
  • 641
  • 5
  • 2
  • 10
    I would like some clarification why this has been voted down. The textarea tag allows literal HTML code. I just happen to be using it to display literal HTML when I came across this question and the answers seems rather complicated compared to just using a textarea. – Jason Wilkins Aug 09 '15 at 16:54
  • I didn't downvote, but in case anyone cares, it looks like character references are still parsed inside ``, e.g. `>` turns into `>`. This does mean it's not the "raw" contents anymore. – ShreevatsaR Dec 30 '20 at 23:51
27

You can use the "xmp" element. The <xmp></xmp> has been in HTML since the beginning and is supported by all browsers. Even it should not be used, it is broadly supported.

Everything inside <xmp></xmp> is taken as it is (no markup lke tags or character references is recognized there) except, for apparent reason, the end tag of the element itself.

Otherwise "xmp" is rendered like "pre".

Roland
  • 538
  • 6
  • 7
17

Try:

<xmp></xmp>

for exemple:

<pre>
     <xmp><!-- your html code --></xmp>
</pre>

bye

Alberto Cerqueira
  • 1,073
  • 12
  • 16
9

Just escape the HTML tags. For example -

Replace < with &lt;

Replace > with &gt;

Complete lookup here

Baum mit Augen
  • 46,177
  • 22
  • 136
  • 173
Vivek Kalyanarangan
  • 7,530
  • 1
  • 17
  • 35
3

This can be easily achieved with a little bit of javascript.

document.querySelectorAll("code").forEach(el => el.innerText = el.innerHTML);

Run snippet below to see it in action:

document.querySelectorAll("code").forEach(el => el.innerText = el.innerHTML);
pre {
  padding: 1rem;
  background-color: #eee;
  border-radius: 0.25rem;
}
<pre>
  <code>
   .class {        
     color:red;
   }
   
   // I would like HTML code inside this
   <h1>Hello this is a heading</h1>
  </code>
</pre>
0

Try CodeMirror (https://codemirror.net/)

It's a lightweight library that styles code in HTML. Here's a screenshot of what I'm referring to:

enter image description here

Worked well for us!

Collarbone
  • 542
  • 6
  • 15
0

Maybe not the best answer, but you can do something like this:

<p><<input type="hidden">h1<input type="hidden">><input type="hidden">This code is 
displayed!<input type="hidden"><<input type="hidden">/h1<input type="hidden">></p>

<p><<input type="hidden">h1<input type="hidden">><input type="hidden">This code is displayed!<input type="hidden"><<input type="hidden">/h1<input type="hidden">></p>
-3

Use this:

<pre>
   <?= htmlentities($script) ?>
</pre>
jkdev
  • 9,037
  • 14
  • 52
  • 75
-4

Yes, with an escape xml function. You'll need to have jQuery enabled for it to work though.

<pre>
  ${fn:escapeXml('
    <!-- all your code -->
  ')};
</pre>
PanicBus
  • 548
  • 1
  • 7
  • 16