Slide 1

Slide 1 text

e Mobile Omnivore’s Dilemma

Slide 2

Slide 2 text

More devices every day

Slide 3

Slide 3 text

Short Run Problem is Mobile Phones

Slide 4

Slide 4 text

Content & Services Car sketch: http://www.flickr.com/photos/cloppy/5081768461/ Long Run Problem

Slide 5

Slide 5 text

How do you deliver the right HTML, CSS and JavaScript to browsers?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

@media screen and (min-width:480px) { /* CSS Rules */ } “screen” media type, we meet again! “min-” is a media query prefix. Rather intuitively, it means we want to query about a minimum width. “width” is a media feature we want to evaluate on the “screen” media type Unsurprisingly, there is also a “max-” prefix. These CSS rules will only get applied if the media query evaluates to TRUE Media Queries

Slide 10

Slide 10 text

Media Queries in Responsive Web Design

Slide 11

Slide 11 text

Target Context Result The size of the element in pixels. The size of the containing “context,” in pixels. Our new, proportional CSS rule, as a percentage.

Slide 12

Slide 12 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels

Slide 13

Slide 13 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels Target Context Result

Slide 14

Slide 14 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels 240 pixels 960 pixels 25% The columns span a quarter of the page width. So this feels pretty intuitive, right?

Slide 15

Slide 15 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels 240 pixels 960 pixels 25% The columns span a quarter of the page width. So this feels pretty intuitive, right? 260 pixels 960 pixels 27.0833333% #main { margin: 10px 260px 0 250px; } 250 pixels 960 pixels 26.0416667% Our current CSS rule

Slide 16

Slide 16 text

960 pixels 240 pixels 240 pixels 460 pixels 960 pixels 240 pixels 960 pixels 25% The columns span a quarter of the page width. So this feels pretty intuitive, right? 260 pixels 960 pixels 27.0833333% #main { margin: 10px 260px 0 250px; } 250 pixels 960 pixels 26.0416667% Our current CSS rule #main { margin: 10px 27.0833333% 0 26.0416667%; }

Slide 17

Slide 17 text

Em-based media queries solve zooming Pixel-based media queries when you zoom Em-based media queries when you zoom (you may have to reload to see change)

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

img, object { max-width: 100%; } ka-pow! Flexible Images and Media

Slide 20

Slide 20 text

http://www.flickr.com/photos/adventuresof/111667571/ is sounds great. What’s the catch?

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Looks can be deceiving Test mobile performance at http://blaze.io/mobile

Slide 24

Slide 24 text

Eight JavaScript files for this page. Total size of all JavaScript is 351K. Woah! There are 35 images. 2.4MB for images alone. See the pie charts by clicking on Show Statistics on the HAR Viewer page. What’s causing the page to be so slow?

Slide 25

Slide 25 text

Extremely long URL abbreviated This single iframe causes 47 files to be downloaded! Look inside ontap.html to find this code. Did you notice the map?

Slide 26

Slide 26 text

Extremely long URL abbreviated This single iframe causes 47 files to be downloaded! Look inside ontap.html to find this code. @media screen and (max-width:480px) { . . . #map {display:none;} } taps.css There are many more rules in the css file. The iframe has an id of map. This rule hides the Google Maps iframe by setting the display to none. Did you notice the map?

Slide 27

Slide 27 text

The taps.jpg file is 440.7K making it the largest file on the page. @media screen and (max-width:480px) { [Other CSS rules are here] .header {display:none;} } Same display:none issue with images

Slide 28

Slide 28 text

Original Resized K Saved % Saved Holmes 34.7K 8.1K 26.6K 76.6% Watson 39.0K 8.4K 30.6K 78.4% Mycroft 30.5K 6.7K 23.8K 78.0% Moriarty 43.4K 8.2K 35.2K 81.1% Adler 26.0K 6.6K 19.4K 74.6% Winter 34.7K 7.8K 26.9K 77.5% Total 208.3K 45.8K 162.5K 78.0% Flexible images are unnecessarily large images

Slide 29

Slide 29 text

Well, that’s disappointing. http://www.flickr.com/photos/kalexanderson/5421517469

Slide 30

Slide 30 text

Mobile First Flips RWD Around http://bradfrostweb.com/blog/web/mobile-first-responsive-web-design/

Slide 31

Slide 31 text

/* Wider viewports/higher resolutions (e.g. desktop) */ @media screen and (min-width:481px) { [Desktop layout rules here] } /* Mobile/lower-resolution devices */ @media screen and (max-width:480px) { [Mobile layout rules here] } Move the mobile media query block above the desktop media query. By doing this, we’re making sure the cascading effect of CSS is consistent with our mobile first progressive enhancement approach. Reorder media queries so cascade goes from small to large screens Keep basic styles outside of media queries.

Slide 32

Slide 32 text

e absence of support for media queries is in fact the rst media query. —Bryan Rieger, Yiibu

Slide 33

Slide 33 text

Oh come on IE. No love? Because IE 8 and below don’t support media queries, IE isn’t getting the CSS rules that create columns.

Slide 34

Slide 34 text

The conditional comment repeats the line above it ensuring desktop IE sees our layout.css file. IE conditional comments

Slide 35

Slide 35 text

The conditional comment repeats the line above it ensuring desktop IE sees our layout.css file. IE conditional comments or use Respond.js (a media query poly ll for IE)

Slide 36

Slide 36 text

Put CSS images inside media queries to prevent extra downloads @media screen and (min-width:481px){ .header { background:URL('images/taps.jpg') repeat-x; height: 300px; } } Doesn’t work in all browsers. Duplicate downloads are unavoidable if the browser downloads assets if they are inside a media query that doesn’t apply.

Slide 37

Slide 37 text

What to do about the map?

Slide 38

Slide 38 text

What to do about the map? Extremely long URL abbreviated This single iframe causes 47 files to be downloaded! Look inside ontap.html to find this code.

Slide 39

Slide 39 text

What to do about the map? var breakpoint = 481, id = 'mapcontainer', viewportWidth = window.innerWidth; if (viewportWidth > breakpoint) { var mapElement = document.createElement('iframe'); mapElement.id = 'map'; mapElement.src = 'http://maps.google.com/maps?f=q&s ource=s_q&hl=en&geocode=&q=334+NW+11th+Ave,+Portland,+ OR+97209&aq=&sll=37.0625,-95.677068&sspn=58.164117,80. 332031&vpsrc=0&ie=UTF8&hq=&hnear=334+NW+11th+Ave,+Port land,+Oregon+97209&t=m&ll=45.525472,-122.68218&spn=0.0 1804,0.025749&z=14&output=embed'; document.getElementById(id).insertBefore(mapElement, maplink); }

Slide 40

Slide 40 text

One SRC to rule all images Bensons Bubbler There are 16 beer labels on the On Tap Now page that use an img tag like this one for the Bensons Bubbler. Despite the need for multiple versions of this image depending on the screen size, HTML only allows one value for the src.

Slide 41

Slide 41 text

http://www.flickr.com/photos/wscullin/3770015203 first load... http://www.slideshare.net/yiibu/muddling-through-the-mobile-web

Slide 42

Slide 42 text

http://www.flickr.com/photos/wscullin/3770015203 initial request 1 Hmm, Huston - we have a problem... ...no idea what we can deliver to this client... HTML JavaScript stylesheets images Flash video fonts http://www.slideshare.net/yiibu/muddling-through-the-mobile-web

Slide 43

Slide 43 text

Option 1: Use JavaScript to set screen width in a cookie Information on location of large file in query string Instead of using query string, some use data attributes

Slide 44

Slide 44 text

Option 1: Use JavaScript to set screen width in a cookie (function( win ){ // Get screen width var sw = win.screen.width, doc = win.document, // Name of cookie cookieName = def.cookieName || “rwd-screensize”, // Cookie value cookieValue = def.cookieValue || sw > 500 ? ( sw > 1000 ? “large” : “medium” ) : “small”, // Cookie options cookieAge = def.cookieAge || 30000, cookieDomain = def.cookieDomain, cookiePath = def.cookiePath || “/”, //record width cookie for subsequent loads recordRes = (function(){ var date = new Date(); date.setTime( date.getTime() + cookieAge ); doc.cookie = cookieName + “=” + cookieValue + “;” + “expires=” + date.toGMTString() + “;” + (cookiePath ? “path=” + cookiePath + “;” : “” ) + ( cookieDomain ? “domain=” + cookieDomain + “;” : “”); })(); })(this);

Slide 45

Slide 45 text

Option 1: Use JavaScript to set screen width in a cookie # ---------------------------------------------------------------------- # Responsive Images # ---------------------------------------------------------------------- # Mobile-First images that scale responsively and responsibly # Copyright 2011, Scott Jehl, Filament Group, Inc # MIT License # ----------------------------------------- # Start Responsive Images # ----------------------------------------- RewriteEngine On #large cookie, large image RewriteCond %{HTTP_COOKIE} rwd-screensize=large RewriteCond %{QUERY_STRING} large=([^&]+) RewriteRule (.*\/).*\.(jpe?g|png|gif|webp) $1%1 #medium cookie, medium image RewriteCond %{HTTP_COOKIE} rwd-screensize=medium RewriteCond %{QUERY_STRING} medium=([^&]+) RewriteRule (.*\/).*\.(jpe?g|png|gif|webp) $1%1 #large cookie, medium image RewriteCond %{HTTP_COOKIE} rwd-screensize=medium RewriteCond %{QUERY_STRING} medium=([^&]+) RewriteRule (.*\/).*\.(jpe?g|png|gif|webp) $1%1 # ----------------------------------------- # END Responsive Images # -----------------------------------------

Slide 46

Slide 46 text

Option 1: Use JavaScript to set screen width in a cookie http://blog.yoav.ws/2011/09/Preloaders-cookies-and-race-conditions

Slide 47

Slide 47 text

Option 1: Use JavaScript to set screen width in a cookie Ugh, unreliable. May not work in future. http://blog.yoav.ws/2011/09/Preloaders-cookies-and-race-conditions

Slide 48

Slide 48 text

Option 2: Use noscript to prevent image from loading ”Koala” File doesn’t download because anything inside noscript tags is not is the DOM if the JavaScript is enabled. $(‘noscript[data-large][data-small]’).each(function(){ var src = screen.width >= 500 ? $(this).data(‘large’) : $(this).data(‘small’); $(‘”’’). insertAfter($(this)); }); Script assumes jQuery is loaded.

Slide 49

Slide 49 text

Option 2: Use noscript to prevent image from loading ”Koala” File doesn’t download because anything inside noscript tags is not is the DOM if the JavaScript is enabled.

Slide 50

Slide 50 text

Option 2: Use noscript to prevent image from loading ”Koala” File doesn’t download because anything inside noscript tags is not is the DOM if the JavaScript is enabled. Ugh, markup is no longer semantic. We’ve changed HTML to t one and only one solution.

Slide 51

Slide 51 text

Option 3: Use Sencha.io SRC or similar solution to resize images Set the first part of the src to http://src.sencha.io/ After the slash add the full URL of the image you want to have resized. Sencha.io SRC will resize the image to fit the size of the device screen. For example, if an iPhone visits the site, the image will be constrained to its screen size of 320 by 480 pixels. Bensons Bubbler Replace with your domain and path to the images.

Slide 52

Slide 52 text

Option 3: Use Sencha.io SRC or similar solution to resize images Set the first part of the src to http://src.sencha.io/ After the slash add the full URL of the image you want to have resized. Sencha.io SRC will resize the image to fit the size of the device screen. For example, if an iPhone visits the site, the image will be constrained to its screen size of 320 by 480 pixels. Bensons Bubbler Replace with your domain and path to the images.

Slide 53

Slide 53 text

Option 3: Use Sencha.io SRC or similar solution to resize images Set the first part of the src to http://src.sencha.io/ After the slash add the full URL of the image you want to have resized. Sencha.io SRC will resize the image to fit the size of the device screen. For example, if an iPhone visits the site, the image will be constrained to its screen size of 320 by 480 pixels. Bensons Bubbler Replace with your domain and path to the images. Ugh, we’re routing everything through a third party. And how does Sencha.io know what size image to send anyways?

Slide 54

Slide 54 text

Only real solution is a new element Giraffe

Long description

W3C Responsive Images Community Group http://www.w3.org/community/respimg/

Slide 55

Slide 55 text

Doing this work does make a difference.

Slide 56

Slide 56 text

How to handle embedded video? How about content reordering? (Flexbox can’t come soon enough) How can we be smart about image and video resizing and converting? What about third-party widgets that aren’t responsive? (Esp. advertising!) How do we integrate with content management systems? Mobile First RWD is Difficult

Slide 57

Slide 57 text

9% 4% 21% 38% 4% 25% Mobile is Larger Same Size Less than 10% Savings 11 to 50% Savings 51% to 100% Savings Greater than 100% Savings Comparison of Mobile & Desktop RWD Views Few Mobile First RWD Sites

Slide 58

Slide 58 text

Device detection http://www.flickr.com/photos/ cesarastudillo/3981364314/

Slide 59

Slide 59 text

How device detection works

Slide 60

Slide 60 text

User agent strings are the third rail of web development http://www.flickr.com/photos/flashflood/5183622956/

Slide 61

Slide 61 text

http://www.flickr.com/photos/77799978@N00/5351372848/ Many believe separate sites are evil.

Slide 62

Slide 62 text

“User agent sniffing must die. Nice job advocating this bad practice.” http://www.flickr.com/photos/kayepants/391645870/

Slide 63

Slide 63 text

Mobile developers weren’t blind to the problems with UAs. http://www.flickr.com/photos/sam-wood/3255460999/

Slide 64

Slide 64 text

25 of 30 sites in Alexa Top 30 use device detection Other ve don’t offer mobile sites!

Slide 65

Slide 65 text

Incoming requests are run against a device database Code evaluates which device class the device matches based on device data In this hypothetical example, devices are sorted into one of four groups Desktop-like devices Spiffy, newer smartphones Middle-of-the- road smartphones and advanced feature phones Devices that didn’t meet bottom-line requirements The bar is set here (like in chapter 4) Group into as few device classes as possible

Slide 66

Slide 66 text

* it actually stands for ‘uniform.’ close enough ;-) http://www.flickr.com/photos/laughingsquid/255852260/sizes/o/ Support every URL on no matter what device.

Slide 67

Slide 67 text

Device detection means signing up for an arms race. http://www.flickr.com/photos/kyle_slushey_is_awesome/4370958261/

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

Tools to make your life easier. http://www.flickr.com/photos/bre/552152780/

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Get 40% off of the print and 50% off of ebook version using code AUTHD at oreilly.com. OR Amazon link (no code): http://bit.ly/hf-mw ank you. You’ve been awesome!