ItzJavaCraft
ItzJavaCraft

Reputation: 91

How to make code show up in a HTML paragraph?

I want to be able to make code show up on my website so that it shows up with the code on the webpage instead of using it as code in the HTML file itself.

Example:

<!DOCTYPE html>
<html>
  <body>
  <p>Hello World</p>
  </body>
</html>

Instead of:

Hello World

I know I explained that horribly but I'm sure you can see where I'm coming from. Can you escape in HTML? Or is there a tag that allows for HTML code to be viewed as text on a webpage?

Upvotes: 0

Views: 2400

Answers (3)

Giuseppe
Giuseppe

Reputation: 876

xmp tag

<!DOCTYPE html>
<html>
  <body>
  <xmp><p>Hello World</p></xmp>
  </body>
</html>

keep in mind that xmp tag is considered obsolete, as far as I know it is still supported by most browser but your mileage may vary.

you are safer if you use <pre> and escape html code with &lt; and &gt; like this

<!DOCTYPE html>
<html>
  <body>
      <pre>&lt;p&gt;Hello World&lt;/p&gt;</pre>
  </body>
</html>

Upvotes: 4

Rasik
Rasik

Reputation: 903

You can use the xmp property. Anything inside the xmp that is exempted by the browser while rendering the HTML code.

Example :

<xmp><h1>Heading</h1></xmp>

Upvotes: 0

Michael Seltene
Michael Seltene

Reputation: 591

There is similar question answered on this link: Display HTML code in HTML

In addition, have a look at the following websites

https://craig.is/making/rainbows

https://highlightjs.org/

Upvotes: 1

Related Questions