0

I am building a simple html tutorial for my university . But I am not able to put the examples within the html file ! I tried <code></code> but it did not worked ! I searched google but no help from there .

8
  • You need to encode your delimiters: jsfiddle.net/userdude/VdFpt Commented Sep 5, 2011 at 2:58
  • Do you mean how do you stop your HTML code being rendered by the browser and actually show the markup? Example: you want to display <b>This is bold text</b> instead of This is bold text. Is that right? Commented Sep 5, 2011 at 3:00
  • @sje <pre></pre> did not worked ! Commented Sep 5, 2011 at 3:01
  • @Jared I need to write delimiters manually ? Commented Sep 5, 2011 at 3:01
  • 2
    Not necessarily; for instance, in PHP you could use htmlspecialchars(). Commented Sep 5, 2011 at 3:03

1 Answer 1

7

You need to HTML escape any HTML special characters.
To conserve the indentation, you should wrap it in <pre> tags.
To format it as code visually, you should also wrap it in <code> tags.
The results looks like this:

<pre><code>
&lt;p&gt;
   This is an example of
   &lt;strong&gt;HTML&lt;/strong&gt;.
&lt;/p&gt;
</code></pre>

This is rendered as:

<p>
   This is an example of
   <strong>HTML</strong>.
</p>
Sign up to request clarification or add additional context in comments.

6 Comments

This is the correct way. @Suraj, there are tools on the internet to help you quickly escape special characters such as this one: felgall.com/htmlt47.htm
What is the benefit of using CODE like that?
Thank you very much mwan100....I was wondering how I could write long examples :) You two solved my question . Thanks
@Jared It's simply the semantic markup for code. By default it means browsers display in a monospaced font.
I thought that PRE's did that as well in most (all?) browsers, but I guess I'm not sure. I can see the semantic argument.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.