Adapt! Media queries and viewport

69
Ap! MEDIA QUERIES AND VIEWPORT 1. Tuesday, 31 July 2012

description

The world is now officially device-crazy! Just look at the number of Mobile phones and tablets sold recently, and the number of ordinary people (not just geeks) who have a mobile device and a tablet in addition to their desktop computer, or have even dispensed with the desktop computer altogether. It would be foolish of us to just design for desktop, or just for mobile. What we need is a way to optimize our layouts for a multitude of different screen sizes and other factors! In this talk Chris Mills looks at media queries and viewport in detail: Their origins in media types Why media types failed for mobile and devices, and why media queries will succeed Media query basics Practical examples of how they work across modern browsers and devices Exploring viewport - why it is needed, and how best to use viewport and MQ's together A look forward to the future, and the @viewport proposal Mobile first versus desktop first The IE problem, and how we can slay that beast in this particular context Device breakpoints versus content breakpoints. What works best?

Transcript of Adapt! Media queries and viewport

Page 1: Adapt! Media queries and viewport

Adapt!MEDIA QUERIES AND VIEWPORT

1.Tuesday, 31 July 2012

Page 2: Adapt! Media queries and viewport

Hi — I’m Chris Mills!Open standards advocate and Education agitator

dev.opera.com editor

W3C web education community group chair

Accessibility whiner

HTML5/CSS3 wrangler

Heavy metal drummer and proud dad

2.Tuesday, 31 July 2012

Page 3: Adapt! Media queries and viewport

3.Tuesday, 31 July 2012

Page 4: Adapt! Media queries and viewport

Use*l Stuffdev.opera.com

slideshare.net/chrisdavidmills

[email protected]

@chrisdavidmills

4.Tuesday, 31 July 2012

Page 5: Adapt! Media queries and viewport

Cont/t For Adaptive Design

5.Tuesday, 31 July 2012

Page 6: Adapt! Media queries and viewport

Adaptive“Able to be easily modified to suit a new purpose or situation”

6.Tuesday, 31 July 2012

Page 7: Adapt! Media queries and viewport

Responsive“Reacting quickly and positively.”

7.Tuesday, 31 July 2012

Page 8: Adapt! Media queries and viewport

RWD/AWD“Designing sites that will dynamically adapt their layout, graphics and other features to suit different browsers and devices.”

Responsive design? Adaptive design? It matters not which term you use.

8.Tuesday, 31 July 2012

Page 9: Adapt! Media queries and viewport

Responsive WebDesign is Hot!!

9.Tuesday, 31 July 2012

Page 10: Adapt! Media queries and viewport

1ree Options for creating tailored displays

1. Adapt a single site

2. Do nothing

3. Create a separate site/separate sites

10.Tuesday, 31 July 2012

Page 11: Adapt! Media queries and viewport

Creating separate sitesUsually a terrible idea

Despite what “that Nielsen bloke” says

Maintenance nightmare

Multiple codebases

Browser sniffing

And how many do you have to make?

11.Tuesday, 31 July 2012

Page 12: Adapt! Media queries and viewport

Doing nothingActually goes a long way

The web is responsive by default

Mobile browsers have features to help render desktop sites

12.Tuesday, 31 July 2012

Page 13: Adapt! Media queries and viewport

Adapt a single siteBy far the best option, usually

Let’s see how!

13.Tuesday, 31 July 2012

Page 14: Adapt! Media queries and viewport

Adaptive

technologies14.

Tuesday, 31 July 2012

Page 15: Adapt! Media queries and viewport

Adaptive Breadand Butter

Liquid layouts

Media types

Media queries

Viewport

Adaptive images

15.Tuesday, 31 July 2012

Page 16: Adapt! Media queries and viewport

Media T4es The idea of adaptive design isn’t new

Media types have been around since CSS2

media="screen"

media="print"

16.Tuesday, 31 July 2012

Page 17: Adapt! Media queries and viewport

Media T4es media="handheld"

media="tv"

17.Tuesday, 31 July 2012

Page 18: Adapt! Media queries and viewport

Small TV Rant“Searching, browsing, updating and buffering are not TV-like. In fact an enormous number of people found that the technology they had purchased wasn't what they expected at all, that they were bringing the worst parts of using a computer into the television environment.”

-- www.insideci.co.uk/news/rovi-research-reveals-changing-consumer-habits.aspx18.

Tuesday, 31 July 2012

Page 19: Adapt! Media queries and viewport

19.Tuesday, 31 July 2012

Page 20: Adapt! Media queries and viewport

Media T4es FailedBecause they make too many assumptions

And don’t give enough control

Devs used “handheld” to serve crap dumbed down experiences to mobile

“handheld”, “tv” and “projection” are really just “screen”

20.Tuesday, 31 July 2012

Page 21: Adapt! Media queries and viewport

Media Queries & Viewport

21.Tuesday, 31 July 2012

Page 22: Adapt! Media queries and viewport

Media Queries...succeed where media types failed, mostly

www.w3.org/TR/css3-mediaqueries/

Build on top of media types

Allow more granular control of style application

See mediaqueri.es for tonnes of examples

22.Tuesday, 31 July 2012

Page 23: Adapt! Media queries and viewport

Basic Media Querymedia="screen and (max-width: 480px)"

@media screen and (max-width: 480px) {

/* Insert rules for narrow screens here */

}

23.Tuesday, 31 July 2012

Page 24: Adapt! Media queries and viewport

24.Tuesday, 31 July 2012

Page 25: Adapt! Media queries and viewport

Adding /tra conditions@media screen and (min-width: 480px) and (max-width: 1024px) {

/* Rules to be applied between 480 and 1024 pixels */

}

25.Tuesday, 31 July 2012

Page 26: Adapt! Media queries and viewport

Multiple right answers@media screen and (max-width: 1024px), print and (max-width: 21cm) {

/* Rules to be applied if either of the two queries are true */

}

26.Tuesday, 31 July 2012

Page 27: Adapt! Media queries and viewport

Media Query negation@media not screen and (max-width: 480px) {

/* Rules to be applied when the screen is NOT narrow */

}

27.Tuesday, 31 July 2012

Page 28: Adapt! Media queries and viewport

5clusive Queries@media only screen and (max-width: 480px) {

/* An exclusive set of rules, ONLY for narrow screens */

}

28.Tuesday, 31 July 2012

Page 29: Adapt! Media queries and viewport

T4ical stylesheet/* Default styles here */

@media screen and (min-width: 1400px) {}

@media screen and (max-width: 800px) {}

@media screen and (max-width: 480px) {}

@media screen and (max-width: 320px) {}

29.Tuesday, 31 July 2012

Page 30: Adapt! Media queries and viewport

BreakpointsWhere do you need to change your styles?

Device breakpoint: change styles for specific target device screen sizes

Content breakpoint: changes styles at the point where your layout actually breaks

30.Tuesday, 31 July 2012

Page 31: Adapt! Media queries and viewport

Which way do you do it?For a general web site, content breakpoints

For a platform-targetted app, device breakpoints

More realistically, you’ll do a combination, eg fluid width for wide screens, a fixed width and height for iPad portrait/landscape, and a fluid width for smartphones and other devices.

31.Tuesday, 31 July 2012

Page 32: Adapt! Media queries and viewport

1e different availableMedia Querieswidth color

height color-index

device-width monochrome

device-height resolution

orientation scan

aspect-ratio grid

device-aspect-ratiodevice-aspect-ratio32.

Tuesday, 31 July 2012

Page 33: Adapt! Media queries and viewport

You will m7t commonly usewidth (and height): this is viewport w/h!

You could use device-width/height for mobiles/tablets, but this confuses things. There is a better way (see later)

resolution: for high fidelity devices

orientation: for landscape/portrait layout optimization (an iPad app, for example)

33.Tuesday, 31 July 2012

Page 34: Adapt! Media queries and viewport

Beware! Mobiles lie! Want this... ...Got this?

34.Tuesday, 31 July 2012

Page 35: Adapt! Media queries and viewport

Mobiles lie...They assume a wider viewport than they really have

Render the site at that width

Then shrink the result to fit on the screen

For example, Opera Mobile uses 980px

35.Tuesday, 31 July 2012

Page 36: Adapt! Media queries and viewport

ViewportInvented by Apple

A meta tag

Suggests initial viewport, zoom, and resolution

36.Tuesday, 31 July 2012

Page 37: Adapt! Media queries and viewport

Viewport /ample<meta name="viewport"content="width=device-width">

37.Tuesday, 31 July 2012

Page 38: Adapt! Media queries and viewport

Much better!

38.Tuesday, 31 July 2012

Page 39: Adapt! Media queries and viewport

Another common use<meta name="viewport" content="width=550">

39.Tuesday, 31 July 2012

Page 40: Adapt! Media queries and viewport

40.Tuesday, 31 July 2012

Page 41: Adapt! Media queries and viewport

Different availableViewport options

width height

minimum-scale maximum-scale

initial-scale user-scalable

target-densitydpitarget-densitydpi

41.Tuesday, 31 July 2012

Page 42: Adapt! Media queries and viewport

CSS Device Adaptation Viewport = more of a style thing than a content thing

Provides @viewport at-rule to contain viewport info

Supported in Opera and IE10, with prefixes

dev.opera.com/articles/view/an-introduction-to-meta-viewport-and-viewport

42.Tuesday, 31 July 2012

Page 43: Adapt! Media queries and viewport

CSS Device Adaptation <meta name="viewport"

content="width=550,

maximum-scale=1.0,

target-densitydpi=device-dpi">

@viewport { width: 550px; max-zoom: 1; resolution: device; }

43.Tuesday, 31 July 2012

Page 44: Adapt! Media queries and viewport

Other Responsive Web Design Issues

44.Tuesday, 31 July 2012

Page 45: Adapt! Media queries and viewport

What else have we got to worry about?

Responsive replaced elements

Bandwidth/loading of resources

Resolution

Processing power

Control mechanisms

45.Tuesday, 31 July 2012

Page 46: Adapt! Media queries and viewport

Replaced elements

46.Tuesday, 31 July 2012

Page 47: Adapt! Media queries and viewport

Replaced elements#related img {

display: block;

margin: 0 auto;

max-width: 100%;

}

47.Tuesday, 31 July 2012

Page 48: Adapt! Media queries and viewport

Be warnedOld IE versions don’t support max-width, so you’ll have to fallback to width: 100% instead.

48.Tuesday, 31 July 2012

Page 49: Adapt! Media queries and viewport

8le s9e also importantUsers on slow connections won’t want to download huge media files.

We need to try to serve them smaller files/alternatives.

Assumptions: e.g. narrow equals mobile equals slow connection

49.Tuesday, 31 July 2012

Page 50: Adapt! Media queries and viewport

CSS resources easyto deal with

Use “mobile first”

Load smaller resources by default, and larger ones inside MQs

Saves narrow screen devices from loading wide screen resources

50.Tuesday, 31 July 2012

Page 51: Adapt! Media queries and viewport

“Mobile first” stylesheet/* Default styles here */

@media screen and (min-width: 320px) {}

@media screen and (min-width: 480px) {}

@media screen and (min-width: 800px) {}

@media screen and (min-width: 1400px) {}

51.Tuesday, 31 July 2012

Page 52: Adapt! Media queries and viewport

“Mobile first” /ampleheader { background: url(small-image.png); }

@media screen and (min-width: 480px) { header { background: url(large-image.png); }

}

52.Tuesday, 31 July 2012

Page 53: Adapt! Media queries and viewport

“Mobile first” problemsIE 6-8 left with mobile layout, as they don’t support media queries

You’ll need to polyfill media queries, e.g. with respond.js

53.Tuesday, 31 July 2012

Page 54: Adapt! Media queries and viewport

HTML5 <video> well served <source src="crystal320.webm"type="video/webm" media="all and (max-width: 480px)">

<source src="crystal720.webm" type="video/webm" media="all and (min-width: 481px)">

54.Tuesday, 31 July 2012

Page 56: Adapt! Media queries and viewport

adaptive-images.comAdd .htaccess and adaptive-images.php to your document root folder.

Add one line of JavaScript into the <head> of your site.

Add your CSS Media Query values into $resolutions in the PHP file.

56.Tuesday, 31 July 2012

Page 57: Adapt! Media queries and viewport

1e <picture> element<picture alt="a picture of something"> <source src="mobile.jpg"> <source src="medium.jpg" media="min width: 600px"> <source src="fullsize.jpg" media="min width: 900px"> <img src="mobile.jpg">

</picture>

57.Tuesday, 31 July 2012

Page 58: Adapt! Media queries and viewport

1e srcset attribute<img src="face-600-200-at-1.jpeg" alt=""srcset="face-600-200-at-1.jpeg 600w 200h 1x, face-600-200-at-2.jpeg 600w 200h 2x, face-icon.png 200w 200h">

58.Tuesday, 31 July 2012

Page 59: Adapt! Media queries and viewport

Processing powerYou might want to turn off effects like shadows, gradients and animations for small screen devices

CSS effects are arguably less power draining than JS or Flash, but even so

They can cause the UI to look cluttered too

59.Tuesday, 31 July 2012

Page 60: Adapt! Media queries and viewport

ResolutionHi-fidelity devices are causing an issue, e.g. iPhone 4s is 960 x 640 pixels at 326ppi

These devices lie twice

One CSS pixel = multiple device pixels

Images can start to look pixellated

60.Tuesday, 31 July 2012

Page 61: Adapt! Media queries and viewport

Solutions<img src="500px_image.jpg" width="250">

61.Tuesday, 31 July 2012

Page 62: Adapt! Media queries and viewport

Solutions@media screen and (-o-min-device-pixel-ratio: 3/2) {

body { background-size: 250px; }

}

@media screen and (-webkit-min-device-pixel-ratio: 1.5) {

body { background-size: 250px; }

}62.

Tuesday, 31 July 2012

Page 63: Adapt! Media queries and viewport

63.Tuesday, 31 July 2012

Page 64: Adapt! Media queries and viewport

Soon to be replaced by@media screen and (resolution: 1.5dppx) {

body { background-size: 250px; }

}

64.Tuesday, 31 July 2012

Page 65: Adapt! Media queries and viewport

Tell the truth with viewport<meta name="viewport"

content="width=device-width,

target-densitydpi=device-dpi">

65.Tuesday, 31 July 2012

Page 66: Adapt! Media queries and viewport

All good, but...Images may now start to look a little small

You could serve larger images for devices with higher resolutions

66.Tuesday, 31 July 2012

Page 67: Adapt! Media queries and viewport

Control mechanismsCurrently tricky

Usual wisdom about web sites applies: Make pages keyboard accessible, etc.

Can’t be detected in CSS (yet)

JavaScript touch detection is an option: Modernizr, jQuery

67.Tuesday, 31 July 2012

Page 68: Adapt! Media queries and viewport

68.

Buy my book“Subtle” advertisment

Tuesday, 31 July 2012

Page 69: Adapt! Media queries and viewport

1anks!dev.opera.com | [email protected] | @chrisdavidmills

69.Tuesday, 31 July 2012