html5 and css3 shizzle
DESCRIPTION
I gave this presentation at the 2010 Scotch on the Rocks conference. It features a brief explanation of why HTML5 and CSS3 are necessary, and then goes on to cover most of the cool new features of HTML5 and CSS3 that are supported across most browsers at the time of publication., including , , HTML5 forms, the new HTML5 elements, box-shadow, text-shadow, web fonts, media queries, and more. Companion code examples are available at http://people.opera.com/cmills/HTML5_CSS3_examples.zipTRANSCRIPT
The HTML5 & CSS3 Shizzle
Chris Mills, Opera Software, all the way from “sunny” Oldham
Slides available
At my.opera.com/ODIN(search for “chrismills” tag)
I work for Opera
Open web standards evangelistTechnologistTech writerDogsbody...
...secret Flash fan
What we'll cover
Why new standards?Things we can use todayThe future
Evolution...
There is nothing wrong with HTML 4 and CSS 2
...Evolved!But HTML5 and CSS3 are much more feature-rich!
HTML5 and CSS3 aren't replacementsThey are backwards compatibleThey fill up gapsThey add new features on top of the old specs
They, um, compete with plugins
Ian Hickson has already said as much.HTML5 will directly compete with other web application technologies, like Flash and Silverlight
HTML5 featuresMore accurate semantics (eg <header>, <footer>)Better forms (built in validation!)<video><canvas>
HTML5 features (cont.)
Drag and dropWeb workersWeb storage, app cache, webdb...and more
New syntax: better semantics
Typical HTML4 page<!DOCTYPE HTML PUBLIC "-//W3C//DTD ... et fucking cetera... <head></head><body> <div id=”masthead”></div> <div id=”nav”></div> <div id=”content”> <div id=”content-main”> <div id=”article”></div> <div id=”article”></div> </div> <div id=”side-content”> </div> </div> <div id=”footer”></div></body>
HTML5 page<!doctype html>
<header></header> <nav></nav> <section id=content> <section id=content-main> <article></article> <article></article> </section> <section id=side-content> </section> </section> <footer></footer>
Deeper into the page...<article> <header> <h1>Article about Chris Mills</h1> </header> <p>Article text...</p> <figure> <img src=chris.jpg alt=”Chris Mills”> <figcaption>Chris Mills</figcaption> </figure> <footer> <p>Published <time datetime=2010-05-06 pubdate>May 6th 2010</time></p> </footer></article>
Better semantics?
Document parts now have clear meaning More machine readableArguably easier for AT to readEasier to write Easier to target with CSS and JS
When can we use these?Now. Browsers don't do anything w/ themBut you can do display: block; And get into the habitStill machine-readable
HTML5 forms
Previously called “Web forms 2.0”More powerful form elementsBuilt-in validationMore standard archetypes
Slider<input type=range>
Calendar widget<input type=date>
Datalist<input type=text list=bands required><datalist id=bands> <option value="Napalm Death">Napalm Death</option> <option value="Britney Spears">Britney Spears</option> <option value="Akon">Akon</option> </datalist>
URL picker, E-mail input<input type=url>
<input type=email>
Client-side validation
Is screwed up in HTML4...
Ok, not that screwed up...
function validate() { var str = ""; var elements = document.getElementsByTagName('input'); // loop through all input elements in form for(var i = 0; i < elements.length; i++) { // check if element is mandatory; ie has a pattern var pattern = elements.item(i).getAttribute('pattern'); if (pattern != null) { var value = elements.item(i).value; // validate the value of this element, using its defined pattern var offendingChar = value.match(pattern); // if an invalid character is found or the element was left emtpy if(offendingChar != null || value.length == 0) { // add up all error messages str += elements.item(i).getAttribute('errorMsg') + "\n" + "Found this illegal value: '" + offendingChar + "' \n"; // notify user by changing background color, in this case to red elements.item(i).style.background = "red"; } } } if (str != "") { // do not submit the form alert("ERROR ALERT!!\n" +str); return false; } else { // form values are valid; submit return true; } }
HTML5 built-in validation<input type=email required>
Autofocus<input type=email requiredautofocus>
HTML5 <canvas>
Scriptable graphicsStandard API for drawingSupported in most browsers
The basics
<canvas id=”canvas” width=”400” height=”300”>...fallback...</canvas>
The basics
var ctx = document.getElementById('canvas').getContext('2d');
ctx.fillStyle='#00f';ctx.fillRect(0, 0, 150, 100);
Example time!
nihilogic.dk has cool stuff on itdev.opera.com has good articles
HTML5 <video> (& <audio>)
New tags, plus new API for controlling audio and video!
The old school way<object width="425" height="344"><param name="movie" value="http://www.example.com/v/LtfQg4KkR88&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.example.com/v/LtfQg4KkR88&hl=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>
The trouser-tinglingly sexy new way<video src="video.webm" controls autoplay loop poster="poster.jpg" preload=”none” width="320" height="240"> <a href="video.webm">Download movie</a></video>
Native <video> is awesome
Works well with open standardsBuilt-in keyboard accessibilityAPI for customising controls, etc.DOESN'T require plugins
<video> problems
Disagreement on formats:H.264Ogg TheoraWebM new to the table
You can provide fallbacks
Different sources
<video width=640 height=480 controls> <source src="video.ogv" type="video/ogg"> <source src="video.mp4" type="video/mp4"> <a href="video.mp4">Download movie</a> </video>
<video> problems (cont.)
Built-in captioning?Currently not ;-(You can build a workaround though
<video> captionsfunction timeupdate() { var v = document.querySelector('video'); var now = v.currentTime; …}
<video width=600 src=synergy.ogv ontimeupdate=timeupdate()><p> <span data-begin=1 data-end=2.4>Hello, Good Evening</span> <span data-begin=3 data-end=3.6> and Welcome.</span></p>
CSS3...
Will save you a lot of time previously spent in Photoshop
Allows you to create the same tacky shit...
...that you did in Flash in 1999!!
text-shadow
text-shadow: #444 2px 2px 2px;
text-shadow: 0 0 4px white, 0 -5px 4px #ff3, 2px -10px 6px #fd3, -2px -15px 11px #f80, 2px -25px 18px #f20;
box-shadow
box-shadow: 10px 10px 15px #000000;
CSS3 opacity
CSS3 colours: rgb(a)
CSS3 colours: hsl(a)
border-radius
Finally, Web 2.0 is easy!!
(Starts from top-left corner)
Transitions
Offer animation-like abilitiesSet a default state for the elementChoose property & durationThen set state to transition to
Transition default statepre#transition1 { background-color: #ff0000; -o-transition-property: background-color; -o-transition-duration: 2s;}
Transitioned statepre#transition1:hover { background-color: #ffffff;}
Transitions: easingAllows you to control the pattern of acceleration/deceleration.More natural feel.
-o-transition-timing-function: ease-in;
Transitions: delayAdd a delay before the transition starts.
-o-transition-delay: 1s;
Multiple transitionsMultiple transitions, each with their own start time.
-o-transition-property: background-color, width, height; -o-transition-duration: 4s, 8s, 5s;
Transforms (2D)Transforming element position, size, etc.: moving, rotating, skewing...
Setting transform originFor example what point does your element rotate around?
-o-transform-origin: 3em bottom;
Moving elementsIn X and Y directions
-o-transform: translateX(50px);-o-transform: translateY(100px);
Resizing elementsBy a set scale factor
-o-transform: scale(2.5);
Skewing elementsSquishy distortion!
-o-transform: skew(10deg, 20deg);
Rotating elementsAround the origin point
-o-transform: rotate(30deg);
Combining transformsDo multiple things in one declaration
-o-transform: scale(2) rotate(45deg) translate(80px);
Combining transitions with transforms......is where it starts to get really fun.
Web FontsDownload custom fonts along with your web pages
Solve the web typographer's nightmare?
Include the font@font-face { font-family: "My font"; src: url("http://www.myweb.com/fonts/ myfont.ttf") format("truetype");}
Use it in your page as normalp { font-family: "My font"; }
Web Fonts issuesGood free fonts are available, but...Many are not licensed for the WebSome also mean large downloadsSome solutions are being explored
Media queriesYou know what media types areMedia queries take the idea further Apply CSS depending on device attributes
Syntax example@media all and (max-width: 400px) {
/* CSS rules go here */
}
Browser support?
Browser support?Graceful degredation in mind...IE9 support announcementsBridging solutions:
ExcanvasHTML5ShivModernizer
Thanks!
[email protected]@chrisdavidmills
Check out dev.opera.comCheck out www.opera.com/wscTry our browsers!!