Lyra Orwell
Lyra Orwell

Reputation: 1258

How do you break out of HTML Syntax?

I am trying to write a programming tutorial for the C programming language. I want to show a code example that imports. I have used the <code> tag to denote code, however HTML treats the import statements in the code itself as tags due to the <> characters.

HTML Fragment

<code>
#include <stdlib.h>
#include <stdio.h>
</code>

Image From Editor

Example

Upvotes: 0

Views: 75

Answers (3)

John
John

Reputation: 13739

HTML is only one version available, there is XML. Why is this important? If you're writing a tutorial write it with the highest accuracy first. You can easily degrade XML down to HTML without problems however the vast majority of instances HTML can not be upgraded to XML without numerous fixes.

There are two separate parsers for HTML and XML. One could say that the XML parser is much stricter however that would imply that HTML parsers are strict in any sense.

Do not use named entities, they break XML parsers.

"But XML is not HTML5!"

HTML and XML have parsers and standards. I've written an entire platform that utilizes the XML parsers of browsers and exceeds HTML5 standards. Using the HTML parser one will not be aware of issues like missing quotes on attributes. I witnessed someone lose three days trying to determine why Safari would not render part of a page correctly. Had they used the XML parser they could have recovered and remained productive in less than a minute!

  • Ampersands, &#38;
  • Less Than &#60;
  • Greater Than &#62;

Keep in mind that browsers are not worth testing, rendering engines are worth testing.

  • Blink (Chrome and endless clones)
  • Gecko (Waterfox / Firefox)
  • KHTML (Konqueror (Linux)
  • Presto (Opera, defunct)
  • Trident (defunct, Internet Explorer and "Edge" rebrand)
  • WebKit (Safari)

Upvotes: 0

Juan Marco
Juan Marco

Reputation: 3241

Wrapping any code inside <code> or <pre> doesn't prevent the Browser from reading the angle brackets. You'll need to escape them with HTML Entities:

<code>
  #include &lt;stdlib.h&gt;
  <br>
  #include &lt;stdio.h&gt;
</code>

Note: There's an obsolete <xmp> element that automatically renders the contents without interpreting as HTML. It's not advisable to use it because Browser support is not guaranteed.

Upvotes: 0

Mat Sz
Mat Sz

Reputation: 2552

You will need to use the HTML character entities, for example &gt; for > and &lt; for <. Here is a reference chart for most of them: https://dev.w3.org/html5/html-author/charref

Upvotes: 1

Related Questions