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.
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 (®)||®||®|
- Perl | Quoted, Interpolated and Escaped Strings
- How to place a div inside an iframe for IE ?
- Index inside map() Function
- How to dynamically load JS inside JS ?
- CSS | page-break-inside Property
- How to replace a word inside a string in PHP ?
- How to add Google map inside html page without using API key ?
- How to apply border inside a table ?
- How to add custom google search bar inside your web-page?
- Where is an object stored if it is created inside a block in C++?
- Add the slug field inside Django Model
- CSS to put icon inside an input element in a form
- How to count text lines inside of DOM element ?
- What purpose does a <script> tag serve inside of a <noscript> tag?
- How to vertically align text inside a flexbox using CSS?
- How to Cast a JSON Object Inside of TypeScript Class ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.