3

I'm asking on how could we show html code on a tutorial html website, for example to show to user how to make a link?

<a href="#">text</a>
Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
clement
  • 4,204
  • 10
  • 65
  • 133

5 Answers5

4

Use HTML entities for it.

E.g.:

  • &lt; for <
  • &gt; for >

<pre>, <code> or <xmp> can be used but have compatibility issue.

Example:

  1. &lt;a href="#"&gt;text&lt;/a&gt;

  2. <span class="code"><span class="lt">&lt;</span>a href="#"<span class="gt">&gt;</span>text<span class="lt">&lt;</span><span class="fs">/</span>a<span class="gt">&gt;</span></span>

CSS for Example 2:

.code {
    font-family:Courier;
    color:#DF42AA;
}
.lt, .gt, .fs{
    font-family:Courier;
    color:#032F82;
}
Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
Kunj
  • 1,980
  • 2
  • 22
  • 34
4

With CDATASection. The CDATASection object represents a CDATA section in a document.

A CDATA section contains text that will NOT be parsed by a parser. Tags inside a CDATA section will NOT be treated as markup and entities will not be expanded.

You can use it in XML for sample :

<![CDATA[<tag>Some text</tag>]]>

It Will interpreted such as :

&lt;tag&gt;some text&lt;/tag&gt;

Or in program Output. Sample with CSS :

<style type="text/css">
/*<![CDATA[*/
body { background:black;  }     
/*]]>*/
</style>

Hope this help you.

Francois Borgies
  • 2,378
  • 31
  • 38
3

replace < by &lt; and > by &gt;

Rukshan C
  • 43
  • 4
2

An other way with CSS :

code::before { content:'<'; }
code::after { content:'>'; }

And then, your HTML code will be :

<code>a href="#"</code>text<code>/a</code>

From this website about CSS (but in french).

AlainPre
  • 441
  • 3
  • 4
0

use

&lt; for < and &gt; for >

A shortcut is "if you use &lt; for <,and don't use &gt; for >, then your output will be same as using both &lt; and &gt.