Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mobile Web Development: The Challenges and How ...

Mobile Web Development: The Challenges and How to Meet Them

My presentation for MinneWebCon 2012.

Avatar for Tony Thomas

Tony Thomas

April 17, 2012
Tweet

Other Decks in Technology

Transcript

  1. Mobile Web Development The Challenges and How To Meet Them

    Tony Thomas - Student Unions & Activities at The University of Minnesota Tuesday, April 17, 12
  2. Student Unions & Activities 2011 Poll 50% have a smartphone

    7% plan to get one Tuesday, April 17, 12 Our unscientific student poll.
  3. Yavin 4 Yavin Tuesday, April 17, 12 This is Yavin

    4, the location of the rebel base
  4. Mobile Operating Systems 0 21500 iOS Android Blackberry W indows

    Phone SymbianOS Samsung Nokia LG Danger Hiptop 21458 10330 767 82 56 37 31 6 4 Tuesday, April 17, 12 Our users are predominantly iOS & Android. Blackberry distant 3rd. Long tail of others.
  5. Android Android Device Dimensions Android Android Android Android Samsung Nokia

    Android Android Blackberry Android Nokia Samsung Android Android iPhone iPod Android Android Blackberry Blackberry SymbianOS Android Blackberry Android Android Blackberry Blackberry Android Windows Android Android Android Android Android SymbianOS Blackberry Android Android Tuesday, April 17, 12 This is just a small sample of device dimensions taken from 1 month of SUA mobile traffic.
  6. Responsive Design Photo: Peter J. Hart flickr.com/photos/40054618@N03/5140728113/ Ethan Marcotte Tuesday,

    April 17, 12 This is Ethan Marcotte who introduced the concept of responsive design in 2010.
  7. A Dao of Web Design Photo: drewm flickr.com/photos/drewm/ flickr.com/photos/drewm/4732738890/ John

    Allsopp Tuesday, April 17, 12 It’s in part inspired by John Allsopp’s 2000 A List Apart article called “A Dao of Web Design.”
  8. A Dao of Web Design alistapart.com/articles/dao/ “It’s time to throw

    out the rituals of the printed page, and to engage the medium of the web and its own nature.” Tuesday, April 17, 12 [Read quote.] Back in 2000, Allsopp was advocating for embracing the inherent flexible nature of the web.
  9. Responsive Architecture Photo: Evangelos Kotsioris “If a building could change

    its posture, tighten its muscles and brace itself against the wind, its structural mass could literally be cut in half.” --Guy Nordenson Tuesday, April 17, 12 Responsive design is also inspired by Responsive, or adaptive architecture where buildings respond and change according to their environment. [read quote] Think of markup, CSS, JavaScript & media as the structural mass of a website.
  10. Flexible, Fluid Layouts widths, margins, padding in relative units Photo:

    Sabrina Campagna flickr.com/photos/mar1lyn84/5649648853/ Tuesday, April 17, 12 The foundation of responsive design are layouts in relative units. That means widths, margins & padding are defined in ems or percents.
  11. Flexible, Fluid Layouts Target / Context = Result Photo: ansik

    flickr.com/photos/ansik/304526237/ Tuesday, April 17, 12 This is Ethan Marcotte’s formula for calculating widths in ems. The assumption here is that most browsers maintain their default setting of a 16px font-size.
  12. Flexible, Fluid Layouts 24 / 16 = 1.5 Photo: ansik

    flickr.com/photos/ansik/304526237/ Tuesday, April 17, 12 In this example, we’re looking for a 24 pixel font-size. 24/16 is 1.5, so our font size is 1.5em.
  13. Context Changes 13 / 24 = 0.54166666666666 Photo: ansik flickr.com/photos/ansik/304526237/

    Tuesday, April 17, 12 Beware of context changes. If our font is 1.5em and thus, 24px and we want a 13px font in that context, we have to divide 13 by 24 (our new context). Notice the number of decimal points in this example. Marcotte advocates for keeping all those decimal places. I haven’t been able to find any information as to how many places a different browsers will calculate.
  14. Media Queries Smartphone Tablet Netbook Desktop Tuesday, April 17, 12

    Back to media queries. After we’ve established our grids, margins and padding in relative units, media queries are the next important step in making a design responsive.
  15. Media Queries @media screen and (min-width: 480px) { /* CSS

    goes here */ } Tuesday, April 17, 12 First a browser reads the media type, then evaluates the query. In this case, if the media is ‘screen’ and the browser viewport is at least 480px, the enclosed styles will be rendered.
  16. Media Queries @media screen and (min-width: 480px) { /* CSS

    goes here */ } Media Tuesday, April 17, 12 First a browser reads the media type, then evaluates the query. In this case, if the media is ‘screen’ and the browser viewport is at least 480px, the enclosed styles will be rendered.
  17. Media Queries @media screen and (min-width: 480px) { /* CSS

    goes here */ } Media Query Tuesday, April 17, 12 First a browser reads the media type, then evaluates the query. In this case, if the media is ‘screen’ and the browser viewport is at least 480px, the enclosed styles will be rendered.
  18. Media Queries @import url(“css/phone.css”) screen and min-width: 480px); Tuesday, April

    17, 12 Here’s an alternate syntax that will load a separate style sheet based on the parameters of the media query.
  19. Media Queries @import url(“css/phone.css”) screen and min-width: 480px); Media Tuesday,

    April 17, 12 Here’s an alternate syntax that will load a separate style sheet based on the parameters of the media query.
  20. Media Queries @import url(“css/phone.css”) screen and min-width: 480px); Media Query

    Tuesday, April 17, 12 Here’s an alternate syntax that will load a separate style sheet based on the parameters of the media query.
  21. Media Query Options • width (browser width) • height (browser

    height) • device-width (device-width) • device-height (device-height) • orientation (portrait or landscape) • aspect-ratio (browser aspect ratio) • device-aspect-ratio (device aspect ratio) • color (bits of color component of device) • color-index (number of entries in the color lookup profile) • monochrome (similar to color but for monochrome devices) • resolution (pixel-density in DPI) • scan (TV’s-whether scanning process is progressive or scan) • grid (grid-based display, like feature phone) Tuesday, April 17, 12 Here’s a complete list of media query options--including some related to television scanning.
  22. Media Query Options • width (browser width) • height (browser

    height) • device-width (device-width) • device-height (device-height) • orientation (portrait or landscape) Tuesday, April 17, 12 In reality these are the most common. I want to point out a few distinctions here. ‘width’ & ‘height’ refer to the browser viewport. ‘device-width’ & ‘device-height’ refer to the height & width of the device. Device orientation is another handy option.
  23. There are no standard devices! Photo: Oliver White flickr.com/photos/nuage_de_lait/6580839781/ Tuesday,

    April 17, 12 Until recently, it’s been popular to write queries based on the dimensions of popular devices. The list on the left of this slide is taken from SUA’s analytics. You can see the incredible diversity of screen sizes in our mobile analytics. There are no standard devices. Instead of trying to keep up w/ new device dimensions, you should evaluate the breakpoints of your design. Look at where the layout gets awkward and set a breakpoint. Then let the devices fall where they may. New devices appear almost every day. Your design is the constant in the equation. Plan accordingly.
  24. Set media query breakpoints where the design breaks. Photo: Ballookey

    Klugeypop flickr.com/photos/ballookey/136450028/ Tuesday, April 17, 12
  25. “CSS Pixels” & “Hardware Pixels” Apple iPhone 4: 326 ppi

    LG KM900 Arena: 311 ppi Samsung S8000 Jet: 301 ppi Canon EOS 550D (T2i): 289 ppi HTC Touch Diamond: 286 ppi Samsung S8500 Wave: 283 ppi HTC Desire: 252 ppi Google Nexus One: 252 ppi Sony Ericsson XPERIA X10: 245 ppi Samsung I9000 Galaxy S: 233 ppi HTC HD2: 217 ppi Nokia N8: 210 ppi Apple iPhone 3GS: 165 ppi Apple iPad: 132 ppi Source: blog.gsmarena.com/how-does-the-iphone-4-pixel-density-stand-in-comparison/ Tuesday, April 17, 12 Here are points per inch for several mobile devices. Notice the iPhone 4 at the top with the most points per inch.
  26. Some Pixels Are More Equal Than Others Hardware Pixel CSS

    Pixel Tuesday, April 17, 12 The iPhone 4 has a pixel-density of 2. That means there are 2 “hardware” pixels on the screen for every “CSS pixel.” That makes fonts and images render very crisply on the screen, but it’s a problem when we’re trying to calculate CSS. Thus we level the field by setting the zoom-level to 100%. When the zoom level is 100%, the ratio of hardware pixels to CSS is 1.
  27. Some Pixels Are More Equal Than Others Hardware Pixel CSS

    Pixel Tuesday, April 17, 12 By putting “initial-scale=1” in the viewport meta tag forces the browser to make hardware pixels equal CSS pixels. The problem is, this will apply to text and layout, but images will zoom up and appear pixelated. You need to figure out the pixel density so the image quality matches the density of the screen. Which leads us to...
  28. Take Control of The Screen <meta name="viewport" content="width=device-width, initial- scale=1,

    maximum-scale=1"> Tuesday, April 17, 12 It’s a good idea to use a “viewport” meta tag in the head of your document to gain some control and provide better consistency between mobile devices.
  29. Take Control of The Screen width=device-width Without this, mobile browsers

    will zoom the page out to 980px. Tuesday, April 17, 12 First in that meta tag, I’m telling the browser to make the content width the device width. device-width
  30. Take Control of The Screen initial-scale = 1 Controls the

    zoom level when the page is loaded. (1 = 100%) Tuesday, April 17, 12
  31. Take Control of the Screen maximum-scale = 1 Controls zooming.

    In my example, the maximum scale is set to 100%. Other options: minimum-scale & user-scalable. Tuesday, April 17, 12
  32. Take Control of the Screen Tuesday, April 17, 12 Left:

    before setting the viewport meta tag. Right: after.
  33. Responsive Images & Media Photo: Vicky Somma flickr.com/photos/tgaw/2556511663/ Tuesday, April

    17, 12 After after fluid layouts and media queries come responsive images. The talking phones are outraged that we’re trying to send them a gigantic image.
  34. Responsive Images & Media #container img { max-width: 100%; }

    Tuesday, April 17, 12 At their simplest, responsive images respond to the layout by never exceed the width of their container. Unfortunately HTML alone is lacking in terms of providing a way to place the smallest possible image on the page. Remember, mobile devices are often lacking in bandwidth. Sending a large image to a small screen is an unnecessary burden. JavaScript does provide some relief.
  35. Start with Basic HTML <a id=”home-link” href=”http://sua.umn.edu”>Home</a> Tuesday, April 17,

    12 Start with the lowest common denominator. In this example, I’m using a link, but this could be any container. This HTML will work with any feature phone.
  36. Use JavaScript to Detect Screen Dimensions h = screen.height; Tuesday,

    April 17, 12 Next we have to try to evaluate the screen height. I use height because smart phones could be turned to landscape mode at any time, making the height the new width.
  37. Use JavaScript to Detect Screen Dimensions h = screen.height; //

    Pixel ratio (WebKit Only) r = window.devicePixelRatio; Tuesday, April 17, 12 With the advent of high density screens, we have to account for pixel-density. The iPhone 4 has a pixel density of 2 and many Android phones have a pixel density of 1.5. Depending on meta tags in the head of your document, an iPhone might report dimensions of 320x480 even though it’s true resolution is 640x960. Caution: devicePixelRatio is WebKit-only. Fortunately this scenario if pure enhancement and won’t affect the functionality or information on the page. A non-WebKit browser will simply report a lower resolution and so the image may seem a little pixelated.
  38. Use JavaScript to Detect Screen Dimensions /* if devicePixelRatio is

    supported, calculate the device height */ if (typeof r !== 'undefined') { /* Calculate the true number of pixels on the screen */ height = h * r; } Tuesday, April 17, 12 We can test for this by making sure the result of devicePixelRatio is not ‘undefined’. If it is, we simply use the original height reading. If not, we can get the true height by multiplying the reported height by the pixel-density.
  39. Use JavaScript to Detect Screen Dimensions // Set the image

    directory accordingly if (height <= 320) { // load 320 header d = 320; } Tuesday, April 17, 12 In this example, I have directories with names matching a few key heights. A copy of the image is stored in each directory that matches it’s width. So a 320px wide image resides in the 320 directory.
  40. Use JavaScript to Detect Screen Dimensions $('a#home-link').html('<img src="/images/mobile/' + d

    + '/ header.png" alt="This is alt text." id="logo" class="logos" />'); Tuesday, April 17, 12 I can then use JQuery write in this HTML with the directory I want. The image will then load that is large enough for the device, but somewhat optimized for the dimensions of the screen. This method is not perfect. For instance, the image will not load until after the DOM has fully loaded--since I’m using JQuery and the ready() method. Still it’s a fairly good compromise to minimize the bandwidth necessary to load the page.
  41. Mobile First, Mobile Separate & RESS Tuesday, April 17, 12

    Now that I’ve touched on a few techniques, I want to go over three approaches to developing for mobile. Those are Mobile First, a separate mobile experience and RESS which stands for Responsive and Server Side.
  42. Mobile First Luke Wroblewski Photo: foltzwerk flickr.com/photos/foltzwerk/2779814860/ Tuesday, April 17,

    12 Mobile First is a concept advocated by Luke Wroblewski in his book of the same name. The basic approach is that sites should be first designed for mobile with desktop versions coming after. This approach begins with a number of constraints that filter through to the desktop experience.
  43. Mobile First • One site for all devices • Focus

    on content that’s truly important • No User Agent sniffing Tuesday, April 17, 12 Some of the advantage of this approach are that you have one site for all devices. The constraints force you focus on what’s truly important. You avoid User Agent sniffing that some other techniques rely upon.
  44. Mobile First • Potential for unnecessary overhead • Convincing stakeholders

    to embrace constraints may be a barrier • Requires polyfills for older browsers Tuesday, April 17, 12 Some disadvantages: Mobile networks are often slower, and sometimes users will have a cap on their data plan. You have to be careful to truly embrace the constraints through the entire stack so you can minimize the size of pages and media. Along with that, it’s not always easy to convince stakeholders to embrace those constraints. Finally, for the site to work in all browsers, you’re going to need to use a few JavaScript polyfills.
  45. Polyfills Respond: github.com/scottjehl/Respond (media queries) Overthrow: github.com/filamentgroup/Overthrow (overflow: auto) html5shiv:

    code.google.com/p/html5shiv (HTML5) Tuesday, April 17, 12 Respond for media queries in older versions of IE Overthrow for using overflow: auto html5shiv if you’re using html5
  46. Feature Detection Modernizr modernizr.com Photo: Patrick Ciebilski flickr.com/photos/mccieb/ Tuesday, April

    17, 12 Modernizr is a JavaScript library for detecting browser features. It offers a lot of flexibility in that you can enhance your site based on the capabilities of each browser.
  47. Modernizr • @font-face • background-size • border-image • border-radius •

    box-shadow • Flexible Box Model • hsla & rgba color • Multiple backgrounds • opacity • text-shadow • CSS Animations • CSS Columns • CSS generated content • CSS gradients • CSS reflections • CSS 2D transforms • CSS 3D transforms • CSS transitions • applicationCache • Canvas • Canvas text • Drag and drop • haschange Event • History management • HTML5 audio & video • IndexedDB • Input attributes • Input types • localStorage • Cross-window messaging • sessionStorage • Web Sockets • Web SQL DB • Web Workers • Geolocation • Inline SVG • SMIL • SVG • SVG clip pathes • Touch events • WebGL Tuesday, April 17, 12 Modernizr has a long list of features that it detects.
  48. Mobile First Flexible, Fluid Layout Media Queries Responsive Images Tuesday,

    April 17, 12 Encompasses all aspects of a responsive design to deliver one site that works for all devices.
  49. Mobile First Flexible, Fluid Layout Media Queries Responsive Images Web

    Standards Tuesday, April 17, 12 And responsive design is built upon web standards. If you’re already used to developing with standards, responsive design is just reaching a little further with the same concept.
  50. Tuesday, April 17, 12 Here’s an example of a responsive

    design in action. Notice how he’s designed the layout and images to work across different screen widths.
  51. Separate Mobile Experience Tuesday, April 17, 12 Remember these guys?

    Both have also done sites with a totally separate mobile experience.
  52. Separate Mobile Experience • Site tailored to use case •

    Eliminate unnecessary markup, CSS & JavaScript • May be easier to convince stakeholders to embrace constraints Tuesday, April 17, 12 Some of the advantages of providing a separate mobile experience: The site can be tailored the use case. You have a lot of control over reducing unnecessary markup, CSS & JavaScript. It may be easier to convince stakeholders to embrace constraints if they get to keep there content somewhere else.
  53. Separate Mobile Experience • Two sites • Might not meet

    all users’ needs • Relies on device detection Tuesday, April 17, 12 Some of the disadvantages: You maintain two sites. A separate experience might not meet all your users’ needs. You’re going to have to rely on some sort of device detection.
  54. Device Detection Photo: Brad Buchanan flickr.com/photos/frozenisles/3443607776/ detectmobilebrowsers.com detector.dmolsen.com Tuesday, April

    17, 12 For the projects we’ve done at SUA so far, we’ve relied on the script at detectmobilebrowsers.com. I have more recently come accross Detector and it looks quite promising.
  55. detectmobilebrowsers.com Apache ASP ASP.NET ColdFusion C# IIS JSP JavaScript jQuery

    Lasso nginx node.js PHP Perl Python Rails Photo: Herr Olsen flickr.com/photos/herrolsen/6199045384/ Tuesday, April 17, 12 Returns boolean. Available is several languages.
  56. Detector • detector.dmolsen.com • PHP only • Beta • Uses

    same database of tests as Modernizr Photo: Jason Ahrns flickr.com/photos/musubk/5578348098/ Tuesday, April 17, 12 Detector, by comparison, is only available in PHP. It’s still in beta. It does a lot more than detect whether a device is mobile or not. It also has the same database of feature detection as Modernizr.
  57. Detector <?php if ($ua->isMobile) { // Code goes here }

    ?> Photo: DigiTaL~NomAd flickr.com/photos/urban-spaceman/3947680057/ Tuesday, April 17, 12 Here’s an example of how you might use Detector to detect mobile devices.
  58. Preserve The URI When Redirecting Photo: Chris Campbell flickr.com/photos/cgc/3809196941/ Tuesday,

    April 17, 12 If you’re using a method to detect and redirect mobile devices, remember to preserve the URL. That is, if they’re trying to get to a specific page or resource, make sure you redirect them to the equivalent page or resource on the mobile site. If you don’t have a mobile version, you shouldn’t be redirecting.
  59. if (is_mobile() && !isset($_COOKIE['mobile'])) { $redirect = '/mobile/'; if (preg_match('/event\/(\d+)/',

    $_SERVER['REQUEST_URI'], $matches) > 0) { $redirect .= $matches[0]; } setcookie('mobile'); header('Location: ' . $redirect); exit; } Tuesday, April 17, 12
  60. JQuery Mobile Tuesday, April 17, 12 We’ve used the JQuery

    Mobile framework for all of your mobile projects so far.
  61. JQuery Mobile Tuesday, April 17, 12 For one reason: it’s

    thoroughly tested. This is an image of the JQuery Mobile testing table of devices.
  62. JQuery Mobile Setup <div data-role="page"> <div data-role="header"> <h1>Page Title</h1> </div><!--

    /header --> <div data-role="content"> <p>Page content goes here.</p> </div><!-- /content --> <div data-role="footer"> <h4>Page Footer</h4> </div><!-- /footer --> </div><!-- /page --> Tuesday, April 17, 12 Setting up the framework is fairly simple. First, it does a require an HTML5 doctype. After that, it makes extensive use of the HTML5 data attribute. You ca see several “roles” defined in the markup here.
  63. Ajax Page Loads <a itemprop="url" data-transition="slide" href="/path/">link</a> Tuesday, April 17,

    12 The framework also does a lot of heavy lifting for you. For instance, HTML page links are automagically turned into AJAX page loads with nice transitions defined in a “data-transition” attribute.
  64. Ajax Page Loads <a itemprop="url" data-transition="slide" href="/path/">link</a> Tuesday, April 17,

    12 The framework also does a lot of heavy lifting for you. For instance, HTML page links are automagically turned into AJAX page loads with nice transitions defined in a “data-transition” attribute.
  65. JQuery Mobile Page Transition Options • fade • pop •

    flip • turn • flow • slide • slideup • slidedown • none Tuesday, April 17, 12 The framework also offers several transition options.
  66. JQuery Mobile Themes Tuesday, April 17, 12 There are also

    6 built-in themes available. You also have the option of designing and coding styles for your own theme.
  67. JQuery Mobile Themes <div data-theme="a"></div> Tuesday, April 17, 12 Themes

    are defined using a “data-theme” attribute.
  68. JQuery Mobile Grid Framework <div class="ui-grid-a"> <div class="ui-block-a"> </div> <div

    class="ui-block-b"> </div> </div><!-- /grid-a --> Tuesday, April 17, 12 There is also a grid framework available. Here’s an example of a two-column grid. There’s a wrapper with the class “ui-grid-a”. The child elements have classes of “ui-block-a” and “ui- block-b” respectively to place them as two columns on the page.
  69. JQuery Mobile Grid Framework • two-column: ui-grid-a • three-column: ui-grid-b

    • four-column: ui-grid-c • five-column: ui-grid-d Photo: Mike Smail flickr.com/photos/smailtronic/2214772479/ Tuesday, April 17, 12 You can create grids all the way up to 5 columns.
  70. Binding JavaScript Events in JQuery Mobile Use pageInit() instead of

    $(document).ready() Tuesday, April 17, 12 Since AJAX is used to load page content, $(document).ready() is only called with the first page load. To successfully trigger events when pages load, use JQuery Mobile’s pageInit instead.
  71. REsponsive & Server Side (RESS) Photo: foltzwerk flickr.com/photos/foltzwerk/2779814860/ Tuesday, April

    17, 12 Luke Wroblewski, who authored Mobile First, also has written about RESS, which is a combination of responsive design techniques and server-side feature detection.
  72. • Heavy lifting handled on server side • Able to

    provide a finely-tuned experience • Removes need for redirects REsponsive & Server Side (RESS) Tuesday, April 17, 12 Some of RESS’ advantages: The work of feature detection is handled on the server side, making the browser on the phone do less work. It offers a way to create a very finely-tuned experience. No more redirects to a different mobile site. Preserves what Phil Hawksworth calls the contract of the URI for providing content.
  73. • Harder to execute • Requires some user agent sniffing

    REsponsive & Server Side (RESS) Tuesday, April 17, 12 • Harder to execute ◦ more resources needed in terms of client and server side coding skills and time ◦ A lot of thought and planning needs to go into components and device classes (phone, tablet, desktop, TV, etc.) • Requires some user agent sniffing
  74. There is no single solution and I will stand on

    Jeffery Zeldman’s coffee table and say that. Tuesday, April 17, 12
  75. Tools & Resources Testing • Comprehensive list of emulators: mobilexweb.com/emulators

    • springbox.com/mobilizer Books • Responsive Web Design - Ethan Marcotte • Mobile First - Luke Wroblewski Links • MobilePerf (z.umn.edu/mobileperf) • Adobe Shadow (labs.adobe.com/ technologies/shadow) • lukew.com (@lukew) • ethanmarcotte.com (@beep) • jquerymobile.com • detectmobilebrowsers.com • detector.dmolsen.com • mobilizr.com Tuesday, April 17, 12