HTML
HTML
HTML
Unlike a scripting or programming language that uses scripts to perform functions, a markup language uses
tags to identify content.
The ability to code using HTML is essential for any web professional. Acquiring this skill should be the starting
point for anyone who is learning how to create content for the web.
HTML: Structure
CSS: Presentation
JavaScript: Behavior
The <html> Tag
Although various versions have been released over the years, HTML basics remain the same.
The structure of an HTML document has been compared with that of a sandwich. As a sandwich has two
slices of bread, the HTML document has opening and closing HTML tags.
The <head> Tag
Immediately following the opening HTML tag, you'll find the head of the document, which is identified by
opening and closing head tags.
The head of an HTML file contains all of the non-visual elements that help make the page work.<html>
<head>…</head>
</html>
The <body> Tag
Headings, paragraphs, lists, quotes, images, and links are just a few of the elements that can be contained
within the body tag.
HTML files are text files, so you can use any text editor to create your first webpage.
There are some very nice HTML editors available; you can choose the one that works for you. For now let's
write our examples in Notepad.
You can run, save, and share your HTML codes on our Code Playground, without installing any additional
software.
Add the basic HTML structure to the text editor with "This is a line of text" in the body section.
<html>
<head>
</head>
<body>
This is a line of text.
</body>
</html>Try It Yourself
When the file is opened, the following result is displayed in the web browser:
Don’t forget to save the file. HTML file names should end in either .html or .htm
Creating a Blog
Throughout this course, we'll help you practice and create your own unique blog project, so you'll retain what
you've learned and be able to put it to use. Just keep going and follow the instructions in the TASK section.
This is what your finished blog page will look like.
TASK: Tap Try It Yourself to see the code and its output.
<html>
<head>
<title>My Blog</title>
</head>
<body>
...Try It Yourself
Don't be afraid of long codes. By the time you complete the course, everything will make complete sense and
look highly doable. We guarantee it!
TASK:
1. Open the code.
2. On the top header, change the name to your own name.
3. Change the page title. Remember, the page title is located inside the <title> tag in the <head> of the page.
The <p> Element
To create a paragraph, simply type in the <p> element with its opening and closing tags:
<html>
<head>
<title>first page</title>
</head>
<body>
<p>This is a paragraph. </p>
<p>This is another paragraph. </p>
</body>
</html>Try It Yourself
The result:
Use the <br /> tag to add a single line of text without starting a new paragraph:
<html>
<head>
<title>first page</title>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph. </p>
<p>This is <br /> a line break </p>
</body>
</html>Try It Yourself
Opening the HTML file in the browser shows that a single line break has been added to the paragraph:
Formatting Elements
Each paragraph in the example is formatted differently to demonstrate what each tag does:
Browsers display <strong> as <b>, and <em> as <i>.
However, the meanings of these tags differ: <b> and <i> define bold and italic text, respectively,
while <strong> and <em> indicate that the text is "important".
HTML Headings
HTML includes six levels of headings, which are ranked according to importance.
These are <h1>, <h2>, <h3>, <h4>, <h5>, and <h6>.
Result:
It is not recommended that you use headings just to make the text big or bold, because search engines use
headings to index the web page structure and content.
Horizontal Lines
Result:
In HTML5, the <hr> tag defines a thematic break.
Comments
The browser does not display comments, but they help document the HTML and add descriptions, reminders,
and other notes.<!-- Your comment goes here -->
Example:
<html>
<head>
<title>first page</title>
</head>
<body>
<p>This is a paragraph </p>
<hr />
<p>This is a paragraph </p>
<!-- This is a comment -->
</body>
</html>Try It Yourself
Result:
As you can see, the comment is not displayed in the browser.
There is an exclamation point (!) in the opening tag, but not in the closing tag.
Formatting Text
You may have noticed that we've also used some CSS to add colors and styles to the page. When you complete
HTML, we strongly recommend our free CSS Tutorial!
TASK:
1. Create your own About Me section by changing the text.
2. Play around with the code; experiment with formatting text.
HTML Elements
HTML documents consist of nested HTML elements. In the example below, the body element includes
the <p> tags, the <br /> tag and the content, "This is a paragraph".
<html>
<head>
<title>first page</title>
</head>
<body>
<p>This is a paragraph <br /></p>
</body>
</html>Try It Yourself
Some HTML elements (like the <br /> tag) do not have end tags.
HTML Elements
Some elements are quite small. Since you can't put contents within a break tag, and you don't have an opening
and closing break tag, it’s a separate, single element.
Some HTML elements (like the <br /> tag) do not have end tags.
HTML Attributes
In this example, the value of "center" indicates that the content within the p element should be aligned to the
center:
Attributes are always specified in the start tag, and they appear in name="value" pairs.
Attribute Measurements
In the example below, we have a paragraph that is aligned to the center, and a line that is aligned to the right.
<html>
<head>
<title>Attributes</title>
</head>
<body>
<p align="center">This is a text <br />
<hr width="10%" align="right" /> This is also a text.
</p>
</body>
</html>Try It Yourself
Attributes
You may be wondering what happens if you try to apply contradictory attributes within the same element.
<p align="center">
This is a text.
<hr width="50%" align="left" />
</p>Try It Yourself
The <img> Tag
The <img> tag is used to insert an image. It contains only attributes, and does not have a closing tag.
You need to put in the image location for the src attribute that is between the quotation marks.
For example, if you have a photo named "tree.jpg" in the same folder as the HTML file, your code should look
like this:
<html>
<head>
<title>first page</title>
</head>
<body>
<img src="tree.jpg" alt="" />
</body>
</html>Try It Yourself
In case the image cannot be displayed, the alt attribute specifies an alternate text that describes the image in
words. The alt attribute is required.
Image Resizing
To define the image size, use the width and height attributes.
The value can be specified in pixels or as a percentage:
<html>
<head>
<title>first page</title>
</head>
<body>
<img src="tree.jpg" height="150px" width="150px" alt="" />
<!-- or -->
<img src="tree.jpg" height="50%" width="50%" alt="" />
</body>
</html>Try It Yourself
Loading images takes time. Using large images can slow down your page, so use them with care.
Image Border
By default, an image has no borders. Use the border attribute within the image tag to create a border around
the image.
<img src="tree.jpg" height="150px" width="150px" border="1px" alt="" /> Try It Yourself
By default, Internet Explorer 9, and its earlier versions, display a border around an image unless a border
attribute is defined.
The <a> Tag
Links are also an integral part of every web page. You can add links to text or images that will enable the user
to click on them in order to be directed to another file or webpage.
In HTML, links are defined using the <a> tag.
Once the code has been saved, "Learn Playing" will display as a link:
Learn Playing
Result:
Result:
The list items will be marked with bullets.
Creating a Table
Result:
Result:
You can see that the cell containing "Orange" spans two cells.
To make a cell span more than one row, use the rowspan attribute.
To change your table's position, use the align attribute inside your table tag:<table align="center">
Now let's specify a background color of red for a table cell. To do that, just use the bgcolor attribute.
<table border="2">
<tr>
<td bgcolor="red">Red</td>
<td>Blue</td>
<td>Green</td>
</tr>
<tr>
<td>Yellow</td>
<td colspan="2">Orange</td>
</tr>
</table>Try It Yourself
Result:
In the case of styling elements, CSS is more effective than HTML. Try our free "Learn CSS" course to learn
more about CSS and styles.
My Schedule
Use table tags to add a table to your blog that represents your daily learning schedule.
The <th> tags represent the table headers. Take a look at the following code:
<h1><span>My Coding Schedule</span></h1>
<table>
<tr>
<th>Day</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
</tr>
<tr>
<td>8-8:30</td>
<td class="selected">Learn</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>9-10</td>
<td></td>
<td class="selected">Practice</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>1-1:30</td>
<td></td>
<td></td>
<td class="selected">Play</td>
<td></td>
<td></td>
</tr>
<tr>
<td>3:45-5</td>
<td></td>
<td></td>
<td></td>
<td class="selected">Code</td>
<td></td>
</tr>
<tr>
<td>6-6:15</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td class="selected">Discuss</td>
</tr>
</table>Try It Yourself
The empty <td> tags represent empty table cells. They're necessary because they maintain the table's
structure.
TASK: Customize the table to create your unique schedule!
Types of Elements
The <div> element is a block-level element that is often used as a container for other HTML elements.
When used together with some CSS styling, the <div> element can be used to style blocks of content:
<html>
<body>
<h1>Headline</h1>
<div style="background-color:green; color:white; padding:20px;">
<p>Some paragraph text goes here.</p>
<p>Another paragraph goes here.</p>
</div>
</body>
</html>Try It Yourself
Similarly, the <span> element is an inline element that is often used as a container for some text.
When used together with CSS, the <span> element can be used to style parts of the text:
<html>
<body>
<h2>Some
<span style="color:red">Important</span>
Message</h2>
</body>
</html>Try It Yourself
Summary
The <div> element defines a block-level section in a document.
The <span> element defines an inline section in a document.
Types of Elements
Other elements can be used either as block level elements or inline elements. This includes the following
elements:
APPLET - embedded Java applet
IFRAME - Inline frame
INS - inserted text
MAP - image map
OBJECT - embedded object
SCRIPT - script within an HTML document
You can insert inline elements inside block elements. For example, you can have multiple <span> elements
inside a <div> element.
Inline elements cannot contain any block level elements.
The <form> Element
The method attribute specifies the HTTP method (GET or POST) to be used when forms are submitted (see
below for description):<form action="url" method="GET">
<form action="url" method="POST">
When you use GET, the form data will be visible in the page address.
Result:
The name attribute specifies a name for a form.
Form Elements
If we change the input type to radio, it allows the user select only one of a number of choices:
<input type="radio" name="gender" value="male" /> Male <br />
<input type="radio" name="gender" value="female" /> Female <br />Try It Yourself
Result:
The type "checkbox" allows the user to select more than one option:
<input type="checkbox" name="gender" value="1" /> Male <br />
<input type="checkbox" name="gender" value="2" /> Female <br />Try It Yourself
Result:
The <input> tag has no end tag.
Form Elements
Result:
After the form is submitted, the data should be processed on the server using a programming language, such
as PHP.
Contact Form
Next, we'll create a Contact Form for your blog. The form will include Name, Email, and Message fields. We'll
also add a Submit button.
Check out the code:
<h1><span>Contact Me</span></h1>
<form>
<input name="name" type="text" /><br/>
<input name="email" type="email" /><br/>
<textarea name="message"></textarea>
<input type="submit" value="SEND" class="submit" />
</form>Try It Yourself
This is just a static HTML page, so it won't work to actually submit the form. You'd need to create the server-
side code in order to submit a real form and process the data. To learn how, complete SoloLearn's PHP course
once you've completed the HTML and CSS courses.
HTML Colors!
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
As you can see, there are 16 values there, 0 through F. Zero represents the lowest value, and F represents the
highest.
Hex values are written using the hashtag symbol (#), followed by either three or six hex characters.
As shown in the picture below, the circles overlap, forming new colors:
This example would produce a dark blue background with a white headline:
<html>
<head>
<title>first page</title>
</head>
<body bgcolor="#000099">
<h1>
<font color="#FFFFFF"> White headline </font>
</h1>
</body>
</html>Try It Yourself
Result:
The color attribute specifies the color of the text inside a <font> element.
The <frame> Tag
The <frame> tag defines one specific window (frame) within a <frameset>. Each <frame> in a <frameset> can
have different attributes, such as border, scrolling, the ability to resize, etc.
The <frameset> element specifies the number of columns or rows in the frameset, as well as what percentage
or number of pixels of space each of them occupies.<frameset cols="100, 25%, *"></frameset>
<frameset rows="100, 25%, *"></frameset>
The <frameset> tag is not supported in HTML5.
Lastly, the <noframes> element provides a way for browsers that do not support frames to view the page. The
element can contain an alternative page, complete with a body tag and any other elements.
<frameset cols="25%,50%,25%">
<frame src="a.htm" />
<frame src="b.htm" />
<frame src="c.htm" />
<noframes>Frames not supported!</noframes>
</frameset>
Blog Project
To finalize our blog, we'll use a frame to embed a YouTube video. We'll also create a Follow Me section that
includes links at the end of the page.
Check out the final code:
...
<div class="section">
<h1><span>My Media</span></h1>
<iframe height="150" width="300" src="https://www.youtube.com/embed/Q6_5InVJZ88" allowfullscreen
frameborder="0"></iframe>
</div>
...Try It Yourself
TASK:
1. Finalize your blog page.
2. Share your code creation with the community, get feedback, and receive upvotes!
HTML5
When writing HTML5 documents, one of the first new features that you'll notice is the doc type declaration:<!
DOCTYPE HTML>
The character encoding (charset) declaration is also simplified:<meta charset="UTF-8">
New Elements in HTML5
<article>, <aside>, <audio>, <canvas>, <datalist>, <details>, <embed>, <footer>, <header>, <nav>,
<output>, <progress>, <section>, <video>, and even more!
The default character encoding in HTML5 is UTF-8.
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.
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
The HTML5 content models are designed to make the markup structure more meaningful for both the browser
and the web designer.
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>
Phrasing: This model has a number of inline level elements in common with HTML4.
Elements: <img>, <span>, <strong>, <label>, <br />, <small>, <sub>, and more.
The same element can belong to more than one content model.
Content Models
Flow content: Contains the majority of HTML5 elements that would be included in the normal flow of the
document.
The <header> Element
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.<footer>…</footer>
The following information is usually provided between these tags:
- Contact Information
- Privacy Policy
- Social Media Icons
- Terms of Service
- Copyright Information
- Sitemap and Related Documents
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.
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.
<article>
<h1>The article title</h1>
<p>Contents of the article element </p>
</article>Try It Yourself
When an <article> element is nested, the inner element represents an article related to the outer element. For
example, blog post comments can be <article> elements nested in the <article> representing the blog post.
The <section> Element
If it makes sense to separately syndicate the content of a <section> element, use an <article> element instead.
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.
<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>Try It Yourself
When an <aside> tag is used outside of an <article> tag, its content should be related to the surrounding
content.
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 src="audio.mp3" controls>
Audio element not supported by your browser
</audio>Try It Yourself
Multiple <source> elements can be linked to different audio files. The browser will use the first recognized
format.
The <audio> element creates an audio player inside the browser.
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
Audio element not supported by your browser.
</audio>Try It Yourself
Result:
The text between the <audio> and </audio> tags will display in browsers that do not support
the <audio> element.
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.
<audio controls autoplay>Try It Yourself
loop
This attribute is used to have the audio replay every time it is finished.
<audio controls autoplay loop>Try It Yourself
Currently, there are three supported file formats for the <audio> element: MP3, WAV, and OGG.
Videos in HTML
Another aspect that the audio and video elements have in common is that the major browsers do not all
support the same file types. If the browser does not support the first video type, it will try the next one.
Attributes of <video>
Another aspect shared by both the audio and the video elements is that each
has controls, autoplay and loop attributes.
Currently, there are three supported video formats for the <video> element: MP4, WebM, and OGG.
Progress Bar
Example:
Status: <progress min="0" max="100" value="35">
</progress>Try It Yourself
Result:
Use the <progress> tag in conjunction with JavaScript to dynamically display a task's progress.
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.
Storing a Value:
localStorage.setItem("key1", "value1");
Getting a Value:
Removing a Value:
localStorage.removeItem("key1");
localStorage.clear();
The same syntax applies to the session storage, with one difference: Instead of localStorage, sessionStorage is
used.
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.
Geolocation is much more accurate for devices with GPS, like smartphones and the like.
The Geolocation API’s main method is getCurrentPosition, which retrieves the current geographic location of
the user's device.navigator.geolocation.getCurrentPosition();
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.
You need to be familiar with basic JavaScript in order to understand and use the API.
Presenting Data
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.
The getCurrentPosition() method returns an object if it is successful. The latitude, longitude, and accuracy
properties are always returned.
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:<img draggable="true" />
Any HTML element can be draggable.
The API for HTML5 drag and drop is event-based.
Example:<!DOCTYPE HTML>
<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>
</body>
</html>
What to Drag
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:function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
In our example, the data type is "text" and the value is the ID of the draggable element ("image").
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):function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
The preventDefault() method prevents the browser's default handling of the data (default is open as link on
drop).
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.
Basic knowledge of JavaScript is required to understand and use the API.
Drawing Shapes
It offers several methods for drawing paths, boxes, circles, text, and graphic 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:<img src="image.svg" alt="" height="300" />
SVG defines vector-based graphics in XML format.
Drawing a Circle
To draw shapes with SVG, you first need to create an SVG element tag with two attributes: width and
height.<svg width="1000" height="1000"></svg>
To create a circle, add a <circle> tag:
<svg width="2000" height="2000">
<circle cx="80" cy="80" r="50" fill="green" />
</svg>Try It Yourself
- 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:
<rect> defines a rectangle:
<svg width="2000" height="2000">
<rect width="300" height="100"
x="20" y="20" fill="green" />
</svg>Try It Yourself
(x1, y1) define the start coordinates(x2, y2) define the end coordinates.
<polyline> defines shapes built from multiple line definitions:
<svg width="2000" height="500">
<polyline style="stroke-linejoin:miter; stroke:black;
stroke-width:12; fill: none;"
points="100 100, 150 150, 200 100" />
</svg>Try It Yourself
The width and height attributes of the <rect> element define the height and the width of the rectangle.
<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.
<svg height="500" width="1000">
<ellipse cx="200" cy="100" rx="150" ry="70" style="fill:green" />
</svg>Try It Yourself
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.
<svg width="2000" height="2000">
<polygon points="100 100, 200 200, 300 0"
style="fill: green; stroke:black;" />
</svg>Try It Yourself
Result:
Polygon comes from Greek. "Poly" means "many" and "gon" means "angle."
Shape Animations
The example below creates a rectangle that will change its position in 3 seconds and will then repeat the
animation twice:
<svg width="1000" height="250">
<rect width="150" height="150" fill="orange">
<animate attributeName="x" from="0" to="300"
dur="3s" fill="freeze" repeatCount="2"/>
</rect>
</svg>Try It Yourself
In the example above, the rectangle changes its x attribute from 0 to 300 in 3 seconds.
To repeat the animation indefinitely, use the value "indefinite" for the repeatCount attribute.
Paths
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:
All of the above commands can also be expressed with lower case letters. When capital letters are used, it
indicates absolute position; lower case indicates relative position.
The <canvas> Element
The HTML canvas is used to draw graphics that include everything from simple lines to complex graphic
objects.
<script>
var can = document.getElementById("canvas1");
var ctx = can.getContext("2d");
</script>
</body>
</html>
getContext() returns a drawing context on the canvas.
Basic knowledge of JavaScript is required to understand and use the Canvas.
Canvas Coordinates
The HTML canvas is a two-dimensional grid.
The upper-left corner of the canvas has the coordinates (0,0).
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 c=document.getElementById("canvas1");
var ctx=c.getContext("2d");
ctx.fillRect(20,20,100,100);Try It Yourself
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 canvas=document.getElementById("canvas1");
var ctx=canvas.getContext("2d");
ctx.fillStyle ="rgba(0, 200, 0, 1)";
ctx.fillRect (36, 10, 22, 22);Try It Yourself
Result:
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.
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)
You can actually use both SVG and canvas on the same page, if needed.
However, you cannot just draw SVG onto a canvas, or vice-versa.
Result:
In this example, the canvas is moved 100px to the right, and 150px down.
Result:
Canvas has several methods for drawing paths, boxes, circles, text, and adding images.
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.fillStyle = "#FF0000";
ctx.fillRect(10,10, 100, 100);
ctx.fillStyle = "#0000FF";
ctx.fillRect(10,10, 100, 100);Try It Yourself
The rotation will only affect drawings made after the rotation is done.
This will scale the canvas 1.5 times in the X-direction, and 4 times in Y-direction:
If you scale a drawing, all future drawings will also be scaled.
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.
New Attributes
Result:
The autofocus attribute makes the desired input focus when the form loads:
<form>
<label for="email">Your e-mail address: </label>
<input type="text" name="email" autofocus/>
</form>Try It Yourself
Result:
The required attribute tells the browser that the input is required.
The form will not be submitted without filling in the required fields.
Result:
The autocomplete attribute specifies whether a form or input field should have autocomplete turned on or off.
When autocomplete is on, the browser automatically complete values based on values that the user has
entered before.
Result:
You must remember to set a name for your input; otherwise, nothing will be submitted.’
Search Options
The <datalist> tag can be used to define a list of pre-defined options for the search field:
<input id="car" type="text" list="colors" />
<datalist id="colors">
<option value="Red">
<option value="Green">
<option value="Yellow">
</datalist>Try It Yourself
Result:
These are especially useful when opening a page on a modern mobile device, which recognizes the input types
and opens a corresponding keyboard matching the field's type:
These new types make it easier to structure and validate HTML forms.