2

Like I want a code block like below.

  <div>
     abcd
  </div>

When I enclose it under pre it doesn't do anything.

I googled it and reached this "If you want to use a code example that includes HTML, you must use HTML entities for your < and >. If you don't do this, it will be interpreted as actual HTML."

Not sure what that means though

name
  • 235
  • 2
  • 12

4 Answers4

5

Please try this code:

<code>
  &lt;div&gt; <br />
  abcd
 <br /> &lt;/div&gt; 
  </code>
2

Some characters are reserved in HTML. If you use the less than (<) or greater than (>) signs in your text, the browser might mix them with tags. Character entities are used to display reserved characters in HTML.

To display a less than sign (<) we must write: &lt; or &#60; To display a less than sign (<) we must write: &lt; or &#62; Here are some other references, https://dev.w3.org/html5/html-author/charref

So, if you want to display <html> inside your HTML output, you have to write &lt;html&#gt; or &60;html&#62; For example

<body>
This is a sample &#60;html&#62; 
</body>
Chilarai
  • 1,842
  • 2
  • 15
  • 33
0

There are number of ways available to do that. In this post I'm going to give you some definition and example from each.


  1. Using HTML Special Characters

<code>
   &lt;div class=&quot;div_class&quot;&gt; <br>
         &nbsp;&nbsp;&nbsp;abcd <br>
   &lt;/div&gt;
</code>

  1. Using ASCII Character Set

<code>
   &#60;div class&#61;&#34;div_class&#34;&#62; <br>
         &nbsp;&nbsp;&nbsp;abcd <br>
   &#60;&#47;div&#62;
</code>

See the reference here


  1. Using HTML only

<code>
   <span><</span>div class="div_class"<span>></span> <br>
         &nbsp;&nbsp;&nbsp;abcd <br>
   <span></</span>div<span>></span>
</code>

  1. Completely Using ASCII Character Set

<code>
   &#60;&#100;&#105;&#118; &#99;&#108;&#97;&#115;&#61;&#34;&#100;&#105;&#118;&#95;&#99;&#108;&#97;&#115;&#34;&#62; <br>
         &nbsp;&nbsp;&nbsp;&#97;&#98;&#99;&#100; <br>
   &#60;&#47;&#100;&#105;&#118;&#62;
</code>

See the reference here

Amaan warsi
  • 184
  • 4
  • 18
0
<pre class="default prettyprint prettyprinted" style="">
 <code>
  <span class="pln"></span>
  <span class="tag">&lt;div&gt;</span>
  <span class="pln">abcd</span>
  <span class="tag">&lt;/div&gt;</span>
 </code>
</pre>

This is the HTML code what StackoverFlow make.

If you want to make the code autoly, the best solution is that use JavaScript to deal with your plain text data that get from the backend.

Such as with Markdown:

data = Markdown(data)
$(".Markdown-div").html(data)
Peterlits Zo
  • 476
  • 1
  • 4
  • 17