Which Characters Should Be Escaped Inside A “pre” tag?
The <pre> tag defines pre-formatted text. Everything (generally, text and code snippets) in a <pre> tag element is displayed in a fixed-width font, and preserves both spaces and line breaks in them. In other words, if anyone wants to show their code snippets on a web page, they can simply enclose their code in the tag.
But there are certain things which most of people completely miss. There are some reserved characters in the <pre> tag. Reserved characters are those characters that are meant to serve a specific or reserved purpose. So here is the list of all the reserved character that should also be escaped while writing the code inside it:
- Less Than (<)
- Greater Than (>)
- Ampersand (&)
- Single Quote or Apostrophe (‘)
- Double Quotes (“)
As already mentioned there are some reserved characters in <pre> tag. These characters have a specific meaning and if we put these characters inside our <pre> tag, it will treat them as a reserved character, and hence, we need to escape them.
- Less Than (<): It is reserved for use in tags (as in <div></div>). Thus, this character will have this specific meaning only. To escape them in <pre> tag tag, we need to use < for HTML entity name or < for HTML entity number as a replacement.
- Greater Than(>): It is reserved for use in tags (as in <body></body>). Thus, this character will have this specific meaning only. To escape them in pre tag, we need to use > for HTML entity name or > for HTML entity number as a replacement.
- Ampersand(&): It is reserved for entities such as which is the HTML entity name for non-breaking space. Thus, it will have that reserved meaning for the whole text inside pre tag. To escape them in pre tag, we need to use & for HTML entity name or & for HTML entity number as a replacement.
- Single Quote or Apostrophe (‘): It is reserved for defining values to the attributes such as <div float:’left’> </div> and thus, will correspond to that meaning only in the whole code between pre tag. To escape them in pre tag, we need to use ' for HTML entity number as a replacement as this does not have any HTML entity name.
- Double Quotes (“): It is again reserved for defining values to the attributes such as <div class:”title”> </div> and thus, will correspond to that meaning only in the whole code between pre tag. To escape them in pre tag, we need to use " for HTML entity name or " for HTML entity number as a replacement.
Example:
<!DOCTYPE html> < html > < head > < title >Page Title</ title > </ head > < body > < center > < h2 style = "color: green;" >w3wiki</ h2 > < p >A Computer Science Portal for Beginner</ p > < pre > < h2 >w3wiki</ h2 > < p >A Computer Science Portal for Beginner</ p > </ pre > </ center > </ body > </ html > |
Output:
Some other special symbols which needed to be escaped along with their entity name and entity number are :
Symbols | Entity name | Entity Number |
---|---|---|
Non-Breaking Space ( ) | |   |
Registered Trademark (®) | ® | ® |
Copyright (©) | © | © |
Euro (€) | € | € |
Pound (£) | £ | £ |
Cent (¢) | ¢ | ¢ |