mysqllearner
mysqllearner

Reputation: 13903

How to quote in HTML (not blockquote)?

I want to some sample code in a html page. How do I do it, for example I want to show a tag and image tag, in stackoverflow I use 4 spaces:

A Tag example:
<a href="your_url"></a>

Image Tag example:
<img...>

Upvotes: 5

Views: 13734

Answers (3)

CastleDweller
CastleDweller

Reputation: 8364

I think you're asking for the <pre> tag for preformatted text.

Everything that goes inside the pre tag it's not interpreted.

Try this on a browser:

<pre>
&lt;img src="whatever" /&gt;
&lt;othertag&gt;whatever&lt;/otherta&gt;
</pre>

EDIT: As Pekka pointed out, for HTML tags, symbols must be converted to their HTML entities.

Upvotes: 2

Pekka
Pekka

Reputation: 449395

To format the output, you can use the pre element.

Howver, you will still need to turn the special characters < > ' " & into their HTML entities &lt; &gt; and so on so the HTML tags turn up in clear text and don't get rendered by the browser.

If you can use PHP, there is htmlspecialchars() for that. Every server-side scripting language has an equivalent of it.

In JavaScript, setting the innerText property of the pre container leads to the characters being displayed literally and not as interpreted HTML.

If you want to do it manually, this is what you need to convert:

& to &amp;
> to &gt;
< to &lt;
" to &quot;
' to &#039

source

Upvotes: 6

TJ L
TJ L

Reputation: 24452

You can use a combination of the <pre> and <code> tags. The <pre> tag formats everything inside of it as is appears in the source code (useful for showing large blocks of source), and the <code> tag is used for inline formatting of code (similar to how the tags in this answer are formatted). Also, anything inside the <code> tags need to be converted into HTML special characters (& gt; and & lt; for example).

Upvotes: 1

Related Questions