12

I know it is possible because this website does it, but I tried researching how and just got a bunch of junk, so how do I add tags to a website paragraph without the browser interpreting it as code.

For example, if I have <p><div></div></p>, I want the div to display in the browser as text not have the browser interpret it as html. Is this complicated to do?

I have been writing tutorials for school, and it would be much easier if I could add the code directly to the webpage in text form instead of images, so students can copy and paste it.

Tom Nolan
  • 1,916
  • 4
  • 32
  • 51

11 Answers11

15

Look at how this website itself achieves this:

<p>For example, if I have <code>&lt;p&gt;&lt;div&gt;&lt;/div&gt;&lt;/p&gt;</code>, I want the div to display in the browser as text not have the browser interpret it as html. Is this complicated to do?</p>

You need to replace the < and > with their HTML character entities.

Matt Healy
  • 18,033
  • 4
  • 56
  • 56
  • 3
    It is never necessary to escape “>” (though it can be done for symmetry). It is often necessary to escape “&” in addition to “<”. – Jukka K. Korpela Feb 18 '13 at 06:41
10

There are many ways to use:

  1. Replace < with &lt;

    `&lt;h1>This is heading &lt;/small>&lt;/h1>`
    
  2. Place the code inside </xmp><xmp> tags

    <xmp>
        <ul>
            <li>Coffee</li>
            <li>Tea</li>
        </ul>
    </xmp>
    

I do not recommend other ways because they do not work on all browsers like <plaintext> or <listing>.

Genhis
  • 1,484
  • 3
  • 27
  • 29
Majali
  • 480
  • 7
  • 11
6

You want to look into something called HTML Entities.

If you want the < character to appear on a website, for example, you can write this HTML code: &lt;. These are the five basic HTML Entities and their source code equivalents:

< &lt;
> &gt;
" &quot;
' &apos;
& &amp;

If you are using a programming language (such as PHP or ASP.NET), then there is probably a built-in command that will do the conversion for you (htmlspecialchars() and Server.HtmlEncode, respectively).

Chris
  • 3,328
  • 1
  • 32
  • 40
6

Use the tag <PRE> before a block of reformatted text and </PRE> after. The text between these tags is rendered as monospaced characters with line breaks and spaces at the same points as in the original file. This may be helpful for rendering poetry without adding a lot of HTML code. Try this:

Mary had a little lamb. 
    Its fleece was white as snow.
And everywhere that Mary went
    the lamb was sure to go.
Ralph Nelson
  • 61
  • 1
  • 1
  • Best solution to save time! I could add a small style improvement: `` – yulian Jan 13 '21 at 15:11
4

To add plain text code in a webpage, HTML Character Escaping is needed on five characters:

< as &lt;
> as &gt;
& as &amp;
' as &apos;
" as &quot;

(OR)

<xmp> tag may also be used as an alternate, this tag disturbs the style and is obsolete.

<xmp>Code with HTML Tags like <div> etc. </xmp>
Community
  • 1
  • 1
Nik
  • 173
  • 2
  • 17
  • According to [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/xmp), the `` tag is obsolete. Just tested it and it won't render correctly in Chrome. – ChrisDevWard Nov 01 '18 at 14:32
1

Use the html entity/special character of the tag, such as &lt; (for less than)

&lt;p&gt; in html -> <p> in browser

You could also write &lt;p> since there is no ambiguity about the opening tag.

Many languages have built in methods to convert HTML special characters such as php's htmlspecialchars

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

You need to escape the HTML tags, namely the less-than sign. Write it as &lt; and it will appear as < on the HTML page.

kufudo
  • 2,803
  • 17
  • 19
0

Your html needs to not be in tags. If you use the <> tags you will have it converted into code not text, if I was to write <br> in the middle of a sentence
then it would do this You will need to Write the code in code so to speak, using the < > (&lt; &gt;) and then you get what you need.

Mark Gavagan
  • 878
  • 12
  • 45
Stonydallas
  • 163
  • 1
  • 1
  • 11
0

I just discovered a much simpler solution at CSS-Tricks... Just have your outer-most wrapper be a 'pre' tag, followed by a 'code' tag, then inside the code tag put your code in paranthesis.

0

The simplest way to do it without having to reformat your text using entities is to use JQuery.

<div id="container"></div>

<script>
    $('#container').text("<div><h1>Hello!</h1><p>I like you.</p></div>");
</script>

If you then do alert($('#container').prop('innerHTML'));, you get &lt;div&gt;&lt;h1&gt;Hello!&lt;/h1&gt;&lt;p&gt;I like you.&lt;/p&gt;&lt;/div&gt;

How useful that technique is depends somewhat on where your material is coming from.

abalter
  • 9,663
  • 17
  • 90
  • 145
0

Use iframe and txt file: <iframe src="html.txt"></iframe>

someone
  • 29
  • 2