9

Basic question which I thought of asking on Superuser, but it is a programming question I think. I just started learning HTML, so please bear with me.

How can I prevent a program from interpreting an HTML tag / syntax? For example, I want to write a flash card like this:

The html code for < is &lt;

I would like a solution that would work for any or most syntax, not just for <.

How can I enter the syntax (without any space) to make sure the code isn't interpreted?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
JDelage
  • 13,036
  • 23
  • 78
  • 112

9 Answers9

10

This:

The html code for &lt; is &amp;lt;

Renders as:

The html code for < is &lt;

The basic strategy is to escape the & as &amp;

Daniel Martin
  • 23,083
  • 6
  • 50
  • 70
9

You are going to have to do it manually.

Here you have the full encoding table. The most commonly used codes are:

Character  Entity Number  Entity Name  Description
"          &#34;          &quot;       quotation mark
'          &#39;          &apos;       apostrophe (does not work in IE)
&          &#38;          &amp;        ampersand
<          &#60;          &lt;         less-than
>          &#62;          &gt;         greater-than
Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
  • @voyager : Not as easy as inserting two tag if you disable interpretation with JavaScript. – user2284570 Sep 06 '14 at 20:48
  • Unable to edit the answer, the link is broken. Working link : https://www.w3schools.com/charsets/ref_html_entities_4.asp – SwissFr Nov 01 '21 at 09:42
7

In this case, you DON'T need to encode it. Try this one:

<xmp> html < &lt; </xmp>

I'm not sure about cross browsers support, but works on IE7,FF3,Chrome3

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
4

I suppose you don't want the entity to be rendered? If you want to display &lt; you'll have to use the entity for the ampersand: &amp;.

Fabian Vilers
  • 2,892
  • 2
  • 24
  • 30
1

The html code for < is &lt;

That is, type &lt; is &amp;lt;.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
1

If you have access to server side scripting capabilities, you might be able to use utility functions of that platform. For example, in PHP you might use the htmlentities function to your advantage:

echo htmlentities("The html code for < is &lt;");
Daan
  • 6,952
  • 4
  • 29
  • 36
0

You could use a <pre> &lt; </pre> sequence

rsp
  • 23,135
  • 6
  • 55
  • 69
  • I think this is the solution I need, i.e., that would work for any tag, correct? Basically anything written between
     and 
    is treated as dumb text?
    – JDelage Oct 09 '09 at 11:34
  • 4
    The pre element preserves whitespace, but it doesn't escape tags. – NickFitz Oct 09 '09 at 11:35
  • Nick is correct in it that it doesn't escape tags (which escaped me for a moment :-), so a combination of <pre>, &lt; and &amp; would do the trick for you. The <pre> tags are presented in a fixed spaced font which makes it clear is meant to be a code example. – rsp Oct 09 '09 at 11:43
  • @rsp: although it would be more semantically correct to use
    ...
    : the code tags make it clear that it's code, as opposed to other text where whitespace needs to be preserved, such as poetry. P.S. Isn't it annoying the way SO has different automatic escaping rules in comments to those in answers ;-)
    – NickFitz Oct 09 '09 at 11:49
  • @NickFitz: xmp element does escape tags; – Rubens Farias Oct 09 '09 at 16:36
0

You need to encode it. For example, '<' = '&lt;'

Here is the list.

So in your case it will end up like this:

The html code for &lt; is &amp; l t ;
NawaMan
  • 25,129
  • 10
  • 51
  • 77
0
<textarea readonly rows="2" cols="50" style="border:none; color:lightGray; background-color:black;">
<<line1>>
<<line2>>
</textarea>

is the only supported option . but its not enabled neither here neither on GitHub .

i'm 5min with HTML, so also would appreciate anyone enhancing it with dynamic size, achieved trough the tag's HTML/CSS attributes, or, if its not possible, trough a small embed Java Script .

irvnriir
  • 673
  • 5
  • 14