Let's Start To Work in HTML5
Let's Start To Work in HTML5: Overview
It’s been a while now that HTML5 has started to be hyped in technology news. Some say that HTML5 might mean the end of Flash, other say that it won’t change a lot. It is now time to look seriously at what HTML5 brings to the table. In this presentation, we are going to look at the new elements and functionality in HTML5 and where and when we should start using them in our work. Also, because it is always better to look at real code, even if we don’t have a lot of time, we are going to try to bring parts of a Wordpress theme to HTML5.
A little history of HTML (and friends)
- 1991:
- HTML is born
- 1993:
- Mosaic: the first graphical browser
- 1995:
- HTML 2, Javascript, Netscape 2.0 beta
- 1996:
- CSS 1, Netscape 3, Internet Explorer 3
- 1997:
- HTML 3.2, Netscape 4, Internet Explorer 4
- 1998:
- HTML 4, CSS 2
- 1999:
- Internet Explorer 5
A little history of HTML (and friends)
- 2000:
- XHTML 1, Internet Explorer 5.5
- 2001
- The end of the browser wars: Internet Explorer 6
- 2002-2007
- Evangelist years, no more tables please!
- 2004
- WHATWG (Web Forms 2.0 and Web Applications 1.0 initiative)
- 2005
- Ajax!
- 2007
- CSS 2.1
- 2009-2011
- HTML5, Firefox 3 + Internet Explorer 9, Safari 4+, Chrome +, CSS 3
Evolution or revolution?
- Do open / free technologies win in the end ?
- Is all the hype worth it ?
- The end of Flash ?
- What about Silverlight, JavaFX and Flex ?
- Mobile Web, Desktop like application, Cloud computing, Social Web
- Forward compatibility / interoperability. Easier graceful degradation
- More details in the specification means more predictable and interoperable implementations
- HTML5's goal is to represent reality not a theory
But What is HTML5 ?
More than just markup ?
New semantic elements
- Structure
- <section>, <article>, <aside>, <figure>
- Text level
- <time>, <ruby>, <mark>, <i> , <b>
Helps maintain, repurpose, accessibility, SEO, etc.
<section>
- <section> is not <div>
- Sectioning a document or an application
- Less generic than <div>, but more generic than <article>, <nav> or <aside>
<section>
<h1 Section 1 </h1>
<section>
<h1 Section 1.1 </h1>
<section>
<h1 Section 1.1.1 </h1>
</section>
</section>
</section>
<section>
<h1 Section 2 </h1>
<section>
<h1 Section 2.1 </h1>
<p>Lorem Ipsum Dolor Sit Amet.<p>
</section>
<section>
<h1 Section 2.2 </h1>
<p>Lorem Ipsum Dolor Sit Amet.<p>
</section>
</section>
<article>
- Self contained content
- Independetly distributable
- Reusable
- Ex : Newspaper article, blog entry or comment, interactive widget
<article>
<h1 Article heading </h1>
<section>
<h1 Section 1 </h1>
<section>
<h1 Section 1.1 </h1>
</section>
</section>
</article>
<nav>
- Section enclosing navigation links
- Only major navigation blocks
- Do not mistake with footer or menu
<nav>
<ul>
<li <a> href="#">Home 1</a> </li>
<li <a> href="#">FAQ 1</a> </li>
<li <a> href="#">About Us 1</a> </li>
<li <a> href="#">Our products 1</a> </li>
</ul>
</nav>
<header>
- Group of introductory or navigational aids
- Includes h1 - h6 elements
- Table of contents
- Logos, search forms, etc
- Metadata about the enclosing section
<header>
<h1><img src="logo.png" alt="Our Company"></h1>
<ul>
<li <a> href="#">Home <a> </li>
<li <a> href="#">FAQ <a> </li>
<li <a> href="#">About Us<a> </li>
<li <a> href="#">Our products<a> </li>
</ul>
<form action="search.do">
<input type="search" placeholder="search" value="search">
<input type="submit" value="go!">
</form>
</header>
<footer>
- Footer for it's nearest ancestor sectioning content
- Can contain author information, links to related documents, copyright data
- Links to sitemaps, contact form, etc
<footer>
<p>This article was written by the 14th doctor.
Copyright © Theta Sigma Corp. Most rights reserved</p>
<ul>
<li <a> href="#">Home </a></li>
<li <a> href="#">Previous article </a></li>
<li <a> href="#">Next article</a></li>
<li <a> href="#">Top 1</a></li>
</ul>
<p>Share with your friends : </p>
<ul>
<li <a> href="#">Twitter 1</a></li>
<li <a> href="#">Facebook 1</a></li>
<li <a> href="#">Friendfeed</a></li>
<li <a> href="#">Whatever1</a></li>
</ul>
</footer>
<hgroup>
- Group a title and a subtitle
- Only the highest level heading is kept in the outline
<header>
<hgroup>
<h1><img src="logo.png" alt="Our Company"></h1>
<h2>Our most convincing slogan!</h2>
<hgroup>
<ul>
<li <a> href="#">Home <a> </li>
<li <a> href="#">FAQ <a> </li>
<li <a> href="#">About Us<a> </li>
<li <a> href="#">Our products<a> </li>
</ul>
<form action="search.do">
<input type="search" placeholder="search" value="search">
<input type="submit" value="go!">
</form>
</header>
<aside>
- Section of content that is tangentially related to the content around the aside element
- Glossary, pull-quote for an article or section
- Blog roll, related links for a page or site
- Not for publicity, unless it is related to the page, article or section
- Content is not necessary for the understanding of the article or page
<article>
<h1> Article heading </h1>
<section>
<h1 Section 1 </h1>
</section>
<aside>
<h1>Glossary</h1>
<dl>
<dt>Sonic Screwdriver</dt>
<dd>Lorem Ipsum Dolor Sit Amet</dd>
<dt>The Shadow Proclamation</dt>
<dd>Lorem ipsum dolor sit amet</dd>
</dl>
</aside>
</article>
<figure> and <figcation>
- Self contained unit of content
- Can contain a caption
- Referenced from the main flow of a document
- Can be moved from the main flow without affecting the document
- Grouping images with a caption
- Code examples
<figure>
<img src="vacation1.png" alt="Family at the beach">
<img src="vacation2.png" alt="Family in the forest">
<img src="vacation3.png" alt="Family at the amusement park">
<figcaption>
Our best vacation photos
<figcaption>
</figure>
<time>
- Represents a point in time (24 hour clock or a date)
- Format : Proleptic Gregorian calendar
- Accessibility and internationalization
- Attribute datetime, enclosed text is arbitrary
- pubdate attribute when within an article represents the published date
<article>
<header>
<h1> Article heading </h1>
<p>Published:
<time pubdate datetime="2010-05-27">27th May, 2010</time</p>
<header>
<section>
<h1> Section 1 </h1>
<section>
<h1> Section 1.1 </h1>
</section>
</section>
</article>
<mark>
- highlights part of the document that is dynamically relevent to the current reading context
- ex: searched terms, new changes in a document
<p>You searched this page for the text <mark>test</mark> : </p>
<section>
<h1> Section title </h1>
This page is a <mark>test</mark>.
Because it is always good to <mark>test</mark>
before going to production!
</section>
New semantics for <i>, <b>, <small> and <hr>
- <i>:
- alternative voice (ex: foreign words for pronounciation, technical terms, )
- <b>:
- span of text offset from its surrounding content without conveying any extra importance
(from W3C specification), (ex: keywords, product names) - <small>:
- side comments and small print
- <hr>:
- paragraph-level thematic break
<ruby>, <rt> and <rp>
Annotations used for pronunciation guidance in Chinese, Japanese and Korean languages
Media elements
No more plug-ins! Yay!
Oh is it really that ? Let's go to the fine print
<video>
- Embed a video on your pages without plugins
- autoplay attribute: play the video when the page is loaded
- preload attribute: download the video in background (none, metadata, auto)
- loop attribute: if specified, the video restarts after it is finished
- poster attribute: link to an image to show while the video is loading / not playing
- <source> element (with src attribute) link to the video file
<video>
Big Buck Bunny trailer with the video element
<video>
Continued...
- control attribute: if present, enables default controls
- Subtitles, caption and accessibility : TTML or WebSRT, the <track> element, kind attribute (subtitles, captions, descriptions, chapters, metadata)
- Fallback: you can use an <object> element (with Flash or Silverlight) after the <source> elements
- API for our own controls: play(), pause(), playbackrate, currentTime, ended, pause, volume, muted, etc.
- canPlayType(): verify if the browsers supports a codec or type of file
<article id="bigbuckbunny">
<h1>Big Buck Bunny trailer with the video element</h1>
<video controls>
<source src="http://mirror.cessen.com/blender.org/peach/trailer/trailer_iphone.m4v">
<source src="http://download.blender.org/peach/trailer/trailer_400p.ogg">
</video>
<footer>
<p>(c) copyright Blender Foundation |
<a href="http://www.bigbuckbunny.org">www.bigbuckbunny.org</a></p>
</footer>
</article>
<audio>
- Embed audio on your pages without plugins
- controls attribute: enables default controls if present
- Fallback: you can use an <object> element (with Flash or Silverlight) after the <source> elements
- API for our own controls: play(), pause(), playbackrate, currentTime, ended, pause, volume, muted,
- canPlayType(): verify if the browsers supports a codec or type of file
My own failed attempt at making music
<article>
<h1>My own failed attempt at making music</h1>
<audio controls>
<source src="..media/minedenuit.ogg">
</audio>
<footer>© Copyright Me 1997</footer>
</article>
The codec debate
- Of patents...
- H.264: MPEG-LA, supported by Internet Explorer 9, Safari 3+ and Chrome 4
- ogg theora: Xiph.org Foundation, supported by Firefox 3.5+, Opera 10.5 and Chrome 4
- VP8 - WebM, Google, will be supported by Firefox, Opera, Chrome and Internet Explorer 9 (with a separate install)
What about DRM ?
Apple vs Google vs Adobe vs Microsoft vs Opera vs Mozilla
App store applications vs Open Web vs something in the middle
Web graphics
The canvas element and 2D API
- width and height attributes
- API : getContext
- Detection
- Canvas state (save() and restore())
- moveTo(), lineto(), beginPath(), closePath(), arcTo(), etc..
- stroke(), fill(), rect(), arc(), clip()
- Transformation matrix
<canvas id="myCanvas" width="400" height="120">
</canvas>
<script type="text/javascript">
if (Modernizr.canvas) {
var canvasCtx = document.getElementById("myCanvas").getContext("2d");
canvasCtx.fillStyle = "rgb(0,100,0)";
canvasCtx.translate(150, 50);
canvasCtx.rotate(45 * Math.PI / 180);
canvasCtx.fillRect(0,0,50,50);
}
</script>
SVG in HTML
- Vector graphics
- <circle>, <text>,<rect>, etc
- In application/xml+xhtml XHTML serialization for inline embedding; in javascript for text/html
- You can use iframe to embed an external svg file
- Internet Explorer 9 support!!!!
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
<g transform="translate(50, 0)">
<g transform="rotate(45)">
<rect width="50" height="50" style="fill:rgb(0,100,0);"/>
</g>
</g>
</svg>
Local / Offline storage
- Session storage
- Storage available to a session in a window / tab
- sessionStorage.setItem(key, stringValue); //(the value must be a string, so serialize objects with JSON)
- sessionStorage.getItem(key);
- sessionStorage.removeItem(key);
- Local storage
- Storage available for the current domain
- localStorage.setItem(key, stringValue); //(the value must be a string, so serialize objects with JSON)
- localStorage.getItem(key);
- localStorage.removeItem(key);
- IndexedDB / Web SQL Database
- Database available in the browser (with SQL and transactions)
Offline application caching
- manifest attribute for <html>
- links to a manifest file sent as text/cache-manifest
- the file specifies the resources to cache, non cacheable resources and fallback for unavailble resources
CACHE MANIFEST
/images/spacer.gif
NETWORK:
ajaxresponder.cgi
FALLBACK
dynamicimage.cgi staticimage.png
Web Forms: New input types
- <input type="search">
- <input type="number">
- <input type="range">
- <input type="color">
- <input type="tel">
- <input type="url">
- <input type="email">
- <input type="date">
- <input type="time">
- <input type="datetime">
- <input type="datetime-local">
- <input type="month">
- <input type="week">
- New attributes : placeholder, autocomplete, autofocus, list, pattern, required
- Detection: create an element, then verify the type (available if not text)
- Multiple file upload! (multiple attribute on type="file" input
<progress>
Represents the prgress of a task (ex: mutiple page form entry, multiple ajax calls)
<section>
<h1>Form completion progress</h1>
<p><progress value="10" max="100">10%</progress></p>
</section>
<meter>
- Represents a measurement wihtin a known range
- attributes : min, max, value, optimal
- Example :
3/5 of this presentation is done
<meter min="0" max="150" value="50">3/5 of this presentation is done</meter>
Semantic Web and distributed extensibility
Microdata
- Anotate HTML elements with custom vocabularies
- Based on key / value
- Scoping with itemscope attribute
- Namespace with itemtype attribute
- Key is the itemprop attribute
- Value depends on the HTML element (href, src or inner text)
- Does not make sense out of HTML
<section itemscope itemtype="http://programmeurspolyglottes.net/sociallinks>
<h1>Social media profiles links</h1>
<section>
<h1> itemprop="name">Benoit Piette</h1&>
<ul>
<li><a itemprop="twitter" href="http://twitter.com/benoitpiette">Twitter</a>
<li><a itemprop="identi.ca" href="http://identi.ca/benoitpiette">Identi.ca</a>
<li><a itemprop="facebook" href="http://www.facebook.com/benoitpiette">Facebook</a>
<li><a itemprop="linkedin"
href="http://ca.linkedin.com/pub/benoit-piette/6/2aa/571">LinkedIn</a>
</ul>
</section>
</section>
HTML5 + RDFa
- Semantic Web, simpler than RDF in XML
- Based on triples (subject, predicate, object)
- Uses namespaces so problematic in text/html
- property attribute for inner text
- rel attribute for href
- Adopted by Facebook (Opengraph) and Drupal
<section typeof="sl:Profiles" xmlns:sl="http://programmeurspolyglottes.net/sociallinks>
<h1>Social media profiles links</h1>
<section>
<h1 property="sl:name">Benoit Piette</h1&>
<ul>
<li><a rel="sl:twitter" href="http://twitter.com/benoitpiette">Twitter</a>
<li><a rel="sl:identi.ca" href="http://identi.ca/benoitpiette">Identi.ca</a>
<li><a rel="sl:facebook" href="http://www.facebook.com/benoitpiette">Facebook</a>
<li><a rel="sl:linkedin"
href="http://ca.linkedin.com/pub/benoit-piette/6/2aa/571">LinkedIn</a>
</ul>
</section>
</section>
data- attributes
- Custom data attributes, private to the page, internal metadat (not reusable)
- Can be specified on any HTML attribute
- element.dataset is not supported yet, but you can use element.getAttribute("data-...")
<p>Who has the shortest straw?</p>
<ul>
<li data-length="12">Ben's straw
<li data-length="5">Dave's straw
<li data-length="10">Ken's straw
</ul>
Interactive elements
<details> and <summary>
- Disclosure widget (click to show more)
- open attribute: details are shown or not the user
- the <summary> element is the always shown part
- the rest of the elements inside the <details> are not shown by default
<details>
<summary>More Info:</summary>
<p>More information about somethiing</p>
</details>
<command> and <menu>
The <command> element defines a command widget in a Web application, similar to a link or button, but specific to a client side application (not an url or a form action)
The <menu< element defines a list of commands, that can be a toolbar, a contextual menu or a simple list of commands. Do not confuse with <nav> (urls)
<menu type="toolbar">
<command type="command" label="New" icon="new.png">
<command type="command" label="Save" icon="save.png">
<command type="command" label="Load" icon="load.png">
</menu>
Geolocation
- Very useful for Mobile Web application
- Strictly speaking, is not part of HTML5
<script type="text/javascript">
function showCoords(position) {
document.getElementById("coordtest").innerHTML =
("<p>Coords: " + position.coords.latitude +
", " + position.coords.longitude + "</p>");
}
if (navigator.geolocation != undefined) {
navigator.geolocation.getCurrentPosition(showCoords);
} else {
document.getElementById("coordtest").innerHTML =
"<p>Geolocation not available</p>";
}
</script>
Web Sockets
- Strictly speaking, not part of HTML5
- Bidirectional communications (not HTTP)
- scheme: ws://example.com:12010/communication
- ex:
var socket = new WebSocket("ws://example.com:12010/communication"); - Only Chrome support for now (2010-05-27)
Web Workers
- Enables part of your Web Application's javascript to run in parralel (another thread)
- A Web worker does not have access to the DOM, just setInterval, setTimeout and XMLHTTPRequest
- Useful for intensive calculations and long XMLHTTPRequest
var myWorker = new Worker("worker-test.js");
myWorker.onmessage = function(evt) {
document.getElementById("myID").innerHTML = evt.data;
}
myWorker.postMessage(500);
From worker-test.js :
onmessage = function (evt) {
// do something intensive with evt.data
// send a message back with the results
postMessage(result);
}
Drag and Drop and File API
- Done with dragstart, dragover, dragenter, dragleave and drop events
- Must cancel the default event on the element
- Images can be dragged by default (but not dropped)
- For other elements, add draggable="true" (and / or manually add the dragstart event)
- File API, access to local files from input type file or when dragged into the browser
- Can read as text, binary (image)
Accessibility and WAI-ARIA
- Does HTML5 have bad accessibility?
- summary, alt and longdesc attribute problems in the specs (impossibility or don't work (?))
- HTML5 objective to make the maximum accessible out of the box
- WAI-ARIA: Accessible Rich Internet Application Suite : making complex Web application more accessible
ARIA roles
- Many HTML elements have native aria role mapping (input types, semantic elements like header and nav)
- Some are overidable (<a>)
- Some don't have any (<div>, <span>)
- Support by screen readers is getting there
Examples of ARIA roles:
- main
- defines the main content of the site
- navigation
- defines a collection of links
- tree and treeitem
- defines trees (ex: explorer left pane type interface)
<section role="main">
Lorem ipsum dolor sit amet
</section>
ARIA states and properties
State and properties provide specific information about an object (Supported state and properties WAI-ARIA)
Here are a couple of examples of aria states and properties
- aria-describedby
- Provides additional details / description to an element
- aria-labeledby
- Provides information on what the element does
- aria-live
- Defines an element that can be updated (ex: via AJAX)
- aria-grabbed
- if set to true, means that en element has been grabbed (set to false when dropped)
<!-- The div is updated by scripts / ajax and assistive
technology should announce the update -->
<div id="updatedInfo" aria-live="polite">
</div>
HTML5 Doctype
<!DOCTYPE html>
It's there just to trigger standard mode rendering in browsers
Not necessery in the XHTML serialization
HTML5 Serialization
- text/html
- HTML5
- application/xml+xhtml
- XHTML5
- XHTML 2 may be not be maintained anymore, but XHTML lives on in XHTML5 serialization
- No more appendix C (cannot send XHTML as text/html)
- HTML accept XHTML syntax for self closing elements (<br />) to ease transition from XHTML 1.x
- XHTML cannot have <noscript>, document.write in javascript, etc
- Polyglot documents have the same serialization (XHTML and HTML)
Other things of interest
- No more doctype and versions?
- the hidden attribute
- srcdoc and sandbox
- embed! You read right.
- "block level" links, placeholder links
- Feature detection : modernizr
- createElement hack for supporting semantic elements in Internet Explorer
- DOM API : document.querySelector, querySelectorAll (finds elements via CSS selectors), document.getElementsByClassName
- contenteditable attribute: creates an online rich text editor (since Internet Explore 5.5 ?)
- Cross window messaging (postMessage and message event)
- WebGL 3D!
- Experimental validation with http://validator.nu
- HTML5shiv, Modernizr
No longer in HTML
- basefont
- big
- center
- font
- s
- strike
- tt
- u
- frame
- frameset
- noframes
- acronym
- applet
- isindex
- dir
A word on CSS3
- Rounded corners !!
- padding:5px;
width:50%;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius:10px; - Text Shadow
- text-shadow: 4px 4px 4px #555;
- Web Fonts
- @font-face {font-family: 'FuroreRegular'; src: url('Furore-webfont.eot');}
- Selectors
- Attributes selectors ([attribute^=value], [attribute$=value], [attribute*=value])
- Box Shadow
- box-shadow: 15px 15px 15px #555; + (don't forget prefix for browser -webkit, -moz, etc)
Multiple columns, generated content, overflow-x, overflow-y
What can I use right now!
- On all browsers with significant share
- Stuff that degrades well (CSS rounded corners), ARIA, data- attributes, microdata, block level links, doctype, xhtml like notation, contenteditable
- Internet Explorer 8 and up
- Local storage, more CSS
- Internet Explorer 9+
- CSS3, SVG and more ?
- Webkit based mobile Web (+Opera)
- Local storage, canvas, geolocation
- On technical blogs
- Semantic elements
Further reading
- HTML5Doctor: Has been a big inspiration for content on this presentation ; thanks to the Doctors!
- HTML5Demos.com: Also a great ressources for code.
- Dive Into HTML5: Great online book.
- Compatibility matrix
- HTML5, ARIA Roles, and Screen Readers in May 2010
- HTML5 differences from HTML4
- www.css3.info: Great Resource on CSS3
- W3Qc: Web Standards in Quebec!
Comments? Questions?
Thank You!
Demo Time: Canvas, Contenteditable, Web Worker, getElementsByClassName and Local Storage Demo
One more thing: let's change a WordPress template live!