Thursday, 2 June 2016

HTML entities and special characters

HTML entities and special characters

HTML ENTITIES

Before we learn what HTML entities are, let's look at how the need for them came about. 
Try the following file in your HTML:
  1. <p>Welcome to the Introduction to HTML 5. The first tag we will be learning about is  the <html> tag.</p>
Screenshot of IntelXDK editor showing entities example
Did you notice the '<html>' tag is missing in your ouput? Basically, your browser mixed it up with an actual tag though you only meant it to be part of your sentence as text. 
Because of this kind of confusion, HTML reserves certain characters. If you want to use these characters in your HTML, you need to use character entities to display them. In layman terms, character entities are like substitutes. There are entities in HTML for several categories and these include:
  • currency symbols
  • copyright, trademark and registered symbol
  • general punctuation
  • arrows
  • mathematical symbols
  • Greek letters
All HTML entities have a name and number. They can be written using either name or number. 
If it is a name, an ampersand symbol '&' will precede it, followed by entity name and a semi-colon. Entity names are case sensitive. 
&entity_name;
If it is a number, an ampersand '&' symbol, followed by the number/hash symbol '#', entity number and a semi-colon.
&#entity_number;

SPECIAL CHARACTERS

You can find all HTML entities, their names and numbers here: https://dev.w3.org/html5/html-author/charref
But can you imagine replacing all your characters with entities? That is going to make coding in HTML5 very difficult. Apart from the five special characters listed in the table below, you don't have to specify entities for others like math, currency and copyright symbols.
But for these, you definitely need to replace them with entities:
SymbolEntity NameEntity NumberUsage
Less than '<'&lt;&#60;
Div tag: &lt;div>
Greater than '>'&gt;&#62;Div tag: &lt;div&gt;
Ampersand '&'&amp;&#38;Tom &amp; Jerry
Non breaking space ' ' - space that will not create a new line&nbsp;&#160;If you add multiple spaces, the browser will remove all but one. So you have to use this entity to add multiple spaces in your HTML page.
Quotes "&quot;&#34;
Link to a another section on the same page using id of the element: &lt;a href=&quot;&num;timetable&quot;&gt;
Displayed as: <a href="#timetable">
&quot; is generally encouraged for code. For an actual quotation, <q> or <blockquote> is preferred. 
We do not want these special characters to be processed by the browser as HTML code. Instead, you want it to be displayed to the user. So if you wish to display this in your browser:
<img src="images/test.png">
You have to write it like this in your HTML code:
&lt;img src=&quot;images/test.png&quot;&gt;
Image tag written using entities Intel XDK Output

0 comments:

Post a Comment

Social Profiles

Twitter Facebook Google Plus LinkedIn RSS Feed Email Pinterest

Popular Posts

Total Pageviews

Powered by Blogger.

Followers

Featured Posts

Copyright © Learn IT Skills | Powered by Blogger
Design by Lizard Themes | Blogger Theme by Lasantha - PremiumBloggerTemplates.com