Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does code render in code block?

Tags:

html

tags

I want to display HTML in a webpage. I've wrapped in a code block but the last two lines still execute/render. What am I doing wrong?

<pre><code>
div {background: brown;}
div.bluebg {background: blue;}
<div>default brown background</div>
<div class="base">blue background</div>
</code></pre>

The last two lines were wrapped in div tags. I notice stackoverflow strips them out. I don't want to strip them but modify I guess with &lt; and &gt;. Is there a listing of tags that should be modified to render them in a webpage? Is there an online program that can convert these to the above syntax?

like image 826
4thSpace Avatar asked Jul 11 '26 04:07

4thSpace


2 Answers

I do not think [those tags] mean what you think they mean.

<pre> allows you to preserve white space and line feeds. <code> allows you to semantically indicate that code is being displayed on your page. Both have some default styles (such as applying a fixed-width font), but neither one does anything to escape <, >, &, or ", so any unescaped HTML code you put in between those tags is going to be processed as HTML. You'll have to use &lt;, &gt;, &amp;, and &quot;. Here's a page where you can paste in text and have it escaped: http://accessify.com/tools-and-wizards/developer-tools/quick-escape/

like image 189
David Kolar Avatar answered Jul 13 '26 18:07

David Kolar


You need to replace < with &lt;, > with &gt; and & with &amp; and that's it.

like image 37
Svante Svenson Avatar answered Jul 13 '26 19:07

Svante Svenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!