HTML5 SVG

Toggle navigation
TUTORIAL HOME
HTML5 SVG
❮ Previous Next ❯
What is SVG?
SVG stands for Scalable Vector Graphics
SVG is used to define graphics for the Web
SVG is a W3C recommendation
The HTML Element
The HTML element is a container for SVG graphics.

SVG has several methods for drawing paths, boxes, circles, text, and graphic images.

Browser Support
The numbers in the table specify the first browser version that fully supports the element.

Element
4.0 9.0 3.0 3.2 10.1
SVG Circle
Example

 

»
SVG Rectangle

Example

  <rect width="400" height="100" style="fill:rgb(0,0,255);stroke-width:10;stroke:rgb(0,0,0)” />

»
SVG Rounded Rectangle
Example

  <rect x="50" y="20" rx="20" ry="20" width="150" height="150"
  style=”fill:red;stroke:black;stroke-width:5;opacity:0.5″ />

»
SVG Star
Example

  <polygon points="100,10 40,198 190,78 10,78 160,198"
  style=”fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;” />

»
SVG Logo
SVG
Example

 
   
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1″ />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1″ />
   
 
 
  SVG
  Sorry, your browser does not support inline SVG.

»
Differences Between SVG and Canvas
SVG is a language for describing 2D graphics in XML.

Canvas draws 2D graphics, on the fly (with a JavaScript).

SVG is XML based, which means that every element is available within the SVG DOM. You can attach JavaScript event handlers for an element.

In SVG, each drawn shape is remembered as an object. If attributes of an SVG object are changed, the browser can automatically re-render the shape.

Canvas is rendered pixel by pixel. In canvas, once the graphic is drawn, it is forgotten by the browser. If its position should be changed, the entire scene needs to be redrawn, including any objects that might have been covered by the graphic.

Comparison of Canvas and SVG
The table below shows some important differences between Canvas and SVG:

Canvas SVG
Resolution dependent
No support for event handlers
Poor text rendering capabilities
You can save the resulting image as .png or .jpg
Well suited for graphic-intensive games
Resolution independent
Support for event handlers
Best suited for applications with large rendering areas (Google Maps)
Slow rendering if complex (anything that uses the DOM a lot will be slow)
Not suited for game applications
To learn more about SVG, please read our SVG Tutorial.

❮ Previous Next ❯

HTML5 Style Guide and Coding Conventions

TUTORIAL HOME
HTML5 Style Guide and Coding Conventions
❮ Previous Next ❯
HTML Coding Conventions
Web developers are often uncertain about the coding style and syntax to use in HTML.

Between 2000 and 2010, many web developers converted from HTML to XHTML.

With XHTML, developers were forced to write valid and “well-formed” code.

HTML5 is a bit more sloppy when it comes to code validation.

Be Smart and Future Proof
A consistent use of style, makes it easier for others to understand your HTML.

In the future, programs like XML readers, may want to read your HTML.

Using a well-formed-“close to XHTML” syntax, can be smart.

Always keep your code tidy, clean, and well-formed.

Use Correct Document Type
Always declare the document type as the first line in your document:

If you want consistency with lower case tags, you can use:

Use Lower Case Element Names
HTML5 allows mixing uppercase and lowercase letters in element names.

We recommend using lowercase element names because:

Mixing uppercase and lowercase names is bad
Developers normally use lowercase names (as in XHTML)
Lowercase look cleaner
Lowercase are easier to write
Bad:


 

This is a paragraph.


Very Bad:

 

This is a paragraph.


Good:

 

This is a paragraph.

Close All HTML Elements
In HTML5, you don’t have to close all elements (for example the

element).

We recommend closing all HTML elements.

Bad:

 

This is a paragraph.
 

This is a paragraph.

Good:

 

This is a paragraph.

 

This is a paragraph.

Close Empty HTML Elements
In HTML5, it is optional to close empty elements.

Allowed:

Also Allowed:

However, the closing slash (/) is REQUIRED in XHTML and XML.

If you expect XML software to access your page, it is a good idea to keep the closing slash!

Use Lower Case Attribute Names
HTML5 allows mixing uppercase and lowercase letters in attribute names.

We recommend using lowercase attribute names because:

Mixing uppercase and lowercase names is bad
Developers normally use lowercase names (as in XHTML)
Lowercase look cleaner
Lowercase are easier to write
Bad:

HTML5 Migration

Toggle navigation
TUTORIAL HOME
HTML5 Migration
❮ Previous Next ❯
Migration from HTML4 to HTML5
This chapter is entirely about how to migrate from HTML4 to HTML5.

This chapter demonstrates how to convert an HTML4 page into an HTML5 page, without destroying anything of the original content or structure.

You can migrate from XHTML to HTML5, using the same recipe.

Typical HTML4 Typical HTML5