HTML-5
- Get link
- X
- Other Apps
Chapter:26
Introduction to HTML5
HTML5
When writing HTML5 documents, one of the first new features that you'll notice is the doc type declaration:
The character encoding (charset) declaration is also simplified:
New Elements in HTML5
<article>, <aside>, <audio>, <canvas>, <datalist>, <details>, <embed>, <footer>, <header>, <nav>, <output>, <progress>, <section>, <video>, and even more!
HTML5
Drag and drop from the options below to create a valid HTML5 doctype:
<
>
<html>
<head>
<title>Title</title>
</head>
<body>
The content of the document
</body>
</html>
New in HTML5
Forms
- The Web Forms 2.0 specification allows for creation of more powerful forms and more compelling user experiences.
- Date pickers, color pickers, and numeric stepper controls have been added.
- Input field types now include email, search, and URL.
- PUT and DELETE form methods are now supported.
Integrated API (Application Programming Interfaces)
- Drag and Drop
- Audio and Video
- Offline Web Applications
- History
- Local Storage
- Geolocation
- Web Messaging
Tap the Continue button to begin!
New in HTML5
Which element/feature is not new in HTML5?
Chapter:27
Content Models
The List of Content Models
In HTML, elements typically belonged in either the block level or inline content model. HTML5 introduces seven main content models.
- Metadata
- Embedded
- Interactive
- Heading
- Phrasing
- Flow
- Sectioning
How many content models does HTML5 offer?
Content Models
Metadata: Content that sets up the presentation or behavior of the rest of the content. These elements are found in the head of the document.
Elements: <base>, <link>, <meta>, <noscript>, <script>, <style>, <title>
Embedded: Content that imports other resources into the document.
Elements: <audio>, <video>, <canvas>, <iframe>, <img>, <math>, <object>, <svg>
Interactive: Content specifically intended for user interaction.
Elements: <a>, <audio>, <video>, <button>, <details>, <embed>, <iframe>, <img>, <input>, <label>, <object>, <select>, <textarea>
Heading: Defines a section header.
Elements: <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <hgroup>
Phrasing: This model has a number of inline level elements in common with HTML4.
Elements: <img>, <span>, <strong>, <label>, <br />, <small>, <sub>, and more.
Content Models
Where is the metadata located in an HTML5 document?
Content Models
Flow content: Contains the majority of HTML5 elements that would be included in the normal flow of the document.
Sectioning content: Defines the scope of headings, content, navigation, and footers.
Elements: <article>, <aside>, <nav>, <section>
Content Models
Which content model contains almost all of the others?
Chapter:28
HTML5 Page Structure
Page Structure in HTML5
A generic HTML5 page structure looks like this:
Page Structure in HTML5
Steps to create a generalized HTML5 page structure.
Chapter:29
Header, Nav & Footer
The <header> Element
In HTML4, we would define a header like this:
The <header> element is appropriate for use inside the body tag.
<html>
<head></head>
<body>
<header>
<h1> Most important heading </h1>
<h3> Less important heading </h3>
</header>
</body>
</html>
The <header> Element
The header element is appropriate to use...
The <footer> Element
The footer element is also widely used. Generally we refer to a section located at the very bottom of the web page as the footer.
- Contact Information
- Privacy Policy
- Social Media Icons
- Terms of Service
- Copyright Information
- Sitemap and Related Documents
The <footer> Element
Which tag specifies the footer element?
The <nav> Element
This tag represents a section of a page that links to other pages or to certain sections within the page. This would be a section with navigation links.
Here is an example of a major block of navigation links:
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">About us</a></li>
</ul>
</nav>
The <nav> Element
Followings are the sections to create a generic HTML5 page structure:
Chapter:30
Article, Section & Aside
The <article> Element
Article is a self-contained, independent piece of content that can be used and distributed separately from the rest of the page or site. This could be a forum post, a magazine or newspaper article, a blog entry, a comment, an interactive widget or gadget, or any other independent piece of content.
The <article> element replaces the <div> element that was widely used in HTML4, along with an id or class.
<h1>The article title</h1>
<p>Contents of the article element </p>
</article>
The <article> Element
Which element was usually used in HTML4 instead of the article tag?
The <section> Element
<section> is a logical container of the page or article.
Sections can be used to divide up content within an article.
For example, a homepage could have a section for introducing the company, another for news items, and still another for contact information.
Each <section> should be identified, typically by including a heading (h1-h6 element) as a child of the <section> element.
<h1>Welcome</h1>
<section>
<h1>Heading</h1>
<p>content or image</p>
</section>
</article>
The <section> Element
The section element should be used only inside an article element.
The <aside> Element
<aside> is secondary or tangential content which could be considered separate from but indirectly related to the main content.
This type of content is often represented in sidebars.
When an <aside> tag is used within an <article> tag, the content of the <aside> should be specifically related to that article.
<h1> Gifts for everyone </h1>
<p> This website will be the best place for choosing gifts </p>
<aside>
<p> Gifts will be delivered to you within 24 hours </p>
</aside>
</article>
The <aside> Element
The aside element is used to define:
Chapter:31
The Audio Element
Audio on the Web
Before HTML5, there was no standard for playing audio files on a web page.
The HTML5 <audio> element specifies a standard for embedding audio in a web page.
There are two different ways to specify the audio source file's URL. The first uses the source attribute:
Audio element not supported by your browser
</audio>
The second way uses the <source> element inside the <audio> element:
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
</audio>
Audio on the Web
Fill in the blanks to add an audio element:
Audio on the Web
The <audio> element creates an audio player inside the browser.
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
Audio element not supported by your browser.
</audio>
Result:
Audio on the Web
The text in between the audio tags is:
Attributes of <audio>
controls
Specifies that audio controls should be displayed (such as a play/pause button, etc.)
autoplay
When this attribute is defined, audio starts playing as soon as it is ready, without asking for the visitor's permission.
loop
This attribute is used to have the audio replay every time it is finished.
Attributes of <audio>
Which of the following is not an attribute of the audio element?
Chapter:32
The Video Element
Videos in HTML
The video element is similar to the audio element.
You can specify the video source URL using an attribute in a video element, or using source elements inside the video element:
<source src="video.mp4" type="video/mp4">
<source src="video.ogg" type="video/ogg">
Video is not supported by your browser
</video>
Videos in HTML
Fill in the blanks to create a video element:
Attributes of <video>
Another aspect shared by both the audio and the video elements is that each has controls, autoplay and loop attributes.
In this example, the video will replay after it finishes playing:
<source src="video.mp4" type="video/mp4">
<source src="video.ogg" type="video/ogg">
Video is not supported by your browser
</video>
Chapter:33
The Progress Element
Progress Bar
The <progress> element provides the ability to create progress bars on the web.
The progress element can be used within headings, paragraphs, or anywhere else in the body.
Progress Element Attributes
Value: Specifies how much of the task has been completed.
Max: Specifies how much work the task requires in total.
Example:
</progress>
Result:
Progress Bar
Define a progress bar that shows 63 percent of progress:
Chapter:34
Web Shortage API
HTML5 Web Storage
With HTML5 web storage, websites can store data on a user's local computer.
Before HTML5, we had to use JavaScript cookies to achieve this functionality.
The Advantages of Web Storage
- More secure
- Faster
- Stores a larger amount of data
- Stored data is not sent with every server request
HTML5 Web Storage
Before HTML5, application data was stored in:
Types of Web Storage Objects
There are two types of web storage objects:
- sessionStorage()
- localStorage()
Local vs. Session
- Session Storage is destroyed once the user closes the browser
- Local Storage stores data with no expiration date
Working with Values
The syntax for web storage for both local and session storage is very simple and similar.
The data is stored as key/value pairs.
Storing a Value:
Getting a Value:
alert(localStorage.getItem("key1"));
Removing a Value:
Removing All Values:
Chapter:35
Geolocation API
What is the Geolocation API?
In HTML5, the Geolocation API is used to obtain the user's geographical location.
Since this can compromise user privacy, the option is not available unless the user approves it.
What is the Geolocation API?
With the Geolocation API, you can obtain...
Using HTML Geolocation
The Geolocation API’s main method is getCurrentPosition, which retrieves the current geographic location of the user's device.
Parameters:
showLocation (mandatory): Defines the callback method that retrieves location information.
ErrorHandler(optional): Defines the callback method that is invoked when an error occurs in processing the asynchronous call.
Options (optional): Defines a set of options for retrieving the location information.
Using HTML Geolocation
Which choice is the mandatory parameter of the getCurrentPosition() method?
Presenting Data
User location can be presented in two ways: Geodetic and Civic.
1. The geodetic way to describe position refers directly to latitude and longitude.
2. The civic representation of location data is presented in a format that is more easily read and understood by the average person.
Each parameter has both a geodetic and a civic representation:
Chapter:36
Drag & Drop API
Making Elements Draggable
The drag and drop feature lets you "grab" an object and drag it to a different location.
To make an element draggable, just set the draggable attribute to true:
Example:
<html>
<head>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>
<div id="box" ondrop="drop(event)"
ondragover="allowDrop(event)"
style="border:1px solid black;
width:200px; height:200px"></div>
<img id="image" src="sample.jpg" draggable="true"
ondragstart="drag(event)" width="150" height="50" alt="" />
</body>
</html>
When the element is dragged, the ondragstart attribute calls a function, drag(event), which specifies what data is to be dragged.
The dataTransfer.setData() method sets the data type and the value of the dragged data:
ev.dataTransfer.setData("text", ev.target.id);
}
Where to Drop
The ondragover event specifies where the dragged data can be dropped. By default, data and elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element.
This is done by calling the event.preventDefault() method for the ondragover event.
Do the Drop
When the dragged data is dropped, a drop event occurs.
In the example above, the ondrop attribute calls a function, drop(event):
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
The dragged data can be accessed with the dataTransfer.getData() method. This method will return any data that was set to the same type in the setData() method.
The dragged data is the ID of the dragged element ("image").
At the end, the dragged element is appended into the drop element, using the appendChild() function.
Making Elements Draggable
How many times can HTML5 events be fired?
Chapter:37
SVG
Drawing Shapes
SVG stands for Scalable Vector Graphics, and is used to draw shapes with HTML-style markup.
It offers several methods for drawing paths, boxes, circles, text, and graphic images.
Drawing Shapes
What does SVG stand for?
Inserting SVG Images
An SVG image can be added to HTML code with just a basic image tag that includes a source attribute pointing to the image:
Inserting SVG Images
Fill in the blanks to add "my.svg" to the page:
Drawing a Circle
To draw shapes with SVG, you first need to create an SVG element tag with two attributes: width and height.
To create a circle, add a <circle> tag:
<circle cx="80" cy="80" r="50" fill="green" />
</svg>
- cx pushes the center of the circle further to the right of the screen
- cy pushes the center of the circle further down from the top of the screen
- r defines the radius
- fill determines the color of our circle
- stroke adds an outline to the circle
Result:
Drawing a Circle
Fill in the blanks to create a red circle at the position X=50, Y=240:
Other Shape Elements
<rect> defines a rectangle:
<rect width="300" height="100"
x="20" y="20" fill="green" />
</svg>
The following code will draw a green-filled rectangle.<line> defines a line segment:
<line x1="10" y1="10" x2="200" y2="100"
style="stroke:#000000; stroke-linecap:round;
stroke-width:20" />
</svg>
(x1, y1) define the start coordinates(x2, y2) define the end coordinates.<polyline> defines shapes built from multiple line definitions:
<polyline style="stroke-linejoin:miter; stroke:black;
stroke-width:12; fill: none;"
points="100 100, 150 150, 200 100" />
</svg>
Points are the polyline's coordinates.
The code below will draw a black check sign:
Other Shape Elements
Fill in the blanks to add a line to the page, starting from coordinates 10, 20 and ending at 50, 100:
<ellipse> and <polygon>
Ellipse
The <ellipse> is similar to the <circle>, with one exception:
You can independently change the horizontal and vertical axes of its radius, using the rx and ry attributes.
<ellipse cx="200" cy="100" rx="150" ry="70" style="fill:green" />
</svg>
Result:
Polygon
The <polygon> element is used to create a graphic with at least three sides. The polygon element is unique because it automatically closes off the shape for you.
<polygon points="100 100, 200 200, 300 0"
style="fill: green; stroke:black;" />
</svg>
Result:
<ellipse> and <polygon>
Which of the following is an HTML5 SVG shape?
Chapter:38
SVG Animations & Paths
Shape Animations
SVG animations can be created using the <animate> element.
The example below creates a rectangle that will change its position in 3 seconds and will then repeat the animation twice:
<rect width="150" height="150" fill="orange">
<animate attributeName="x" from="0" to="300"
dur="3s" fill="freeze" repeatCount="2"/>
</rect>
</svg>
from: Specifies the starting value of the attribute
to: Specifies the ending value of the attribute
dur: Specifies how long the animation runs (duration)
fill: Specifies whether or not the attribute's value should return to its initial value when the animation is finished (Values: "remove" resets the value; "freeze" keeps the “to value”)
repeatCount: Specifies the repeat count of the animation
In the example above, the rectangle changes its x attribute from 0 to 300 in 3 seconds.
Shape Animations
Which tag is used to create shape animations?
Paths
The <path> element is used to define a path.
The following commands are available for path data:
M: moveto
L: lineto
H: horizontal lineto
V: vertical lineto
C: curveto
S: smooth curveto
Q: quadratic Bézier curve
T: smooth quadratic Bézier curveto
A: elliptical Arc
Z: closepath
Define a path using the d attribute:
<path d="M 0 0 L200 200 L200 0 Z" style="stroke:#000; fill:none;" />
</svg>
M places our "virtual pen" down at the position 0, 0. It then moves 200px down and to the right, then moves up to the position 200, 0. The Z command closes the shape, which results in a hypotenuse:
Paths
Which shape is indicated by the following path? <path d="M0 0 L0 100 L100 100 L100 0 Z" />
Chapter:39
Canvas
The <canvas> Element
The HTML canvas is used to draw graphics that include everything from simple lines to complex graphic objects.
The <canvas> element is defined by:
</canvas>
<head></head>
<body>
<canvas id="canvas1"
width="400" height="300"></canvas>
<script>
var can = document.getElementById("canvas1");
var ctx = can.getContext("2d");
</script>
</body>
</html>
The <canvas> Element
You can draw on the canvas using...
Canvas Coordinates
The HTML canvas is a two-dimensional grid.
The upper-left corner of the canvas has the coordinates (0,0).
X coordinate increases to the right.
Y coordinate increases toward the bottom of the canvas.
Canvas Coordinates
X and Y are...
Drawing Shapes
The fillRect(x, y, w, h) method draws a "filled" rectangle, in which w indicates width and h indicates height. The default fill color is black.
A black 100*100 pixel rectangle is drawn on the canvas at the position (20, 20):
var ctx=c.getContext("2d");
ctx.fillRect(20,20,100,100);
Result:The fillStyle property is used to set a color, gradient, or pattern to fill the drawing.
Using this property allows you to draw a green-filled rectangle.
var ctx=canvas.getContext("2d");
ctx.fillStyle ="rgba(0, 200, 0, 1)";
ctx.fillRect (36, 10, 22, 22);
Result:The canvas supports various other methods for drawing:
Draw a Line
moveTo(x,y): Defines the starting point of the line.
lineTo(x,y): Defines the ending point of the line.
Draw a Circle
beginPath(): Starts the drawing.
arc(x,y,r,start,stop): Sets the parameters of the circle.
stroke(): Ends the drawing.
Gradients
createLinearGradient(x,y,x1,y1): Creates a linear gradient.
createRadialGradient(x,y,r,x1,y1,r1): Creates a radial/circular gradient.
Drawing Text on the Canvas
Font: Defines the font properties for the text.
fillText(text,x,y): Draws "filled" text on the canvas.
strokeText(text,x,y): Draws text on the canvas (no fill).
Drawing Shapes
fillRect (36,10,22,12) indicates a rectangle with a height of...
Chapter:40
SVG vs. Canvas
Canvas vs. SVG
Canvas
- Elements are drawn programmatically
- Drawing is done with pixels
- Animations are not built in
- High performance for pixels-based drawing operations
- Resolution dependent
- No support for event handlers
- You can save the resulting image as .png or .jpg
- Well suited for graphic-intensive games
SVG
- Elements are part of the page's DOM (Document object model)
- Drawing is done with vectors
- Effects, such as animations are built in
- Based on standard XML syntax, which provides better accessibility
- Resolution independent
- Support for event handlers
- Not suited for game applications
- Best suited for applications with large rendering areas (for example, Google Maps)
However, you cannot just draw SVG onto a canvas, or vice-versa.
Chapter:41
Canvas transformation
Working with Canvas
The Canvas element can be transformed. As an example, a text is written on the canvas at the coordinates (10, 30).
ctx.textAlign="start";
ctx.fillText("start", 10, 30);
x indicates how far to move the grid horizontally, and y indicates how far to move the grid vertically.
ctx.fillText("after translate", 10, 30);
Result:
Working with Canvas
Which method is used to move the canvas element?
The rotate() Method
The rotate() method is used to rotate the HTML5 Canvas. The value must be in radians, not degrees.
Here is an example that draws the same rectangle before and after rotation is set:
ctx.fillRect(10,10, 100, 100);
ctx.rotate( (Math.PI / 180) * 25); //rotate 25 degrees.
ctx.fillStyle = "#0000FF";
ctx.fillRect(10,10, 100, 100);
The rotate() Method
The rotation parameter is in:
The scale() Method
The scale() method scales the current drawing. It takes two parameters:
- The number of times by which the image should be scaled in the X-direction.
- The number of times by which the image should be scaled in the Y-direction.
ctx =canvas.getContext('2d');
ctx.font="bold 22px Tahoma";
ctx.textAlign="start";
ctx.fillText("start", 10, 30);
ctx.translate(100, 150);
ctx.fillText("after translate", 0, 0);
ctx.rotate(1);
ctx.fillText("after rotate", 0, 0);
ctx.scale(1.5, 4);
ctx.fillText("after scale", 0,20);
This will scale the canvas 1.5 times in the X-direction, and 4 times in Y-direction:
The scale() Method
What mRukdecrease the size of the current drawing?
Chapter:42
HTML5 Forms, Part 1
HTML5 Forms
HTML5 brings many features and improvements to web form creation. There are new attributes and input types that were introduced to help create better experiences for web users.
Form creation is done in HTML5 the same way as it was in HTML4:
<label>Your name:</label>
<input id="user" name="username" type="text" />
</form>
New Attributes
HTML5 has introduced a new attribute called placeholder. On <input> and <textarea> elements, this attribute provides a hint to the user of what information can be entered into the field.
<label for="email">Your e-mail address: </label>
<input type="text" name="email" placeholder="email@example.com" />
</form>
<label for="email">Your e-mail address: </label>
<input type="text" name="email" autofocus/>
</form>
Forms with Required Fields
The "required" attribute is used to make the input elements required.
<label for="e-mail">Your e-mail address: </label>
<input name="Email" type="text" required />
<input type="submit" value="Submit"/>
</form>
The form will not be submitted without filling in the required fields.
Result:
When autocomplete is on, the browser automatically complete values based on values that the user has entered before.
HTML5 added several new input types:
- color
- date
- datetime
- datetime-local
- email
- month
- number
- range
- search
- tel
- time
- url
- week
New input attributes in HTML5:
- autofocus
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- height and width
- list
- min and max
- multiple
- pattern (regexp)
- placeholder
- required
- step
Forms with Required Fields
Designate the username field as required, and focus on the name field when the page loads:
Chapter:43
HTML5 Forms, Part 2
Creating a Search Box
The new search input type can be used to create a search box:
Creating a Search Box
Fill in the blank to create a Search Box:
Search Options
The <datalist> tag can be used to define a list of pre-defined options for the search field:
<datalist id="colors">
<option value="Red">
<option value="Green">
<option value="Yellow">
</datalist>
Search Options
Fill in the blanks to associate the input with the datalist:
Creating More Fields
Some other new input types include email, url, and tel:
<br />
<input id="url" name="url" type="url" placeholder="example.com" />
<br />
<input id="tel" name="tel" type="tel" placeholder="555.555.1211" />
Creating More Fields
Which of the following is not a supported type for the input tag?
Chapter:44
Model Quiz
Which choice is the correct HTML5 element for playing video files?
The <canvas> element in HTML5 is used to:
Which tag contains the navigation?
sessionStorage stores data for the duration of how many session(s)?
What shape results from the following code? <svg width="100" height="100"> <line x1="50" y1="0" x2="50" y2="100" style="stroke:black" /> <line x1="0" y1="50" x2="100" y2="50" style="stroke:black" /> </svg>
in the blanks to turn off auto complete and require the password field.
- Get link
- X
- Other Apps







Comments
Post a Comment