On Github chrisweb / html5_and_beyond
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Title</title> <link rel="stylesheet" href="example.css" type="text/css"> <script type="text/javascript" src="example.js"> </script> </head> <body> <p>Hello World</p> </body> </html>
<!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8> <title>Title</title> <link rel=stylesheet href=example.css> <script src=example.js></script> </head> <body> <p>Hello World</p> </body> </html>
shorter doctype
no more namespace
shorter charset meta tag
no more type parameter
attribute quotes are optional
title must be in head (not new but important)
<!DOCTYPE html> <meta charset="UTF-8"> <title>Title</title> <p>Hello World</p>
For a complete list of differences from HTML4 check the w3c page.
Of course this is valid:
<br>
But the XML style from XHTML is still valid too:
<br />
Check out the w3c syntax guide: http://www.w3.org/TR/html5/syntax.html
HTML5 is not XML but closing tags are still valid.
If you want to check the validity of self closing tags with a space and slash at the end in HTML5 use the w3c validator: http://validator.w3.org/check
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HTML5 self closing tags</title> <base href="http://www.example.com/" target="_blank" /> <link rel="stylesheet" href="#" /> <meta name="author" content="chris.lu" /> </head> <body> <map name="foo"><area /></map> <br /> <table><colgroup><col /></colgroup><tr><th></th></tr></table> <command /> <embed /> <hr /> <img src="#" alt="#" /> <input /> <keygen /> <object data="#"><param name="#" value="#" /></object> <audio><source src="#" /></audio> <video><track src="#" /></video> <wbr /> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>html5 outlining</title> </head> <body> <header> <hgroup> <h1>the content of h1</h1> <h2>the content of h2</h2> </hgroup> <nav> <ul> <li> <a href="#">first link</a> <a href="#">second link</a> </li> </ul> </nav> </header> <aside> <p>some aside content</p> </aside> <section> <p>section content</p> <article> <header> <p>first article header</p> </header> <p>first article</p> <footer> <p>first article footer</p> </footer> </article> <article> <p>second article</p> </article> </section> <footer> <p>some footer content</p> </footer> </body> </html>test this code in an online outliner:http://hoyois.github.com/html5outliner/ orhttp://gsnedders.html5.org/outliner/
<section id="news"> The articles <section id="comments"> The comments </section> </section>
<article>My Article</article>
<section id="news"> The news </section> <aside> A blogroll </aside>
<hgroup> <h1>Foo</h1> <h2>Bar</h2> </hgroup>
<header> The page header </header> <article> <header>Article header</header> </article>
<article> <footer>Article footer</footer> </article> <footer> The page footer </footer>
<nav> <ul> <li>Menu item</li> </ul> </nav>
<figure> <img src="images/html5_badges.png" /> <figcaption>HTML5 badges (caption)</figcaption> </figure>
<video controls="controls" preload="auto" width="640" height="360"> <source src="../video_track/bunny.webm" type="video/webm" /> <source src="../video_track/bunny.mp4" type="video/mp4" /> <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'> <param name='movie' value='player.swf'> <param name='allowfullscreen' value='true'> <param name='flashvars' value='file=../video_track/bunny.mp4'> <embed src='player.swf' allowscriptaccess='always' allowfullscreen='true' flashvars="file=../video_track/bunny.mp4" /> </object> </video>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HTML5 audio</title> </head> <body> <audio id="example" controls="controls" preload="auto"> <source src="song.ogg" type="audio/ogg" /> <source src="example.mp3" type="audio/mpeg" /> </audio> </body> </html>
source: caniuse.com
There is even a proposal for DRM in the works, something the industry wants to have but lots of users and perhaps some browser vendors don't want.
Progress:
<progress value="22" max="100"></progress>
There is a very good article about progress bar styling on css-tricks.
Meter:
<meter value=55 min=0 max=100 low=30 high=70 optimum=80></meter>If the optimum is below min the meter color will be red, between min and max yellow and green above max. The colors will be in reverse order if the optimum is below min, min green, middle yellow and max green. If the optimum is between min and max the color is green else yellow.definition of optimum
If the meter belongs to a form but is placed outside of the form tags you can add the forms ID using the form attribute of meter, here is an example.
<article> <time datetime="YYYY-MM-DDThh:mm:ssTZD"> <time datetime="2012-04-17" pubdate="pubdate">17.4.2012</time> </article>
A good article describing how to use the time tag can be found on Bruce Lawson's blog
Some time ago the time tag was dropped, but then after protest of web developers was put back in.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>wbr</title> </head> <body> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id eleifend odio. Sed eget fermentum nisi. Donec posuere, nulla eget lobortis mattis, <wbr>(the <wbr> is here) quam dolor commodo dolor, ac hendrerit mi orci ut mauris. Donec sed lectus dui, vel gravida urna. Mauris vulputate nulla at est tincidunt a euismod leo commodo. Pellentesque pretium sapien pellentesque dui volutpat dictum. </p> <p>Llllooorrreeeemmmm<wbr>(<wbr>)iiiiippppppsssssuuuuummmmmm</p> <p>Llllooorrreeeemmmmmmiiiiiiiippppppsssssuuuuuuummmmmmm</p> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>html5 line canvas</title> <script> function initialize() { var canvas = document.getElementById('example'); if (canvas.getContext) { var context = canvas.getContext('2d'); fillRectangle(context); drawLine(context); } } function drawLine(context) { context.moveTo(100, 150); context.lineTo(250, 50); context.stroke(); } function fillRectangle(context) { var gradient = context.createLinearGradient(0, 0, 0, 100); gradient.addColorStop(0, "#ff005a"); gradient.addColorStop(1, "#ff985a"); context.fillStyle = gradient; context.fillRect(50, 50, 150, 150); } </script> <style> canvas { border: 1px solid blue; } </style> </head> <body onLoad="initialize();"> <canvas id="example" width="500" height="300">Your browser does not support canvas ... go away!</canvas> </body> </html>
<menu type="toolbar"> <command type="radio" radiogroup="alignment" checked="checked" label="Left" icon="icons/left.png" onclick="alert('left')"> <command type="radio" radiogroup="alignment" label="Right" icon="icons/right.png" onclick="alert('right')"> <hr> <command type="command" disabled label="Publish" icon="icons/disabled.png" onclick="alert('disabled')"> </menu>
<details> <summary>summary</summary> <p>Additional Data.</p> </details>summary
Additional Data.
<form> <input list="browsers"> <datalist id="browsers"> <option value="Chrome"> <option value="Firefox"> <option value="Safari"> <option value="Internet Explorer"> <option value="Opera"> </datalist> </form>
I know this one is not new, but with all the new semantic tags, you could wonder if there is still use for div and / or span.
Yes there is, use divs every time there is no tag with a semantic value that could replace it, or for content where you don' want to add semantic value because you just wont to be able to style it's content using CSS.
Was already available in HTML4.01 but there the address always referred to the document owner, in HTML5 address can be placed in an article and then represents the address of the article author.
<!--[if lt IE 9]> <script src="//example.com/html5shiv-printshiv.js"></script> <![endif]-->Did you notice the double slash in the URI, this is valid, advantage of omitting the protocol (http: / https:), is that the browser will choose which to use depending on the protocol used by your page
In HTML5 users can create custom attributes that will be valid, the only rule is that the attribute must be prefixed with data-, for example data-foo="bar".
it's recommended that you use a specific prefix after the data prefix for your app to avoid conflicts with other libraries you use that also use the data attribute but may not have an own prefix<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>html5 data attribute</title> <script> function initialize() { var example = document.querySelector('#example'); // Add new data attributes via JS. example.dataset.myprefixFoo = 'bar'; //example.setAttribute('data-myprefix-foo', 'bar'); for (var key in example.dataset) { console.log(key, ': ', example.dataset[key]); } } </script> </head> <body onload="initialize();"> <p id="example" data-myprefix-hello="world">Example</p> </body> </html>
A useful new attribute for script tags is the async attribute. It ensures you scripts get loaded asynchronously and don't block you page rendering. Async is useful for all javascript files that have code that you page needs at rendering time, but only later to progressively enhance your page.
<script src="example.js" async></script>
The w3c HTML5 working draft specifies a new context menu attribute:
It allows you to add own entries in the context menu that appears when a user right clicks on an element
Right Click ME!
<!DOCTYPE html> <meta charset="UTF-8"> <title>Context Menu</title> <p contextmenu=examplemenu>Right Click ME!</p> <menu type=context id=examplemenu> <command label="FOO" onclick="alert('FOO ACTION');"> <command label="BAR" onclick="alert('BAR ACTION');)"> </menu>
(As of today, works only in latest Firefox)
An howto for using yepnope and modernizr to detect the new native form input types or use a fallback can be found at css-tricks.com
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>html5 form input types</title> </head> <body> <form> <ul> <li><input type="tel" placeholder="tel" required="required"></li> <li><input type="search" placeholder="search" autofocus="autofocus"></li> <li><input type="url" placeholder="url"></li> <li><input type="email" placeholder="email"></li> <li><input type="datetime" placeholder="datetime"></li> <li><input type="date" placeholder="date"></li> <li><input type="month" placeholder="month"></li> <li><input type="week" placeholder="week"></li> <li><input type="time" placeholder="time"></li> <li><input type="datetime-local" placeholder="datetime-local"></li> <li><input type="number" placeholder="number"></li> <li><input type="range" placeholder="range"></li> <li><input type="color" placeholder="color" required></li> <li><input type="text" placeholder="pattern attribute" id="part" name="part" required pattern="[A-Z]{3}[0-9]{4}" title="Part numbers consist of 3 uppercase letters followed by 4 digits." /></li> <li><input type="submit" value="go4it"></li> </ul> </form> </body> </html>
Side note not related to html5: Since google chrome 11, there is also a new input field attribute x-webkit-speech (not a standard from w3c and therefore not part of html5 / for chrome only)
google did submit a proposal for this to the w3c, speech input draft
<input type="text" x-webkit-speech />
There is a new attribute called the placeholder attribute for forms input fields. It will create a semi transparent text inside input fields indented to eventually replace the label.
<input type="text" placeholder="placeholder text" />
Use the autofocus attribute as attribute of the input field of your form if you want that field to be selected open opening the page.
<input type="text" autofocus /> <input type="text" autofocus="autofocus" />
Form fields can now have the attribute required, browsers like chrome will show a tooltip message near the field if the user tries to submit the form but the field is still empty and will prevent the submit event until the required field is not empty anymore.
<input type="text" required /> <input type="text" required="required" />
Using the pattern attribute you can define a regular expression that your field content is validated against. You should also use the title attribute and explain what you expect from the user.
<input type="text" placeholder="pattern attribute" id="part" name="part" required pattern="[A-Z]{3}[0-9]{4}" title="Part numbers consist of 3 uppercase letters followed by 4 digits." />If the input does not match the pattern, the CSS selector "invalid" will be added to the field and you can use it to style the field differently:
:invalid { border: 1px solid red; }
To upload multiple files you can add the new multiple attribute to the file input tag. The other can now select multiple files and then click the upload button. You have to adapt your side side scripts to let them accept multiple file uploads and maybe should add some rules to avoid users spamming you with hundreds of file uploads.
<input type="file" multiple> OR <input type="file" multiple="multiple">
Why not? Add the new types and attributes to your form, anyway they degrade gracefully:
some browser will already support them and increase the usability of your forms, other browser won't support them, therefore you probably still want to add a javascript library with ui form widgets.
You can use modernizr to check if the browser supports them nativly or if you have to use the javascript fallback libraries like jQuery UI or dojo toolkit form widgets
HTML5 defines some types of links using the rel attribute. One of the new values is prefetch with tells the browser that it can prefetch that link because it is likely this resource next.
Or the values prev and next which are useful for pagination.
Nofollow tells search engines that the link should not influence link targets rating (its useful to avoid hackers to spam your comments sections with posts full of links if you apply automatically nofollow to all links that are in comments).
The bookmark link type could be very usefull too, it specifies a permalink for the parent content. In combination with url shorteners like goo.gl and the rel="canonical" you could make permalinks that are usefull for the visitor as well as search engines. Canonical is not part of the HTML5 specification but it will not make your html5 page invalid, because the HTML5 specification says that values of the rel attribute that are unrecognized by the browser will be treated as normal links.
Here is a list and description of all rel values available in html5.
Microdata are machine-readable labels that can enhance the semantic value of your documents for crawlers like google bot.
the w3c microdata specification.
A definition of structures for microdata from schema.org.
I think so
Google has a very useful page about microdata, at the end of the page are examples of microdata that google recognizes like people or reviews. Go for example to the page reviews and there you will find examples howto use microdata to ensure google uses your data to create rich snippets that will enhance the search results of your site in google results pages.
You can test your page to check if google recognizes your microdata.
Look at the microdata example file, to see an example of a document containing a blog post and comments (source: w3c HTML5 specification).
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>html5 microdata</title> </head> <body> <article itemscope itemtype="http://schema.org/BlogPosting"> <header> <h1 itemprop="headline">The Very First Rule of Life</h1> <p><time itemprop="datePublished" datetime="2009-10-09">3 days ago</time></p> <link itemprop="url" href="?comments=0"> </header> <p>If there's a microphone anywhere near you, assume it's hot and sending whatever you're saying to the world. Seriously.</p> <p>...</p> <section> <h1>Comments</h1> <article itemprop="comment" itemscope itemtype="http://schema.org/UserComments" id="c1"> <link itemprop="url" href="#c1"> <footer> <p>Posted by: <span itemprop="creator" itemscope itemtype="http://schema.org/Person"> <span itemprop="name">George Washington</span> </span></p> <p><time itemprop="commentTime" datetime="2009-10-10">15 minutes ago</time></p> </footer> <p>Yeah! Especially when talking about your lobbyist friends!</p> </article> <article itemprop="comment" itemscope itemtype="http://schema.org/UserComments" id="c2"> <link itemprop="url" href="#c2"> <footer> <p>Posted by: <span itemprop="creator" itemscope itemtype="http://schema.org/Person"> <span itemprop="name">George Hammond</span> </span></p> <p><time itemprop="commentTime" datetime="2009-10-10">5 minutes ago</time></p> </footer> <p>Hey, you have the same first name as me.</p> </article> </section> </article> </body> </html>
DOM4 is not part of HTML5 but it will probably be part of modern web development as well as CSS3 which also isn't part of HTML5.
To learn more about DOM4 red the w3c editors draft.
One of the cool things DOM4 will offer are DOM Mutation Observers, a good article can be found on html5rocks.com.
An API for playing audio which can be used with the new audio tag.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>html5 audio api</title> <script> function initialze() { console.log(document.getElementByID('example').duration); console.log(document.getElementByID('example').currentTime); } </script> </head> <body> <audio id="example" controls="controls" preload="auto"> <source src="song.ogg" type="audio/ogg" /> <source src="example.mp3" type="audio/mpeg" /> </audio> <div> <button onclick="document.getElementById('example').play()">Play the Audio</button> <button onclick="document.getElementById('example').pause()">Pause the Audio</button> <button onclick="document.getElementById('example').volume+=0.1">Increase Volume</button> <button onclick="document.getElementById('example').volume-=0.1">Decrease Volume</button> </div> </body> </html>
A very nice audio library can be found on github. Audio.js is a javascript library which fallbacks to flash in older browsers that don't have the HTML5 audio capabilities.
We have already seen the video tag, the track tag and the webVTT format for subtitles but there is even more. With the native browser API, you can choose not to use the native controls but build your own. Or make your script interact with the videos.
The html5 drag and drop API will allow you to make items on your website draggable. Combined with the new file API you can even do drag and drop file uploads.
Two good articles that will help you get started with the drag and drop API by html5doctor and the html5rocks article.
try this drag and drop api example
API that exposes the history and allows pages to add to it to prevent breaking the back button.
This new API is especially useful, if for example you use Ajax to retrieve a list of news and the user changes the filters and then he bookmarks the page. When he comes back, everything is reset and he sees the default news list again. But if you change the URI using the history API while he changes the filter options to save their state in the url and he then bookmarks the page, every time he comes back the filters will be set to the values he had selected when making the bookmark.
An impressive example of the history API, is github, check out how github only reloads the content area of the page and changes the url without reloading the whole page. jQuery Mobile also uses the history API, jQuery Mobile loads new pages using Ajax, adds those pages to the DOM and then changes the url.
As like other html5 APIs there is a javascript library polyfill for history, named history.js, it also supports older browser's by using the hash as fallback method
try this history api example
This API sends out notifications if the current browser tab changes it's visibility, you can catch events like prerendering, visible or hidden status of your page.
Page visibility is still work in progress and it looks like it got implemented slightly differently by the browsers that support it (chrome, safari, opera and firefox, IE 10 will support it too). Therefore it might be a good idea to use a wrapper library like visibility.js that can be found on github.
The whole working draft about page visibility at w3.org
A nice page visibility example
The geolocation API allow you to locate users. The techniques for geolocation vary depending on which technique is available, GSM triangulation, GPS localization, WiFi triangulation or IP geolocation via a database.
With the watchPosition function you can track users, if the position changes the callback will be executed. To stop watching geolocation changes you call clearWatch.
Geolocation API w3c page
you can try this geolocation api example
Web workers allow you to put heavy tasks like calculations in threads, to make them run in the background and therefore don't make the user wait for them to finish. You can listen to web workers and get a notice when their work is done.
<script> var worker = new Worker('worker.js'); worker.onmessage = function (event) { document.getElementById('result').textContent = event.data; }; </script>
The w3c web workers editors draft which already had some good examples to get started
A very good article about web workers can be found on html5rocks and another one can be found on the mozilla developer zone
Another html5 feature is the offline ode which allows you to make your website available offline. This can be useful for mobile users.
To use the offline mode you need create a manifest file and add a the manifest attribute to the html tag of your page, the manifest attribute value is the path to your manifest file.
<html lang="en" manifest="my.manifest">
More informations about the manifest can be found in the mozilla developer zone and on html5rocks
CACHE MANIFEST # 2012-0-22:v3.1 CACHE: /favicon.ico index.html stylesheet.css images/logo.png NETWORK: chat.js http://google-analytics.com/ga.js FALLBACK: /login login.html
The manifest file has three sections.
NETWORK is for resources that you don't want to make available offline like a javascript file used to do track user statistics.
CACHE is a list of all the files you want to make available offline.
FALLBACK a list of files that were available online like a login page which you want to replace by a special offline version of that page.
While downloading the files listed in your manifest your browser will fire out progress events you can listen to and which tell you how much files have already been downloaded. It will fire even more events when download starts, finishes or noupdate if nothing new got downloaded because all files are already in your browser cache.
The local storage allow you to save key / value pairs locally in your browser. Those informations persist even if the user closes his browser. Those informations can not be read by another website that is hosted on another domain because of the same origin policy. Unlike cookies the information in the storage never gets transmitted to the server.
localStorage.length; localStorage.key(n); localStorage.getItem(key); localStorage.setItem(key,data); localStorage.removeItem(key); localStorage.clear();
The session storage is like the local storage but the data doesn't persist if the user closes his browser.
var cnt = sessionStorage.length; sessionStorage.key(n); sessionStorage.getItem(key); sessionStorage.setItem(key,data); sessionStorage.removeItem(key); sessionStorage.clear();
Using the fileapi you can now listen to the progress events and display the progress to users using a progress bar. You can preview images before they get uploaded. You can even create files, for example you can create thumbnails of images even before the user clicks the upload button.
<input type="file" id="input" multiple onchange="handleFiles(this.files)">
The w3c html5 file API draft and here is the filesystem API w3c page.
Lots of informations and code examples can be found on the mozilla developer zone.
Here is a file upload drag and drop demo and html5rocks has an article that explains howto use the file api and create a drag and drop upload field.
Yet again on html5rocks is a very good article about the filesystem API.
The fullscreen API allows you to put any html element in fullscreen mode.
A good article about the fullscreen API can be found on html5rocks
getUserMedia() or the prefixed webkit version webkitGetUserMedia() also called the device API, is a function that will allow us to build great web apps in the future, but it's not part of HTML5. A good source of informations is the webRTC w3c editors draft
getUserMedia() can be used to access the users webcam, the video stream can be displayed in a canvas element and therefore be modified in real time using canvas functionality. Opera is the has a nice example.
The w3c editors draft can be found here.
// Replace the source of the video element with the stream from the camera var video = document.getElementById('sourcevid'); if (navigator.getUserMedia) { navigator.getUserMedia('video', successCallback, errorCallback); function successCallback(stream) { video.src = stream; } function errorCallback(error) { console.error('An error occurred: [CODE ' + error.code + ']'); return; } } else { console.log('Native web camera streaming (getUserMedia) is not supported in this browser.'); return; }With the webRTC standard you could easly create a web app like chat-roulette.
With html4 you could use getElementById , now with html5 you can the two newcomers querySelector and querySelectorAll
var domNode = document.querySelector('.classA', '.classB'); var domNodes = document.querySelectorAll('.classA', '.classB');
Web sockets allow you to build a bi-directional communication channel to a socket of your server. That connection stays open for as long as you want, you can send data as often as needed to the server. The server can push answers to the client every time he wants, the server doesn't need to wait for the client to ask something.
An application that needs such a feature is a chat. An ajax chat would have to check every second if there is a new message an the server that got written by another user using an ajax request. With web sockets all you need to do is open a connection and wait for incoming messages by the server.
<script> conn = new WebSocket('ws://nodejs.example.com:9999'); conn.onmessage = function (event) { var message = event.data; } </script>
The web sockets specification from w3c. There are also two interesting articles on the mozilla developer zone and html5rocks and a demo can be found on html5demos.com.
Cross-window messaging is a new technique that allows you to send messages to other tabs opened in your browser.
A nice article from John Resig about cross-window messaging
Your right if you think that xmlhttprequest are nothing new, the news is the level2 suffix.
The most important new advantages of xhr2 are a standardized technique, xhr file transfer (ajax file upload) and cross domain xhr requests.
<script> var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://www.example.com/hello.json'); xhr.onload = function(e) { var data = JSON.parse(this.response); } xhr.send(); </script>
The w3c xmlhttprequest working draft.
A good article about the xhr2 can be found on html5rocks with examples for file uploads using xhr, sending data to the server and even howto request data from a different origin.
This new feature allows you make elements editable, like a div containing some text.
The w3c's description of this new feature. A very good article about contenteditable can be found at html5doctor. A small demo can be found on this page.
You may wonder for what this is usefull, if we already have forms or what you should done after then content got edited. I think the idea is to listen to changes using javascript, for example for keypress events on the contenteditable div and then send those changes to the server by using ajax. You could also record changes using javascript and then do some interaction with anot