laying out the future

91
Laying out The future Friday, 8 March 13

Upload: chris-mills

Post on 27-Jan-2015

114 views

Category:

Technology


0 download

DESCRIPTION

This version of my main presentation for 2013, first given at the Camp Digital event in Manchester, provides an overview of the most interesting new layout features available in CSS3, along with advice on supporting older browsers, and some context around why they are important.

TRANSCRIPT

Page 1: Laying out the future

Laying outThe future

Friday, 8 March 13

Page 3: Laying out the future

In the early days

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

limited outlook✴ Limited toolset

Friday, 8 March 13

Page 4: Laying out the future

Consistent CSS support

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

of layout

Friday, 8 March 13

Page 5: Laying out the future

CSS2 gave us

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

Friday, 8 March 13

Page 6: Laying out the future

We still have problems

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

Friday, 8 March 13

Page 7: Laying out the future

What about

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

Friday, 8 March 13

Page 8: Laying out the future

Same height columns

Friday, 8 March 13

Page 9: Laying out the future

Same height columns

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

Friday, 8 March 13

Page 10: Laying out the future

Fortunately we have

A New Hope

Friday, 8 March 13

Page 11: Laying out the future

New tools on the horizon

✴ CSS WG + browser vendors✴ Hard at work

Friday, 8 March 13

Page 12: Laying out the future

Media queries

Friday, 8 March 13

Page 13: Laying out the future

Media queries

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

Friday, 8 March 13

Page 14: Laying out the future

Media queries

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

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

Friday, 8 March 13

Page 15: Laying out the future

Media queries

Friday, 8 March 13

Page 16: Laying out the future

matchMedia

✴ matchMedia = media queries inside script

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

Friday, 8 March 13

Page 17: Laying out the future

matchMedia example

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

Friday, 8 March 13

Page 19: Laying out the future

viewport/CSS device

adaptation

Friday, 8 March 13

Page 20: Laying out the future

Mobile browsers lie

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

Friday, 8 March 13

Page 21: Laying out the future

Mobile misbehavior

Friday, 8 March 13

Page 22: Laying out the future

Viewport

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

Friday, 8 March 13

Page 23: Laying out the future

Better

Friday, 8 March 13

Page 24: Laying out the future

@viewport

✴ Because viewport is more of a style thing

✴ Specify viewport inside the @viewport rule

✴ Opera Presto, IE10, WebKit

Friday, 8 March 13

Page 25: Laying out the future

Viewport

@viewport { width: device-width;}

Friday, 8 March 13

Page 27: Laying out the future

Flexbox

Friday, 8 March 13

Page 28: Laying out the future

Flexbox rocks

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

syntax)

Friday, 8 March 13

Page 29: Laying out the future

An example

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

Friday, 8 March 13

Page 30: Laying out the future

An example

section { display: flex;}

Friday, 8 March 13

Page 31: Laying out the future

Gives you

Friday, 8 March 13

Page 32: Laying out the future

Rows and columns

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

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

Friday, 8 March 13

Page 33: Laying out the future

Main and cross axis

Friday, 8 March 13

Page 34: Laying out the future

align items on main axis

section { justify-content: center;}

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

Friday, 8 March 13

Page 35: Laying out the future

Friday, 8 March 13

Page 36: Laying out the future

align items on cross axis

section { align-items: stretch;}

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

Friday, 8 March 13

Page 37: Laying out the future

Friday, 8 March 13

Page 38: Laying out the future

Ordering elements

✴ Reorder child elements easily and quickly

✴ Without screwing with the DOM

Friday, 8 March 13

Page 39: Laying out the future

Ordinal groups

nav { order: 1;}

Friday, 8 March 13

Page 40: Laying out the future

Gives you

Friday, 8 March 13

Page 41: Laying out the future

Flexing our muscle

✴ The flex property✴ Set what proportion of available

space child elements occupy✴ Unit-less proportion values

Friday, 8 March 13

Page 42: Laying out the future

Flex property

nav { flex: 1;}

article { flex: 3;}

Friday, 8 March 13

Page 43: Laying out the future

Gives you

Friday, 8 March 13

Page 44: Laying out the future

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 */

Friday, 8 March 13

Page 45: Laying out the future

A case study

Friday, 8 March 13

Page 46: Laying out the future

Placing the nav

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

nav { flex: 1 100%;}

Friday, 8 March 13

Page 47: Laying out the future

Flexible awesome nav!

Friday, 8 March 13

Page 48: Laying out the future

Making awesome

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

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

Friday, 8 March 13

Page 49: Laying out the future

Making awesome

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

Friday, 8 March 13

Page 51: Laying out the future

Multi-column

Friday, 8 March 13

Page 52: Laying out the future

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

Friday, 8 March 13

Page 53: Laying out the future

Great at some things

<article>...</article>

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

Friday, 8 March 13

Page 54: Laying out the future

Gives you

Friday, 8 March 13

Page 55: Laying out the future

Controlling columnbreaks

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

Friday, 8 March 13

Page 56: Laying out the future

Gives you

Friday, 8 March 13

Page 57: Laying out the future

Column-spanning headings

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

Friday, 8 March 13

Page 58: Laying out the future

Gives you

Friday, 8 March 13

Page 59: Laying out the future

Can also specifycolumn width

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

Friday, 8 March 13

Page 60: Laying out the future

Gives you Friday, 8 March 13

Page 62: Laying out the future

Regions

Friday, 8 March 13

Page 63: Laying out the future

CSS regions

✴ Turn containers into vessels to flow content into

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

moment

Friday, 8 March 13

Page 64: Laying out the future

Put your content in a separate block

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

Friday, 8 March 13

Page 65: Laying out the future

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>

Friday, 8 March 13

Page 66: Laying out the future

Specify where toflow it into

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

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

Friday, 8 March 13

Page 67: Laying out the future

A little something I cooked up

Friday, 8 March 13

Page 68: Laying out the future

Exclusions and shapes

Friday, 8 March 13

Page 69: Laying out the future

CSS exclusions

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

complex shapes✴ Chrome Canary/IE only at the

moment

Friday, 8 March 13

Page 70: Laying out the future

Friday, 8 March 13

Page 71: Laying out the future

Position your exclusion

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

header { position: absolute; etc. }

Friday, 8 March 13

Page 72: Laying out the future

Then exclude it!

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

Friday, 8 March 13

Page 73: Laying out the future

Different effects

Text

both start end

minimum maximum clear

Friday, 8 March 13

Page 74: Laying out the future

Shape your exclusion

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

shape-inside: polygon( ... )

shape-outside: polygon( ... )

Friday, 8 March 13

Page 75: Laying out the future

shape inside/outside

Friday, 8 March 13

Page 76: Laying out the future

A note onCSS units

Friday, 8 March 13

Page 77: Laying out the future

✴ rem units used throughout my examples

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

✴ Much easier maths

rems

Friday, 8 March 13

Page 78: Laying out the future

✴ 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

Friday, 8 March 13

Page 79: Laying out the future

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

✴ text-size relative to viewport = accessibility problem?

vh, vw, and vmin

Friday, 8 March 13

Page 80: Laying out the future

New responsive capabilities

Friday, 8 March 13

Page 81: Laying out the future

Fallbacks and alternatives

Friday, 8 March 13

Page 82: Laying out the future

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

Friday, 8 March 13

Page 83: Laying out the future

✴ Intelligent alternatives via feature detection

✴ @supports: native feature detection

✴ Modernizr is still an excellent solution

Hell no!

Friday, 8 March 13

Page 84: Laying out the future

Modernizr

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

Friday, 8 March 13

Page 85: Laying out the future

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 ... ">

Friday, 8 March 13

Page 86: Laying out the future

Modernizr CSS

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

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

Friday, 8 March 13

Page 87: Laying out the future

Fallback basic layout

Friday, 8 March 13

Page 88: Laying out the future

Modernizr JS

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

Friday, 8 March 13

Page 89: Laying out the future

Buy my book“Subtle” advertisement

Friday, 8 March 13

Page 90: Laying out the future

Thanks!

Friday, 8 March 13