How to show <div> tag literally in <code>/<pre> tag?
Asked Answered
D

11

93

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.

Determinable answered 8/7, 2012 at 20:49 Comment(2)
possible duplicate of How to escape < and > inside <pre> tagsDelete
What you really want is a <![CDATA[]]> section; good luck waiting for the browser authors to catch up with 1988 in between adding kewl feetchers.Timeserver
D
115

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.

Defile answered 8/7, 2012 at 20:50 Comment(2)
Thanks. I'm using this tool now to escape HTML characters: htmlescape.net/htmlescape_tool.htmlDeterminable
@XedinUnknown — And here 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 describes the content model of a pre element and it can contain elements. Compare to textarea which can only contain text.Defile
Q
62

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

<textarea readonly> <!-- html code --> </textarea>
Quintic answered 7/8, 2015 at 15:48 Comment(3)
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.Quintic
I didn't downvote, but in case anyone cares, it looks like character references are still parsed inside <textarea> … </textarea>, e.g. &gt; turns into >. This does mean it's not the "raw" contents anymore.Diathermic
This is a good answer, the only reason I can see someone using codes like &gt; would be the default styles of textarea. Otherwise, this is my go-to option whenever I need to do this in my code.Georgie
D
35

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".

Direful answered 6/10, 2016 at 15:42 Comment(5)
This tag is obsolete.Emend
@UsmanAhmed Be that as it may - the xmp is used by nice stuff like embedding markdownVisby
xmp may be deprecated, but it is still HTML spec to support it to this day (which is why all major browsers do). I'd imagine it never gets fully deprecated, as it's the only tag of its kind and is more widely used today than it was when it was deemed deprecated.Signalment
Still working in Chrome two years later, thanks for the answer that solved my problem.Slifka
HTML Spec says: Use pre and code instead, and escape "<" and "&" characters as "&lt;" and "&amp;" respectively. html.spec.whatwg.org/#xmpDallman
N
20

Try:

<xmp></xmp>

for exemple:

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

bye

Nygaard answered 13/4, 2017 at 18:16 Comment(3)
Works well but according to documentation, it's deprecated... Too bad because it's awesome :-DPushup
Roland gave the same answer a year earlier and with more detail so this is a duplicate.Slifka
bye......... :)Cockhorse
V
10

Just escape the HTML tags. For example -

Replace < with &lt;

Replace > with &gt;

Complete lookup here

Varicella answered 13/12, 2016 at 13:20 Comment(0)
I
5

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>
Ingra answered 17/4, 2020 at 11:54 Comment(0)
S
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!

Seeseebeck answered 2/6, 2015 at 20:3 Comment(0)
G
0

Not the best answer, but you could try to put a comment inside the tag like this:

<pre>
    <code<!-->>
        ...
    <<!-->/<!-->code>
</pre>
Georgie answered 9/3, 2020 at 20:37 Comment(0)
C
0

<pre>
  <code><textarea>
    <div>Now you can write Tags inside pre tag!</div>
  </textarea><code>
 <pre>
Casilde answered 27/10, 2021 at 8:9 Comment(0)
G
0

If you only need an opening tag, e.g <span>:

document.querySelectorAll('code').forEach(codeElement => {
    codeElement.innerText = `<${codeElement.innerText}>`;
});

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Gag answered 16/9, 2022 at 20:49 Comment(0)
F
-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>
Filberto answered 17/12, 2014 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.