HTML5 Essentials for Web Programming
HTML5 Essentials for Web Programming
MODULE-2
HTML5
2.1. Hello HTML5
The syntax of HTML5 should be mostly familiar.
A simple HTML5 document looks like this:
For all practical purposes, all that is different from standard HTML in this example is
the statement.
As indicated by its atypical statement, HTML5 is not defined as an SGML or XML
application.
As an example, consider the following flawed markup:
When checked with an HTML5 conformance checker, such as the W3C Markup
Validation Service used in this chapter (available at [Link] you see
the expected result:
These “mistakes” are actually allowed under traditional HTML and thus are allowed
under HTML5.
To ensure that conform to the HTML5 specification, should be concerned primarily
about the following:
Make sure to nest elements, not cross them; so
<b><i> is in error as tags cross </b></i>
whereas
<b><i> is in error as tags cross </i></b>
Quote attribute values when they are not ordinal values, particularly if they contain
special characters, particularly spaces; so
Understand and follow the content model. Just because one browser may let you use a
list item anywhere you like,
Latha Yadav T R, Asst. Prof., Dept. Of CSE( Data Science) VIT,
Bengaluru
5
Introduction to Web Programming (BPLCK205A)
Do not use invented tags unless they are included via some other markup language:
Encode special characters, particularly those used in tags (< >), either as an entity of a
named form, such as <, or as a numeric value, such as <.
2.3. XHTML5
HTML5 neither makes the clean markup you write non-conforming nor suggests that
you shouldn’t author markup this way.
If you want to pursue an “XMLish” approach to your document, HTML5 allows it.
Consider, for example, a strict XHTML example that is now HTML5:
XHTML5 usage clearly indicates that an HTML5 document written to XML syntax
must be served with the MIME type application/xhtml+xml or application/xml.
HTML5 makes one very major contribution: it defines what to do in the presence of
markup syntax problems.
The permissive nature of browsers is required for browsers to fix markup mistakes.
HTML5 directly acknowledges this situation and aims to define how browsers should
parse both well formed and malformed markup, as indicated by this brief excerpt from
the specification:
o This specification defines the parsing rules for HTML documents, whether
they are syntactically correct or not.
Certain points in the parsing algorithm are said to be parse errors. The error handling
for parse errors is well-defined:
o user agents must either act as described below when encountering such
problems, or must abort processing at the first error that they encounter for
which they do not wish to apply the rules.
HTML5’s aim to bring order to the chaos of sloppy markup is but one of the grand
aims of the specification.
It also aims to replace traditional HTML, XHTML, and DOM specifications, and to
do so in a backward-compatible fashion.
In its attempt to do this, the specification is sprawling, addressing not just what
elements exist but how they are used and scripted.
HTML5 embraces the fact that the Web not only is composed of documents but also
supports applications, thus markup must acknowledge and facilitate the building of
such applications.
Looking at Table 2-1, notice that some elements that apparently should be eliminated
somehow live on.
For example, continues to be allowed, but is obsolete.
The idea here is to preserve elements but shift meaning. For example, is no longer
intended to correspond to text that is just reduced in size, similar to or , but instead is
intended to represent the use of small text, such as appears in fine print or legal
information.
Table 2-2 presents the meaning-changed elements that stay put in HTML5 and their
new meaning.
Table 2-6 provides a brief overview of these attributes. We’ll take a look at many of
these in upcoming sections and a complete reference for all is found in the next
chapter.
Some of them, such as reversed for use on ordered lists ( ), are a long time in coming,
while others simply add polish, or address details that few page authors may notice.
Similarly, a footer element is provided for document authors to define the footer
content of a document, which often contains navigation, legal, and contact
information:
The actual content to be placed in a tag may be enclosed in div, p, or other block
elements, as illustrated by this simple example:
The HTML5 structural element with the most possible uses is the section element.
A particular tag can be used to group arbitrary content together and may contain
further tags to create the idea of subsections.
The example illustrates the basic use of HTML5 sections:
But a section element may contain header and footer elements of its own:
For example, the expanded example here shows a number of sections with headers,
footers, headlines, and content:
HTML5–compliant browsers should take this markup and define an outline based
upon the use of headers, like so:
For example, the first header really was not two levels of sectioning but simply one
with a subhead. To address this outlining, you would take this markup:
Given these semantics, it is clear that HTML5 sectioning elements are not just a
formalization of tags with appropriate class values. For example,
For example, the article element is used to define a discrete unit of content such as a
blog post, comment, article, and so on. For example, the following defines a few
individual blog posts in a document:
The idea of defining these discrete content units specially is that to extract them
automatically, so again, having defined elements as opposed to some ad hoc use of
class names on tags is preferred.
HTML5 also introduces an aside element, which may be used within content to
represent material that is tangential or, as the element name suggests, an aside:
Noted that an tag was used in the aside. While not required, it is useful as a reminder
to readers that aside elements serve as outline sectioning elements, as shown here
The element should contain a date/time value that is in the format YYYY-MM-
DDThh:mm:ss TZD, where the letters correspond to years, months, days, hours,
minutes, and seconds, T is the actual letter ‘T,’ and ZD represents a time zone
designator of either Z or a value like +hh:mm to indicate a time zone offset.
To provide both human- and machine-friendly date/time content, the element supports
a datetime attribute, which should be set to the previously mentioned date format of
YYYY-MM-DDThh:mm:ssTZD.
Acting as a semantic element, figure simply groups items within an enclosed tag,
though it may associate them with a caption defined by a tag as shown in the example.
Specifying Navigation One new HTML5 element that is long overdue is the nav
element.
The purpose of this element is to encapsulate a group of links that serves as a
collection of offsite links, document navigation, or site navigation:
Many Web developers have used and tags to encapsulate navigation and then styled
the elements appropriately as menu items.
Traditionally, multimedia has been inserted with the embed and object elements,
particularly when inserting Adobe Flash, Apple QuickTime, Windows Media, and
other formats.
HTML5 brings this concept of tag-based multimedia.
<video>
To insert video, use a tag and set its src attribute to a local or remote URL
containing a playable movie.
Also display playblack controls by including the controls attribute, as well as set
the dimensions of the movie to its natural size.
This simple demo shows the use of the new element:
HTML5 open video has, as it currently stands, brought back the madness of media
codec support that Flash solved, albeit in a less than stellar way.
To address the media support problem, you need to add in alternative formats to
use by including a number of <source> tags:
Also note in the preceding snippet the use of the poster attribute, which is set to
display an image in place of the linked object in case it takes a few moments to load.
Latha Yadav T R, Asst. Prof., Dept. Of CSE( Data Science) VIT,
Bengaluru
22
Introduction to Web Programming (BPLCK205A)
Other video element specific attributes like autobuffer can be used to advise the
browser to download media content in the background to improve playback, and
autoplay, which when set, will start the media as soon as it can.
A complete example of the video element in action is shown here:
<audio>
HTML5’s audio element is quite similar to the video element. The element should
support common sound formats such as WAV files:
In this manner, the audio element looks pretty much the same as Internet Explorer’s
proprietary bgsound element. Having the fallback content rely on that proprietary tag
might not be a bad idea:
To allow the user to control sound play, unless you have utilized JavaScript to control
this, you may opt to show controls with the same named attribute.
Latha Yadav T R, Asst. Prof., Dept. Of CSE( Data Science) VIT,
Bengaluru
23
Introduction to Web Programming (BPLCK205A)
Depending on the browser, these controls may look quite different, as shown next.
As with the video element, also have autobuffer and autoplay attributes for the
audio element.
Unfortunately, just like video, there are also audio format support issues, so may want
to specify different formats using <source> tags:
Note the alternative content placed within the element for browsers that don’t support
the element.
After place a <canvas> tag in a document, the next step is to use JavaScript to access
and draw on the element.
For example, the following fetches the object by its id value and creates a two-
dimensional drawing context:
The strokeRect (x, y, width, height) method takes x and y coordinates and height and
width, all specified as numbers representing pixels. For example,
context. strokeRect(10,10,150,50);
Draw a simple rectangle of 150 pixels by 50 pixels starting at the coordinate 10,10
from the origin of the placed <canvas> tag.
To set a particular color for the stroke, you might set it with the strokeStyle() method,
like so:
By default, the fill color will be black, but you can define a different fill color by
using the fillColor() method. As a demonstration this example sets a light red color:
[Link] = "rgb(218,0,0)";
CSS color functions, which may include opacity; for example, here the opacity of the
reddish fill is set to 40 percent:
[Link] = "rgba(218,112,214,0.4)";
In addition to the CSS color values, you can also set the fillColor to a gradient object.
A gradient object can be created by using createLinearGradient() or
createRadialGradient(). The following example creates a simple linear gradient that
will be applied to a rectangle using the createLinearGradient(x1,y1,x2,y2) method.
The gradient is positioned at 10,150 and is set to go 200 pixels in both directions.
var lg = [Link](10,150,200,200);
The gradient colors are added using the addColorStop() method.
This specifies a color and the offset position in the gradient where the color should
occur.
The offset must be between 0 and 1.
Finally, the fillColor is set to the gradient. Here is the complete code snippet,
followed by a visual example:
The complete example, drawing a few different shapes with fills and styles, is
presented here:
The lineTo() call is used to create three sides of the cube, but the points set are not
straight horizontal and vertical lines as we see when we make 2D squares.
Shading is applied to give the illusion of dimensionality because of the application of
a light source.
While the code here is pretty simple, you can see that using canvas properly is often a
function:
Drawing on canvas isn’t limited to simple lines; it is also possible to create curved
lines using arc(), arcTo(), quadraticCurveTo(), and bezierCurveTo().
To illustrate these methods, this section shows how to draw a simple face.
Use the arc(x,y,radius, startAngle, endAngle, counterclockwise) method to draw
circles and parts of circles. Its location is defined by the point of its center (x,y) as
well as the circle’s radius.
How much of the circle is drawn is defined by startAngle and endAngle, in radians.
The direction of the curve is set by a Boolean value, which is the final parameter
specified by counterclockwise.
To start face drawing, use arc() to draw the head as a circle:
The x parameter shows how much to scale in the horizontal direction and the y
parameter indicates how much to scale vertically.
The translate(x,y) function is a handy function to use to change the origin from (0,0)
to another location in the drawing.
The following example moves the origin to (100,100).
Then, when the start coordinates of the rectangle are specified at (0,0), it really starts
at (100,100).
Use drawImage(img,x,y,w,h) if you need to modify the image size and set the width
and height.
The actual image passed in to the drawImage() method can come from a few places. It
can be
• An image already loaded on the page
• Dynamically created through the DOM
• Another canvas object
• An image created by setting its src to a data: URL
The important thing to remember is that the image must be loaded by the time canvas
is ready to access it.
This may require use of the onload function for the image:
The following example loads an image and draws a region in preparation for
eventually adding a caption:
A variety of date controls can now be directly created by setting the type attribute to
date, datetime, datetime-local, month, week, or time.
Several of these controls are demonstrated here:
Setting type to number gives you a numeric spin box in conforming browsers:
The max attribute can be set to limit the maximum value, min to limit the smallest
value, and even step to indicate how values may be modified. For example,
<input>
It is also possible to further define semantic restrictions by setting an tag’s
type attribute to tel, email, or url:
The pattern attribute also can be employed to force the entered data to conform to a
supplied regular expression:
Under HTML5, the input element’s list attribute is used to set the DOM id of a
datalist element used to provide a predefined list of options suggested to the user for
entry:
HTML5 also introduces the autofocus attribute, which when placed on a field should
cause a supporting browser to immediately focus this field once the page is loaded:
To display the autocomplete suggestions provided for fields if similar field names
have been used in the past:
Repurposed One element that will be implemented in browsers but might not
perform the actions defined in HTML5 is the menu element.
Traditionally, this element was supposed to be used to create a simple menu for
choices, but most browsers simply rendered it as an unordered list:
A new attribute, type, is introduced that takes a value of toolbar, context, or list
(the default).
This example sets up a simple File menu for a Web application:
With menu, it would also be possible to define a context menu, usually invoked by
a right-click:
command Element
The menu element may contain not just links but also other interactive items,
including the newly introduced command element.
This empty element takes a label and may have an icon decoration as well.
The command element has a type attribute, which may be set to command, radio,
or checkbox, though when radio is employed there needs to be a radiogroup
indication.
A simple example here with the repurposed menu element should illustrate the
possible use of this element:
details Element
The new details element is supposed to represent some form of extra details, such
as a tooltip or revealed region that may be shown to a user.
The details element can contain one dt element to specify the summary of the
details as well as one dd element to supply the actual details.
The attribute open can be set to reveal the details or can be changed dynamically,
as shown in this example:
output Element
The final stop on this speculative tour is the output element, which is used to
define a region that will be used as output from some calculation or form control.
Here using the calendar picker and having the eventual release date of HTML5
being revealed in an output element:
The following events are attached to the item that will be dragged:
contenteditable Attribute
HTML5 standardizes the use of this attribute globally on all elements.
The basic sense of the attribute is to set true, to modify the text directly when it click
the element:
spellcheck Attribute
HTML5 defines a spellcheck attribute globally for elements.
Interestingly, some browsers such as Firefox have supported spell checking of form
fields and elements in content editing mode using the contenteditable attribute for
some time.
HTML5 makes this attribute standard. Enabling the spell checking of element content
is a matter of setting the spellcheck attribute to true: