future web layouts with chris mills

110
Future Layouts Thursday, 14 March 13

Upload: fitc

Post on 27-Jan-2015

104 views

Category:

Technology


0 download

DESCRIPTION

We’ve been publishing documents, pages, apps, books and cat pictures on the Web for around 20 years now, so it is very surprising that we’ve never really had much in the way of easily manipulable, proper tools for creating web layouts. For years we relied on spacer gifs and horrible abuses of table markup, because nothing else had anything approaching cross browser support. Yes, we’ve had CSS for a long time, but positioning doesn’t have all the answers, and using floats for multiple columns is a hack. In 2012/2013, we are just starting to see proper layout tools for the web emerge and get support in browsers. In this talk Chris Mills will take you through what’s available in the CSS3/4 layout specs, what can be used now in production projects, and what’s coming up on the horizon, including media queries, flexible box, multi-column layout, grids, regions, and more. Your cat pictures will never have looked better.

TRANSCRIPT

Page 1: Future Web Layouts with Chris Mills

FutureLayouts

Thursday, 14 March 13

Page 3: Future Web Layouts with Chris Mills

In the early days

✴ We thought the Web was print✴ Limited device landscape meant

limited outlook✴ Limited toolset

Thursday, 14 March 13

Page 4: Future Web Layouts with Chris Mills

Print means

✴ Designer has full control✴ Technologies will be supported✴ Canvases are fixed

Thursday, 14 March 13

Page 5: Future Web Layouts with Chris Mills

Web means

✴ Designer has less control✴ Technologies won’t necessarily be

supported✴ Canvases are variable

Thursday, 14 March 13

Page 6: Future Web Layouts with Chris Mills

Limited devices...

✴ We had desktops and laptops✴ Of around 480 x 320, 640 x 480...✴ ...or 800 x 600 if you were really

posh✴ (We’ve come a long way since

then)

Thursday, 14 March 13

Page 7: Future Web Layouts with Chris Mills

...means limited outlook

✴ Designing for the page✴ Fixed, inflexible designs✴ Mobile needed a separate site?✴ WAP was a good idea?

Thursday, 14 March 13

Page 8: Future Web Layouts with Chris Mills

Limited toolset

✴ We didn’t have a good toolset for layout

✴ CSS came along soon after...✴ ..but CSS support didn’t✴ So we got into HTML tables and

spacer GIFs

Thursday, 14 March 13

Page 9: Future Web Layouts with Chris Mills

Consistent CSS support

✴ Was very welcome...✴ Still didn’t give us much in the way

of layout

Thursday, 14 March 13

Page 10: Future Web Layouts with Chris Mills

CSS2 gave us

✴ floats: limiting, and kind of abused.✴ positioning: feh.✴ margins, padding: pfffff.✴ borders.✴ meh.

Thursday, 14 March 13

Page 11: Future Web Layouts with Chris Mills

We still have problems

At this point, there was still a raft of design problems that were absurdly difficult to solve on the web.

Thursday, 14 March 13

Page 12: Future Web Layouts with Chris Mills

What about

✴ Centering stuff? ✴ Same height columns?✴ Flexible multiple columns?✴ Complex floats?✴ Shrinkwrapping contents?✴ etc.

Thursday, 14 March 13

Page 13: Future Web Layouts with Chris Mills

Centering stuff™

<figure> <div> <img src="../base-styles/grim-north.jpg" alt="A map of the North of England"> <figcaption>Figure 1.1: It's grim up North.</figcaption> </div></figure>

Thursday, 14 March 13

Page 14: Future Web Layouts with Chris Mills

Centering stuff™

figure { text-align: center;}

figure div { display: inline-block; ...}

Thursday, 14 March 13

Page 15: Future Web Layouts with Chris Mills

Same height columns

Thursday, 14 March 13

Page 16: Future Web Layouts with Chris Mills

Same height columns

✴ We can fix this with faux columns✴ Or JavaScript✴ But really? Should we need to?

Thursday, 14 March 13

Page 17: Future Web Layouts with Chris Mills

Fortunately we have

A New Hope

Thursday, 14 March 13

Page 18: Future Web Layouts with Chris Mills

New tools on the horizon

✴ CSS WG + browser vendors✴ Hard at work

Thursday, 14 March 13

Page 19: Future Web Layouts with Chris Mills

Media queries

Thursday, 14 March 13

Page 20: Future Web Layouts with Chris Mills

Media queries

✴ Same content, different layouts✴ Polyfills/fixed layouts for old IE

Thursday, 14 March 13

Page 21: Future Web Layouts with Chris Mills

Media queries

media="screen and (max-width: 480px)"

@media screen and (max-width: 480px) { /* do amazing stuff for narrow screens */}

Thursday, 14 March 13

Page 22: Future Web Layouts with Chris Mills

Media queries

/* Mobile first - make your mobile layout the default... */

@media screen and (min-width: 481px) { /* ...then do amazing stuff for wider screens */}

Thursday, 14 March 13

Page 23: Future Web Layouts with Chris Mills

Media queries

Thursday, 14 March 13

Page 24: Future Web Layouts with Chris Mills

matchMedia

✴ matchMedia = media queries inside script

✴ For IE<10 and Opera Presto, polyfill github.com/paulirish/matchMedia.js/

Thursday, 14 March 13

Page 25: Future Web Layouts with Chris Mills

matchMedia example

if (window.matchMedia("(min-width: 480px)").matches) { /* Do stuff for wider screens */} else { /* Do stuff for narrow screens */}

Thursday, 14 March 13

Page 27: Future Web Layouts with Chris Mills

viewport/CSS device

adaptation

Thursday, 14 March 13

Page 28: Future Web Layouts with Chris Mills

Mobile browsers lie

✴ About viewport width✴ Some also lie about the resolution✴ So media queries alone don’t cut it

Thursday, 14 March 13

Page 29: Future Web Layouts with Chris Mills

Mobile misbehavior

Thursday, 14 March 13

Page 30: Future Web Layouts with Chris Mills

Viewport

<meta name="viewport" content="width=device-width">

Thursday, 14 March 13

Page 31: Future Web Layouts with Chris Mills

Better

Thursday, 14 March 13

Page 32: Future Web Layouts with Chris Mills

@viewport

✴ Because viewport is more of a style thing

✴ Specify viewport inside the @viewport rule

✴ Opera Presto, IE10, WebKit

Thursday, 14 March 13

Page 33: Future Web Layouts with Chris Mills

Viewport

@viewport { width: device-width;}

Thursday, 14 March 13

Page 35: Future Web Layouts with Chris Mills

Flexbox

Thursday, 14 March 13

Page 36: Future Web Layouts with Chris Mills

Flexbox rocks

✴ Allows easy flexible layouts✴ Content reordering✴ Same height columns✴ Cure for cancer?✴ Chrome, Opera Presto, FF, IE (old

syntax)

Thursday, 14 March 13

Page 37: Future Web Layouts with Chris Mills

An example

<section> <nav> ... </nav> <article> ... </article> <article> ... </article></section>

Thursday, 14 March 13

Page 38: Future Web Layouts with Chris Mills

An example

section { display: flex;}

Thursday, 14 March 13

Page 39: Future Web Layouts with Chris Mills

Gives you

Thursday, 14 March 13

Page 40: Future Web Layouts with Chris Mills

Rows and columns

section { display: flex; flex-direction: row; flex-wrap: nowrap;}

/* flex-flow is shorthand for flex-direction/ wrap */

Thursday, 14 March 13

Page 41: Future Web Layouts with Chris Mills

Main and cross axis

Thursday, 14 March 13

Page 42: Future Web Layouts with Chris Mills

align items on main axis

section { justify-content: center;}

/* can take other values such as flex-start, flex-end, space-between and space-around */

Thursday, 14 March 13

Page 43: Future Web Layouts with Chris Mills

Thursday, 14 March 13

Page 44: Future Web Layouts with Chris Mills

align items on cross axis

section { align-items: stretch;}

/* can take other values such as flex-start, flex-end, and center; */

Thursday, 14 March 13

Page 45: Future Web Layouts with Chris Mills

Thursday, 14 March 13

Page 46: Future Web Layouts with Chris Mills

Ordering elements

✴ Reorder child elements easily and quickly

✴ Without screwing with the DOM

Thursday, 14 March 13

Page 47: Future Web Layouts with Chris Mills

Ordinal groups

nav { order: 1;}

Thursday, 14 March 13

Page 48: Future Web Layouts with Chris Mills

Gives you

Thursday, 14 March 13

Page 49: Future Web Layouts with Chris Mills

Flexing our muscle

✴ The flex property✴ Set what proportion of available

space child elements occupy✴ Unit-less proportion values

Thursday, 14 March 13

Page 50: Future Web Layouts with Chris Mills

Flex property

nav { flex: 1;}

article { flex: 3;}

Thursday, 14 March 13

Page 51: Future Web Layouts with Chris Mills

Gives you

Thursday, 14 March 13

Page 52: Future Web Layouts with Chris Mills

But there’s more

article { flex: 3 2 400px;}

/* flex-grow shares out positive spaceflex-shrink shares out overflow reductionflex-basis initially applied = CAN GET BLOODY COMPLICATED */

Thursday, 14 March 13

Page 53: Future Web Layouts with Chris Mills

A case study

Thursday, 14 March 13

Page 54: Future Web Layouts with Chris Mills

Placing the nav

section { display: flex; flex-flow: row wrap;}

nav { flex: 1 100%;}

Thursday, 14 March 13

Page 55: Future Web Layouts with Chris Mills

Flexible awesome nav!

Thursday, 14 March 13

Page 56: Future Web Layouts with Chris Mills

Making awesome

nav { display: flex; justify-content: center; }

nav ul { display: flex; flex-flow: row wrap; justify-content: center; width: 80%;}

Thursday, 14 March 13

Page 57: Future Web Layouts with Chris Mills

Making awesome

nav ul li { flex: auto; min-width: 5rem;}

Thursday, 14 March 13

Page 59: Future Web Layouts with Chris Mills

Multi-column

Thursday, 14 March 13

Page 60: Future Web Layouts with Chris Mills

Multi-col layouts

✴ Break content into multi-col✴ Cut down on markup cruft✴ Specify column breaks, column

rules and heading span✴ Most modern browsers have this

Thursday, 14 March 13

Page 61: Future Web Layouts with Chris Mills

Great at some things

<article>...</article>

article { column-count: 2; column-gap: 1rem; column-rule: 2px solid rgba(0,0,255,0.25);}

Thursday, 14 March 13

Page 62: Future Web Layouts with Chris Mills

Gives you

Thursday, 14 March 13

Page 63: Future Web Layouts with Chris Mills

Controlling columnbreaks

article h2 { break-before: column; break-after: avoid-column;}

Thursday, 14 March 13

Page 64: Future Web Layouts with Chris Mills

Gives you

Thursday, 14 March 13

Page 65: Future Web Layouts with Chris Mills

Column-spanning headings

article h2 { break-after: avoid-column; column-span: all;}

Thursday, 14 March 13

Page 66: Future Web Layouts with Chris Mills

Gives you

Thursday, 14 March 13

Page 67: Future Web Layouts with Chris Mills

Can also specifycolumn width

article { column-width: 20rem; column-gap: 2rem;}

Thursday, 14 March 13

Page 68: Future Web Layouts with Chris Mills

Gives you Thursday, 14 March 13

Page 69: Future Web Layouts with Chris Mills

Not so great at other things

✴ No proper multi-column layouts✴ break properties are fiddly✴ Only works for simple layouts

Thursday, 14 March 13

Page 71: Future Web Layouts with Chris Mills

Grids

Thursday, 14 March 13

Page 72: Future Web Layouts with Chris Mills

CSS grid layout

✴ A proper grid system for the web✴ Completely remove content from

any layout concern✴ IE10 only at the moment✴ Spec in flux

Thursday, 14 March 13

Page 73: Future Web Layouts with Chris Mills

Define your grid

body { display: grid; grid-columns: 4% 20% 20% 12% 20% 20% 4%; grid-rows: 300px 450px 450px 450px 300px;}

Thursday, 14 March 13

Page 74: Future Web Layouts with Chris Mills

Define your gridcontents

header { grid-column-position: 1; grid-row-position: 1; grid-column-span: 7;}

Thursday, 14 March 13

Page 75: Future Web Layouts with Chris Mills

Thursday, 14 March 13

Page 76: Future Web Layouts with Chris Mills

http://24ways.org/2012/css3-grid-layout/ (out of date syntax, but gives good overview)

More on grids

Thursday, 14 March 13

Page 77: Future Web Layouts with Chris Mills

Regions

Thursday, 14 March 13

Page 78: Future Web Layouts with Chris Mills

CSS regions

✴ Turn containers into vessels to flow content into

✴ Flexible complex layouts✴ IE10 and Chrome Canary only at the

moment

Thursday, 14 March 13

Page 79: Future Web Layouts with Chris Mills

Put your content in a separate block

<article class="content"> <h2>Introduction</h2> <p>Hello, dear readers...</article>

Thursday, 14 March 13

Page 80: Future Web Layouts with Chris Mills

Then create your layout blocks

<div class="layout"> <div class="text-container"></div> <div class="text-container"></div> <div class="image-container"> ... </div> <div class="text-container"></div></div><div class="text-overflow"></div>

Thursday, 14 March 13

Page 81: Future Web Layouts with Chris Mills

Specify where toflow it into

.content { -webkit-flow-into: article;}

.text-container, .text-overflow { -webkit-flow-from: article;}

Thursday, 14 March 13

Page 82: Future Web Layouts with Chris Mills

A little something I cooked up

Thursday, 14 March 13

Page 83: Future Web Layouts with Chris Mills

Exclusions and shapes

Thursday, 14 March 13

Page 84: Future Web Layouts with Chris Mills

CSS exclusions

✴ Create really complex floats✴ Flow content around (and inside)

complex shapes✴ Chrome Canary/IE only at the

moment

Thursday, 14 March 13

Page 85: Future Web Layouts with Chris Mills

Thursday, 14 March 13

Page 86: Future Web Layouts with Chris Mills

Position your exclusion

<article class="content"> <header> ... </header> ...</article>

header { position: absolute; etc. }

Thursday, 14 March 13

Page 87: Future Web Layouts with Chris Mills

Then exclude it!

header { position: absolute; etc. wrap-flow: both; /* Can also take values of start, end, minimum, maximum, clear */}

Thursday, 14 March 13

Page 88: Future Web Layouts with Chris Mills

Different effects

Text

both start end

minimum maximum clear

Thursday, 14 March 13

Page 89: Future Web Layouts with Chris Mills

Shape your exclusion

shape-inside: rectangle(0, 0, 100%, 100%, 25%, 25%);

shape-inside: polygon( ... )

shape-outside: polygon( ... )

Thursday, 14 March 13

Page 90: Future Web Layouts with Chris Mills

shape inside/outside

Thursday, 14 March 13

Page 91: Future Web Layouts with Chris Mills

My examples...

✴ ...didn’t work✴ Apparently wrap-flow is in IE10✴ and shape-inside/outside in

Chrome Canary...

Thursday, 14 March 13

Page 92: Future Web Layouts with Chris Mills

A note onCSS units

Thursday, 14 March 13

Page 93: Future Web Layouts with Chris Mills

✴ rem units used throughout my examples

✴ size relative to the root (html) font-size, not the parent font size.

✴ Much easier maths

rems

Thursday, 14 March 13

Page 94: Future Web Layouts with Chris Mills

✴ Percentage of viewport size✴ 1vw = 1% of viewport width✴ 1vh = 1% of viewport height✴ 1vmin = 1vw or 1vh, whatever is

smallest

vh, vw, and vmin

Thursday, 14 March 13

Page 95: Future Web Layouts with Chris Mills

✴ Supported in IE10, FF, Chrome, iOS, Blackberry?

✴ text-size relative to viewport = accessibility problem?

vh, vw, and vmin

Thursday, 14 March 13

Page 96: Future Web Layouts with Chris Mills

New responsive capabilities

Thursday, 14 March 13

Page 97: Future Web Layouts with Chris Mills

Fallbacks and alternatives

Thursday, 14 March 13

Page 98: Future Web Layouts with Chris Mills

✴ A lot of this stuff doesn’t degrade very gracefully

✴ Not a surprise, as layout is a pretty big deal

In truth

Thursday, 14 March 13

Page 99: Future Web Layouts with Chris Mills

So do we just wait until support is everywhere and IE6-9 is destroyed?

Thursday, 14 March 13

Page 100: Future Web Layouts with Chris Mills

✴ Intelligent alternatives via feature detection

✴ @supports: native feature detection

✴ Modernizr is still an excellent solution

Hell no!

Thursday, 14 March 13

Page 101: Future Web Layouts with Chris Mills

@supports

/* Provide basic layout, e.g. with floats */

@supports (display:flex) { /* Provide Flexbox layout for supporting browsers */}

Thursday, 14 March 13

Page 102: Future Web Layouts with Chris Mills

Modernizr

<html lang="en-US" class="no-js"><head> <script src="modernizr.js"></script></head>

Thursday, 14 March 13

Page 103: Future Web Layouts with Chris Mills

Modernizr

<html class=" js flexbox canvas canvastext webgl no-touch geolocation postmessage no-websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients no-cssreflections csstransforms no-csstransforms3d csstransitions fontface generatedcontent video audio ... ">

Thursday, 14 March 13

Page 104: Future Web Layouts with Chris Mills

Modernizr CSS

.feature-no-regions .layout, .feature-no-regions .text-overflow { display: none;}

.feature-no-regions .content { float: left; width: 48%; padding: 1%;}

Thursday, 14 March 13

Page 105: Future Web Layouts with Chris Mills

Fallback basic layout

Thursday, 14 March 13

Page 106: Future Web Layouts with Chris Mills

Modernizr JS

function rotateForm() { if(Modernizr.cssanimations && Modernizr.csstransforms3d) { form.setAttribute("class","form-rotate"); form.style.left = "0rem"; } else { back.style.zIndex = "5"; } }

Thursday, 14 March 13

Page 107: Future Web Layouts with Chris Mills

Buy my book“Subtle” advertisement

Thursday, 14 March 13

Page 108: Future Web Layouts with Chris Mills

Thanks!

Thursday, 14 March 13