Q-1. What Are The New Features Introduced in HTML5? Answer
Q-1. What Are The New Features Introduced in HTML5? Answer
Answer.
HTML5 introduces a number of new elements and attributes that help in building an attractive
webSite, that we see nowadays.
New Semantic Elements – These are like <header>, <footer>, and <section>.
Forms 2.0 – It contains improvements to HTML web forms. It has introduced new attributes for
the <input> tag.
Persistent Local Storage – With HTML5, it is possible to achieve this, without resorting to third-
party plugins.
WebSocket – It facilitates setting up a bidirectional communication for web applications.
Server-Sent Events(SSE) – These events got introduced in HTML5. The direction of the flow of
the execution of these events is from the server to the Web Browser.
Canvas – It supports a two-dimensional drawing surface that is programmable using JavaScript.
Audio & Video – It allows embedding audio or video on the web pages without resorting to third-
party plugins.
Geolocation – It facilitates the visitors to share their physical location with the web application.
Microdata – It allows building our personal vocabulary beyond HTML5 and extends our web
pages with those custom semantics.
Drag and drop – It supports to Drag and drop the items from one location to another location on
the same Web page.
Q-2. What Would Happen If The HTML Document Does Not Contain
<!DOCTYPE>?
Answer.
It instructs the Web Browser about the version of HTML used for creating the Web page.
If the developer misses declaring the DOCTYPE information in the code, then new features and tags
provided by HTML5, like <article>, <footer>, and <header> will not be supported. Additionally, the
Browser may automatically go into Quirks or Strict Mode.
Q-5. What Are The Various Elements Provided By HTML5 For Media
Content?
Answer.
HTML provides the support of following elements for representing the media content.
Q-6. What Are The New Form Elements Made Available In HTML5?
Answer.
When we want to collect some data from the person visiting our site, we use HTML Forms. An
example is, the user has to enter his name, email id when he registers for the first time.
A form takes input from the site visitor and then posts it to a back-end application such as CGI, ASP
Script or PHP script. The back-end application will then perform required processing on the passed
data based on defined business logic inside the application.
There are various form elements available in earlier version of HTML like, text fields, text area fields,
drop-down menus, radio buttons, checkboxes, etc.
HTML5 provides the support of some new Form elements that are as follows.
A semantic element has its meaning expressed to both the browser and the developer. Also, all the
modern browsers support this feature. However, it is possible for a developer to train old browsers to
handle unknown elements.
Here is the list of some of the commonly used HTML5 Semantic Elements.
<article>
<figcaption>
<figure>
<header>
<footer>
<nav>
<section>
<article>
<aside>
<summary>
The Difference – Semantic Vs. Non-Semantic.
Semantic – These elements clearly describe their content like <img>, <form>, <table> etc.
Non-semantic – These elements are without any definition. They don’t describe anything about their
structure such as <span> and <div>.
Q-8. What Are The Various Tags Provided For Better Structuring In HTML5?
Answer.
The various tags provided for better structuring in HTML 5 are:
The new input types for forms provided by HTML5 are as follows.
<script>
document.createElement﴾"myElement"﴿
</script>
It can be used in the HTML as.
<myElement>hello</myElement>
With HTML4, developers have to either do complex Javascript programming or use other Javascript
frameworks like jQuery to achieve this functionality.
HTML 5 introduced a Drag and Drop (DnD) API that provides the support of all the fundamental DnD
operations to the browser. Thus making it easy for the developers to code it.
Latest versions of all the main browsers, like Chrome, Firefox, and Safari, support this DnD API of
HTML5.
dragstart – it gets triggered when the user starts dragging a draggable object.
dragenter – it gets triggered when the user has dragged the draggable element over the target
element.
drag – it gets fired when the object is getting dragged.
dragend – it gets fired when the user releases the mouse button after dragging an object.
dragleave – This event gets triggered when the mouse leaves an element while a drag is
occurring.
dragover – This event gets fired when the mouse moves over an element while a drag is
occurring.
It can store 5 to 10 MB data. That is far more than what cookies allow.
HTML5 never transfers Web storage data with any HTTP request. Thus creating less overhead
than cookies and in turn, increase the performance of the application.
Apps can work both online and offline.
This API is easy to learn and use.
Q-16. What Are The Different Types Of Web Storage Provided By HTML5?
Answer.
There are two types of Web Storage.
1. Session Storage
As its name indicates, it stores data of current session only. The data stored in Session Storage
clears on closing the browser.
The localStorage object stores the data without an expiry date. However, sessionStorage object
stores the data for only one session.
In the case of a localStorage object, data will not delete when the browser window closes.
However, the data gets deleted, if the browser window closes, in the case of sessionStorage
objects.
The data in sessionStorage is accessible only in the current window of the browser. But the
data in the localStorage can be shared between multiple windows of the browser.
Q-19. What Is The Concept Of Application Cache In HTML5? What Are Its
Advantages?
Answer.
HTML5 introduced the concept of Application Cache. It means that a web application is cached, and
is accessible without an internet connection.
Offline browsing – Users can use the application even when they are offline.
Speed – Cached resources load faster as compared to content that gets
downloaded, directly from the server.
Reduced server load – The browser will only download updated/modified resources from
the server.
CACHE MANIFEST – HTML5 performs the caching of files listed under this section after
they get downloaded for the first time.
NETWORK – Files listed here, always need a connection to the server. The browser can never
cache them.
FALLBACK – Files listed here specify the fallback pages, if any page in it is not accessible.
Q-21. What Is The Difference Between HTMl5 Application Cache And Regular
HTML Browser Cache?
Answer.
Following are the key differences between the two.
In AppCache, we can define all the assets the browser should cache in a manifest file (even the
entire site). For fetching this content, it is not necessary for the user to have accessed it
previously. In other words, Application Cache can prefetch pages that have not been visited at
all and are thereby unavailable in the regular browser cache. However, the browser cache will
only store the pages (and associated assets) the user has visited actually.
The AppCache allows web apps (and websites) to be made available offline, that too, with the
same speed benefits as the regular browser cache could provide only when the user is online.
A web worker is a script, which runs in the background. It exists in external files.
The user can perform actions like clicking, selecting things and so on. Meanwhile, the Web
worker runs in the background.
It is appropriate, to use Web worker for CPU intensive tasks.
Since Web workers are in external files, they do not have access to the following JavaScript objects.
Once the Web worker gets spawned, it starts the communication with the parent page, using the
postMessage() method. The Web worker, in turn, returns a message, that gets accessed using the
onmessage() event on the main page.
Let’s take an example, where the script spawns a Web worker to execute a loop having thousands
of iterations. After that, the Web worker returns the calculated value to the HTML page.
<script>
var worker = new Worker('calculateLoop.js');
worker.onmessage = function (event) {
alert("Completed " + event.data + "iterations" );
};
</script>
Below is the code of “calculateLoop.js” file. It makes use of postMessage() API, to pass the
communication back to the HTML page.
Q-23. What Are The New Attributes Provided In HTML5 For <Form>?
Answer.
The new attributes provided in HTML5 for <form> are.
autocomplete
It specifies if a form or an input field should have “autocomplete” feature set as on or off.
If autocomplete is set to on, it enables the browser to fill the values, based on the values
that the user starts to enter.
autocomplete works for input types like text, search, URL, tel, email, password, date
pickers, range, and color.
novalidate
It is a boolean attribute.
Its presence signifies that the form-data should not get validated at the time of submission.
<!DOCTYPE html>
<html>
<head>
<title>HTML Output Tag</title>
</head>
<body>
<form
oninput="sumresult.value=parseInt(val1.value)+parseInt(val2.value)+parseIn
t(val3.value)">
<input type="range" name="va1" value="10" /> +
<input type="number" name="val2" value="20" /> +
<input type="number" name="val3" value="40" /><br />
The output is: <output name="sumresult"></output>
</body>
</html>
The form attribute associates the <output> with a form. It displays the output as “70” on the web
page.
Q-24. What Are The New Attributes Provided In HTML5 For <Input> Element?
Answer.
Following are the new attributes provided in HTML5 for <input>.
autofocus
It is a Boolean attribute.
The presence of this attribute means that an <input> element should automatically come
into focus when the page gets loaded.
form
This attribute specifies about all the forms, to which a particular <input> element belongs.
formaction
This attribute defines the URL of a file, that will process the input control after the form gets
submitted.
This attribute is used along with type=”submit” and type=”image”.
Also, it overrides the action attribute of the <form> element.
formenctype
This attribute defines, the method to encode the form data before submitting it to the
server.
It gets used with type=”submit” and type=”image”.
Also, it overrides the enctype attribute of the <form> element.
formmethod
It defines the HTTP method used for sending form related data to the action URL.
It gets used with type=”submit” and type=”image”.
It overrides the method attribute of the <form> element.
formnovalidate
It is a boolean attribute.
It gets used with type= “submit”.
It indicates that the validation of the <input> element, should not be done at the time of
submission.
It overrides the novalidate attribute of the <form> element.
formtarget
It specifies a name or a keyword of the area where response received after submitting the
form will be displayed.
It gets used with type=”submit” and type=”image”.
height and width
It specifies the height and width of an <input> element.
It gets used only with <input type=”image”>.
list
It refers to a <datalist> element, which contains a list of pre-defined options for an <input>
element.
min and max
It specifies the minimum and maximum value for an <input> element.
It works with the following input types, number, range, date, datetime, datetime-local,
month, time, and week.
multiple
It is a boolean attribute.
It specifies that the user is allowed to enter more than one value in the <input> element.
It works with the following input types: email and file.
pattern
It specifies a regular expression with which the value of the <input> element gets
compared.
It works with the following input types: text, search, URL, tel, email, and password.
placeholder
It displays a short hint that indicates the expected value of an input field.
It works with the following input types: text, search, URL, tel, email, and password.
required
It is a boolean attribute.
It indicates that it is mandatory to fill the particular field, before submitting the form.
step
It specifies the legal number intervals for an <input> element.
It works with the following input types: number, range, date, datetime, datetime-local,
month, time, and week.
<!DOCTYPE HTML>
<html>
<body>
</body>
</html>
The HTML5 audio tag supports following attributes to direct the look and feel and various
functionalities of the control.
autoplay
It is a boolean attribute. If, the value is set the audio track starts playing automatically. The
System will not wait for data loading to complete.
autobuffer
It is a boolean attribute. If set, the audio will automatically begin buffering, even if the
automatic play is not enabled.
controls
If this attribute is present, it will allow the user to control audio playback, including volume,
seeking, and pause/resume playback.
loop
Setting this boolean attribute would automatically restart the audio from the beginning,
once it reaches to the end.
preload
This attribute specifies that the audio will be loaded at page load, and will be ready to run.
If autoplay is present, this attribute will not work.
src
It represents the URL of the audio to embed. Its presence is optional.
It must start with a <legend>tag because the <legend> tag defines the title of the fieldset.
Following is the Syntax of the <fieldset> tag in HTML5.
<fieldset>Controls</fieldset>
All the popularly known browsers provide the support for the <fieldset> tag.
disabled
Its value is disabled. It specifies, whether the fieldset will be displayed or not.
name
Its value is in the form of text. It defines the name of the fieldset.
form
Its value is the name of the form. It specifies the form related to the fieldset.
Let’s see an example, where we create a fieldset in a form. Here we use the <legend> tag to define
the heading for the fieldset.
<html>
<body>
<form>
<fieldset>
<legend>Personal Information</legend>
First Name: <input type="text" />
<br/><br/> Last Name: <input type="text" />
<br/><br/> person_Address: <input type="text" />
<br/><br/> person_Qualification: <input type="text" />
</fieldset>
</form>
</body>
</html>
Q-28. What Are The HTML Tags Which Get Deprecated In HTML5?
Answer.
Following are the tags that are deprecated in HTML5.
<basefont>
<big>
<center>
<font>
<s>
<strike>
<tt>
<u>
<frame>
<frameset>
<noframe>
<acronym>
<applet>
<isindex>
<dir>
Some attributes from HTML4 are no longer allowed in HTML5 since their functionality is better
handled by CSS. Below are some of the known attributes that got removed and the corresponding
impacted element.
Q-29. What Is A Meter Tag? What Is The Difference Between Progress Tag
And A Meter Tag?
Answer.
The <meter> tag defines a scalar measurement within a known range or a fractional value. We can
also call it a gauge.
Some of the items that can be represented using <meter> tag are Disk usage, the relevance of a
query result, and so on.
Note: The <meter> tag should not be used to indicate progress (as in a progress bar). For progress
bars, use the <progress> tag.
min
It is a number. It specifies the minimum value of the range.
max
It is a number. It specifies the maximum value of the range.
low
It is a number. It defines a range that represents <low> value.
high
It is a number. It defines a range that represents “high” value.
value
It is a number. It is a mandatory element. It defines the current value of the gauge.
optimum
It is a mandatory element with a numeric value. It specifies the optimum, or the best value,
for the element. If this value is higher than the “high” value, this indicates that the higher
the value, the better it is. If it’s lesser than the <low> mark, it means that the lower values
are better. If it is, in between, then it indicates that neither high nor low values are good.
form
It specifies one or more forms that define the <meter> element. It has value form_id.
However, there are some situations, where web pages require a long-term connection with the web
server. A typical example is stock quotes on finance websites where price update happens
automatically. Other examples are news feeds, sports results that run continuously on media
websites, Facebook/Twitter updates and so on.
We can achieve the above, using HTML5 using SSE. It enables a web page to hold an open
connection to the web server so that it can send a response automatically at any time. Thus there’s
no need to reconnect and run the same server script from scratch over and over again.
First, create a new EventSource object, and specify the URL of the page sending the updates
(in this example “sse_demo.php”).
Every time an update arrives, onmessage event gets triggered.
When an onmessage event occurs, it places the received data into the element that has <id =
result>.
Server-Side Code Example.
For the above example to work, we need a server capable of sending data updates. The server-side
event stream syntax is simple. Set the “Content-Type” header to “text/event-stream”. Now you can
start sending event streams. Following is the code (demo_sse.php).
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
Following is the explanation of the above code.
</body>
</html>
Example Explained
The <!DOCTYPE html> declaration defines this document to be HTML5
The <html> element is the root element of an HTML page
The <head> element contains meta information about the document
The <title> element specifies a title for the document
The <body> element contains the visible page content
The <h1> element defines a large heading
The <p> element defines a paragraph
HTML Tags
HTML tags are element names surrounded by angle brackets:
Tip: The start tag is also called the opening tag, and the end tag the closing tag.
Web Browsers
The purpose of a web browser (Chrome, IE, Firefox, Safari) is to read HTML documents and
display them.
The browser does not display the HTML tags, but uses them to determine how to display the
document:
HTML Page Structure
Below is a visualization of an HTML page structure:
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Note: Only the content inside the <body> section (the white area above) is displayed in a
browser.
It must only appear once, at the top of the page (before any HTML tags).
<!DOCTYPE html>
HTML Versions
Since the early days of the web, there have been many versions of HTML:
Version Year
HTML 1991
XHTML 2000
HTML5 2014
HTML Editors
Write HTML Using Notepad or TextEdit
Web pages can be created and modified by using professional HTML editors.
However, for learning HTML we recommend a simple text editor like Notepad (PC) or TextEdit
(Mac).
Follow the four steps below to create your first web page with Notepad or TextEdit.
Step 1: Open Notepad (PC)
Windows 8 or later:
Open the Start Screen (the window symbol at the bottom left on your screen). Type Notepad.
Windows 7 or earlier:
Also change some preferences to get the application to save files correctly. In Preferences >
Format > choose "Plain Text"
Then under "Open and Save", check the box that says "Display HTML files as HTML code
instead of formatted text".
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Step 3: Save the HTML Page
Save the file on your computer. Select File > Save as in the Notepad menu.
Name the file "index.htm" and set the encoding to UTF-8 (which is the preferred encoding for
HTML files).
You can use either .htm or .html as file extension. There is no difference, it is up to you.
Step 4: View the HTML Page in Your Browser
Open the saved HTML file in your favorite browser (double click on the file, or right-click - and
choose "Open with").
It is the perfect tool when you want to test code fast. It also has color coding and the ability to
save and share code with others:
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
HTML Documents
All HTML documents must start with a document type declaration: <!DOCTYPE html>.
The HTML document itself begins with <html> and ends with </html>.
The visible part of the HTML document is between <body> and </body>.
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
HTML Headings
HTML headings are defined with the <h1> to <h6> tags.
<h1> defines the most important heading. <h6> defines the least important heading:
Example
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
HTML Links
HTML links are defined with the <a> tag:
Example
<a href="https://www.w3schools.com">This is a link</a>
HTML Images
HTML images are defined with the <img> tag.
The source file (src), alternative text (alt), width, and height are provided as attributes:
Example
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">
HTML Buttons
HTML buttons are defined with the <button> tag:
Example
<button>Click me</button>
HTML Lists
HTML lists are defined with the <ul> (unordered/bullet list) or the <ol> (ordered/numbered
list) tag, followed by <li> tags (list items):
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
HTML Elements
An HTML element usually consists of a start tag and end tag, with the content inserted in
between:
The HTML element is everything from the start tag to the end tag:
<br>
HTML elements with no content are called empty elements. Empty elements do not have an
end tag, such as the <br> element (which indicates a line break).
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Example Explained
The <html> element defines the whole document.
<html>
<body>
</body>
</html>
The element content is two other HTML elements (<h1> and <p>).
<body>
</body>
<p>This is a paragraph
<p>This is a paragraph
The example above works in all browsers, because the closing tag is considered optional.
Never rely on this. It might produce unexpected results and/or errors if you forget
the end tag.
<br> is an empty element without a closing tag (the <br> tag defines a line break).
Empty elements can be "closed" in the opening tag like this: <br />.
HTML5 does not require empty elements to be closed. But if you want stricter validation, or
if you need to make your document readable by XML parsers, you must close all HTML
elements properly.
The HTML5 standard does not require lowercase tags, but W3C recommends lowercase in
HTML, and demandslowercase for stricter document types like XHTML.
HTML Attributes
All HTML elements can have attributes
Attributes provide additional information about an element
Attributes are always specified in the start tag
Attributes usually come in name/value pairs like: name="value"
Example
<a href="https://www.w3schools.com">This is a link</a>
Try it Yourself »
Example
<p style="color:red">I am a paragraph</p>
Declaring a language is important for accessibility applications (screen readers) and search
engines:
<!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
The first two letters specify the language (en). If there is a dialect, use two more letters
(US).
Example
<p title="I'm a tooltip">
This is a paragraph.
</p>
Use Lowercase Attributes
The HTML5 standard does not require lowercase attribute names.
The title attribute can be written with uppercase or lowercase like title or TITLE.
W3C recommends lowercase in HTML, and demands lowercase for stricter document
types like XHTML.
W3C recommends quotes in HTML, and demands quotes for stricter document types like
XHTML.
Sometimes it is necessary to use quotes. This example will not display the title attribute
correctly, because it contains a space:
Using quotes are the most common. Omitting quotes can produce errors.
In some situations, when the attribute value itself contains double quotes, it is necessary to
use single quotes:
Or vice versa:
<html>
<body>
</body>
</html>
Change the size of the image to 250 pixels wide and 400 pixels tall.
Exercise:
Transform the text below into a link that goes to
"https://www.w3schools.com".
<!DOCTYPE html>
<html>
<body>
This is a link
</body>
</html>
The image below does not exist. Specify the alternate text
"w3schools.com" for the image.
<body>
</body>
HTML Attributes
Below is an alphabetical list of some attributes often used in HTML:
Attribute Description
Alt Specifies an alternative text for an image, when the image cannot be displayed
A complete list of all attributes for each HTML element, is listed in our: HTML Attribute
Reference.
Bigger Headings
Each HTML heading has a default size. However, you can specify the size for any heading
with the style attribute, using the CSS font-size property:
The <hr> element is used to separate content (or define a change) in an HTML page:
The <head> element is a container for metadata. HTML metadata is data about the HTML
document. Metadata is not displayed.
The <head> element is placed between the <html> tag and the <body> tag:
<!DOCTYPE html>
<html>
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>
<body>
.
Metadata typically define the document title, character set, styles, links, scripts, and other
meta information.
<html>
<body>
<p>London is the capital city of England. It is the most populous city in the
United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
</body>
</html>
Add <h1>London</h1>
<html>
<body>
<h1>London</h1>
<p>London is the capital city of England. It is the most populous city in the
United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
</body>
</html>
Exercise:
Mark up the following text with appropriate tags:
"Universal Studios Presents" is the most important content.
"Jurassic Park" is the next most important content.
"About" is of lesser importance than Jurassic Park.
The last sentence is just a paragraph.
Jurassic Park
About
On the Island of Isla Nublar, a new park has been built: Jurassic Park is a theme park of cloned dinosaurs!!
</body>
</html>
You will learn more about HTML tags and attributes in the next chapters of this tutorial.
Tag Description
<head> A container for all the head elements (title, scripts, styles, meta information, and more)
Note: Browsers automatically add some white space (a margin) before and after a
paragraph.
HTML Display
You cannot be sure how HTML will be displayed.
Large or small screens, and resized windows will create different results.
With HTML, you cannot change the output by adding extra spaces or extra lines in your
HTML code.
The browser will remove any extra spaces and extra lines when the page is displayed:
Example
<p>
This paragraph
contains a lot of lines
in the source code,
but the browser
ignores it.
</p>
Don't Forget the End Tag
Most browsers will display HTML correctly even if you forget the end tag:
Use <br> if you want a line break (a new line) without starting a new paragraph:
Example
<p>This is<br>a paragraph<br>with line breaks.</p>
The <br> tag is an empty tag, which means that it has no end tag.
The text inside a <pre> element is displayed in a fixed-width font (usually Courier), and it
preserves both spaces and line breaks:
Example
<pre>
My Bonnie lies over the ocean.
HTML Styles
The HTML Style Attribute
Setting the style of an HTML element, can be done with the style attribute.
<tagname style="property:value;">
<!DOCTYPE html>
<html>
<body>
<p>I am normal</p>
</body>
</html>
HTML Background Color
The background-color property defines the background color for an HTML element.
<body style="background-color:powderblue;">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
HTML Fonts
The font-family property defines the font to be used for an HTML element:
HTML also defines special elements for defining text with a special meaning.
HTML uses elements like <b> and <i> for formatting output, like bold or italic text.
The HTML <b> element defines bold text, without any extra importance.
The HTML <strong> element defines strong text, with added semantic "strong" importance.
<body>
</html>
Tag Description
<p>WWF's goal is to: <q>Build a future where people live in harmony with
nature.</q></p>
he HTML <blockquote> element defines a section that is quoted from another source.
Marking abbreviations can give useful information to browsers, translation systems and
search-engines.
Will display
The <address> element is usually displayed in italic. Most browsers will add a line break
before and after the element.
<address>
Written by John Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</address>
The <address> element is usually displayed in italic. Most browsers will add a line break
before and after the element.
<address>
Written by John Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</address>