Optimizing your layout for phones and tablets using viewport and media queries
Upcoming SlideShare
Loading in...5
×
 

Optimizing your layout for phones and tablets using viewport and media queries

on

  • 21,166 views

With mobile web browsing becoming widespread, users expect their favorite sites to "just work" on their mobile device of choice. Exciting times, especially as there are a number of new hooks in modern ...

With mobile web browsing becoming widespread, users expect their favorite sites to "just work" on their mobile device of choice. Exciting times, especially as there are a number of new hooks in modern mobile browsers that help web designers to create compelling experiences on phones and tablets.

In my talk, I cover the various features of the viewport meta tag, and look at how they allow you to control mobile browsers' zoom behaviors. I also connect this to Opera's CSS Device Adaptation proposal, which brings this feature to CSS in the form of the @viewport rule.

Furthermore, I explain how viewport can be combined with media queries to create super-flexible layouts that work nicely across different browsing contexts and devices, and explore various strategies to dealing with different screen sizes and pixel densities.

Viewport demos can be found on http://people.opera.com/andreasb/demos/parisweb2011/

Statistics

Views

Total Views
21,166
Views on SlideShare
19,877
Embed Views
1,289

Actions

Likes
56
Downloads
742
Comments
0

26 Embeds 1,289

http://www.conseilsmarketing.com 363
http://www.paris-web.fr 338
http://blog.johanbleuzen.fr 111
http://parisweb2011 88
http://lanyrd.com 76
http://us-w1.rockmelt.com 62
http://a0.twimg.com 61
http://www.scoop.it 59
http://feeds.feedburner.com 29
http://www.stonepower.fr 29
http://soup.sindre.at 15
http://paper.li 14
http://85.17.163.194 7
http://www.johan.me 6
http://oembed.wordpress.dev 6
https://twitter.com 6
http://blog.gaborit-d.com 5
http://www.paperblog.fr 3
http://www.johanbleuzen.fr 2
http://moisesgarcia.es 2
http://leken.stonepower.fr 2
http://localhost 1
http://zootool.com 1
http://www.bonkm.com 1
https://www.rebelmouse.com 1
http://www.lablogule.com 1
More...

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Optimizing your layout for phones and tablets using viewport and media queries Optimizing your layout for phones and tablets using viewport and media queries Presentation Transcript

  • Optimizing your layoutfor phones and tabletsusing viewport andmedia queriesAndreas Bovens - Opera Software
  • about me http://www.flickr.com/pho tos/thomaaas/4783952591 /
  • about me http://www.flickr.com/pho tos/thomaaas/4783952591 /
  • about me http://www.flickr.com/pho tos/thomaaas/4783952591 / ccio1/119626133/ om/photos/bocca http://www.flickr.c
  • about me http://www.flickr.com/pho tos/thomaaas/4783952591 / ccio1/119626133/ om/photos/bocca http://www.flickr.c
  • about me http://www.flickr.com/pho tos/thomaaas/4783952591 / ccio1/119626133/ om/photos/bocca http://www.flickr.c http://www.flickr.com/photos/forcery/ 3511170176/
  • Bruce Lawson Daniel Davis Divya Manian Chris Mills Karl Dubost Shwetank Dixit Tiffany Brown Andreas Bovens Luz CaballeroPatrick Lauke Zi Bin Cheah Vadim Makeev Mike Taylor
  • “Patterns forMultiscreenStrategies” Coherence Syncronization Screen sharingBy Precious http://slidesha.re/kiip5y Device shifting Complementarity Simultaneity These patterns should help understand and define strategies for the multiscreen world.
  • Coherence Syncronization Screen sharing Device shifting Complementarity SimultaneityThese patterns should help understand and define strategies for the multiscreen world. http://slidesha.re/kiip5y
  • “A digital product orservice looks andworks coherentlyacross devices.Features are optimizedfor specific devicecharacteristics andusage scenarios.” Coherence http://slidesha.re/kiip5y
  • Coherence http://slidesha.re/kiip5y
  • ?
  • show top-left and pan
  • small screen rendering aka single column view
  • zoom and pan
  • That’s all nice,but what if I want to control this zooming behavior?
  • Answer:viewport meta
  • This is the viewport
  • (not just this)
  • Most commonlythough, you’ll want tofocus on controllingpage width...
  • <meta ...> ... using a "viewport" meta tag in the <head> of the page.
  • works in:with some exceptions ;-)
  • http://www.opera.com/developer/tools/mobile/
  • HVGA portrait
  • HVGA portrait
  • HVGA portrait no viewport defined in <head>, so fallback to default of 850px, which is squeezed inside 320px.
  • HVGA portrait <meta name="viewport" content="width=320">
  • HVGA portrait <meta name="viewport" content="width=320">
  • HVGA portrait HVGA landscape <meta name="viewport" content="width=320">
  • HVGA portrait <meta name="viewport" content="width=device-width">
  • HVGA portrait <meta name="viewport" content="width=device-width">
  • HVGA portrait HVGA landscape <meta name="viewport" content="width=device-width">
  • HVGA landscape HVGA landscape 480px 320px × 1.5 zoom
  • HVGA landscape HVGA landscape 480px 320px × 1.5 zoom
  • HVGA portrait <meta name="viewport" content="initial-scale=1">
  • HVGA portrait <meta name="viewport" content="initial-scale=1">
  • HVGA portrait HVGA landscape <meta name="viewport" content="initial-scale=1">
  • HVGA portrait HVGA landscape <meta name="viewport" content="initial-scale=1"> (same result as width=device-width)
  • HVGA portrait HVGA landscape <meta name="viewport" content="initial-scale=0.5">
  • HVGA portrait HVGA landscape <meta name="viewport" content="initial-scale=2">
  • <meta name="viewport"content="width=device-width">+<div style="width: 600px">
  • HVGA portrait <meta name="viewport" content="width=device-width"> + <div style="width: 600px">
  • HVGA portrait <meta name="viewport" content="width=device-width, initial-scale=1"> + <div style="width: 600px">
  • other stuffmaximum-scale=... , minimum-scale=...<meta name="viewport" content="initial-scale=1, maximum-scale=1.5">user-scalable=yes|no<meta name="viewport" content="width=320, user-scalable=no">height=...<meta name="viewport" content="height=device-height">
  • other stuffmaximum-scale=... , minimum-scale=...<meta name="viewport" content="initial-scale=1, maximum-scale=1.5">user-scalable=yes|no<meta name="viewport" content="width=320, user-scalable=no">height=...<meta name="viewport" content="height=device-height">
  • other stuffmaximum-scale=... , minimum-scale=...<meta name="viewport" content="initial-scale=1, maximum-scale=1.5">user-scalable=yes|no<meta name="viewport" content="width=320, user-scalable=no">height=...<meta name="viewport" content="height=device-height">
  • other stuffmaximum-scale=... , minimum-scale=...<meta name="viewport" content="initial-scale=1, maximum-scale=1.5">user-scalable=yes|no<meta name="viewport" content="width=320, user-scalable=no">height=...<meta name="viewport" content="height=device-height">
  • NB:Make sure to use commas, not semi-colons asdelimiters between viewport values!Remember this, especially when syntax-googling ;-)
  • http://www.flickr.com/photos/33284937@N04/4771132618/
  • So, how to use this?Sniffing for mobilebrowsers maybe?
  • So, how to use this?Sniffing for mobilebrowsers maybe?Better don’t sniff.
  • Opera/9.80 (Macintosh; Intel Mac OS X 10.5.8; U; en) Presto/2.9.220 Version/12.00
  • Opera/9.80 (Macintosh; Intel Mac OS X 10.5.8; U; en) Presto/2.9.220 Version/12.00
  • So, avoid sniffing,but if you really have to,provide a way for usersto make corrections.
  • So, avoid sniffing,but if you really have to*,provide a way for usersto make corrections.* always ask yourself why the mobile site should bedifferent from the desktop site
  • * always ask yourself why the mobile site should be different from the desktop sitecustomer requir ements tim e restrictions budget limitationscapabilities are different different layout needed
  • * always ask yourself why the mobile site should be different from the desktop sitecustomer requir ements tim e restrictions budget limitationscapabilities are different different layout needed capability detection
  • * always ask yourself why the mobile site should be different from the desktop sitecustomer requir ements tim e restrictions budget limitationscapabilities are different different layout needed capability detection viewport + media queries
  • Let’s talk aboutviewport + media queriesmedia queries
  • Media queries are conditional hooksfor applying different CSS rules,depending on e.g. browser width,screen height, aspect ratio, etc.
  • Differently said, media queriesallow you to optimize your sitelayout for all kinds of form factors. Via http://mediaqueri.es/
  • @media screen and (min-width: 400px) { article { border: 5px solid hsla(32, 98%, 51%, 1); box-shadow: hsla(0, 100%, 0%, 0.5) 0 0 10px; max-width: 800px; margin: 0 auto 0 auto; } }
  • @media screen and (min-width: 400px)and (max-width: 800px) { article { /* css for browsers with width ≥ 400px and ≤ 800px */ } }
  • @media screen and (min-width: 400px),not projection and (aspect-ratio: 16/9) { article { /* css for browsers with width ≥ 400px, except projectors with 16/9 aspect ratio */ } }
  • @media screen and (max-width: 800px) { article { /* css for browsers with width ≤ 800px */ } }@media screen and (max-width: 400px) { article { /* css for browsers with width ≤ 400px */ } }
  • @media screen and (max-width: 400px) { article { /* css for browsers with width ≤ 400px */ } }Remember that mobile browsers have adefault viewport width (e.g. 850px).For your mobile specific media queriesto work, you need to set the viewport!
  • default viewport. width=device-width.mobile specific media mobile specific media queries not applied. queries applied.
  • The viewport and media queriescombo allows you to create justone responsive site that workseverywhere.
  • Q: What about max/min-device-width/height?A: There are use cases for this, but note that yourmedia queries won’t be applied when resizing thebrowser on desktop.
  • Q: Should I start from small to large, or vice versa?A: Do mobile first, then enhance for desktop. Thiswill make the mobile experience faster. Deal withold IE using a polyfill, such as Respond.js https://github.com/scottjehl/Respond
  • Q: Which screen sizes should I design for?A: All of them! No, seriously, don’t limit yourself.Resize your browser window and tweak differentelements’ CSS properties when needed.
  • http://www.flickr.com/photos/33284937@N04/5445675024/
  • Dealing withhigh-DPI screens
  • A pixel is not what it seemsThus far, we’ve talked aboutpixels in terms of “CSS pixels”.One CSS pixel can be multipledevice pixels.
  • HVGA portraitalmost WVGA portrait
  • HVGA portraitalmost WVGA portrait
  • almost WVGA portraitDevice is 480pxwide, but browserapplies defaultzoom of 150%.480px/1.5 = 320px
  • In most scenarios, you won’thave to worry about this.It just works.
  • However, if you want tocontrol DPI related stuff,these are the things you can do:
  • almost WVGA portrait(1)Use high resolutionimages to preservecrispiness.
  • almost WVGA portrait(2)Use the device-pixel-ratio mediaquery to serveDPI-specific CSS.
  • almost WVGA portraitI’ve set the1500×1500pxbackground-imageto repeat every1000px, making itcrispy again. Therest of the content isstill scaled 150%.
  • NB:-o-max/min-device-pixel-ratio uses fractions-webkit-max/min-device-pixel-ratio uses numbers
  • almost WVGA portrait(3)Set the metaviewport’s target-densitydpi propertyto device-dpi.
  • almost WVGA portrait(3)Everything is shownat 100%. One CSSpixel is equal to onedevice pixel.
  • NB:There are other target-densitydpi values, such ashigh-dpi, medium-dpi, low-dpi, as well as actual DPInumbers, but don’t bother with them. Just usedevice-dpi, and then only if really needed.
  • http://www.flickr.com/photos/33284937@N04/5588693890/
  • @viewportCSS Device Adaptation ED
  • Why doing all this viewportstuff with <meta> tags? Isn’tthis something for CSS?
  • <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">becomes@viewport { width: device-width; zoom: 1; user-zoom: fixed;}
  • <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">becomes@-o-viewport { width: device-width; zoom: 1; user-zoom: fixed;}
  • @-o-viewport { width: device-width;}@media screen and (max-device-width: 480px) { @-o-viewport { width: 480px; }}
  • Craz y stu ff@-o-viewport { width: device-width;}@media screen and (max-device-width: 480px) { @-o-viewport { width: 480px; }}
  • Phew, that was all!Thanks for listening :-) @andreasbovensThanks to ~yc for the wooden background: http://yc.deviantart.com/art/Wood-Rays-62468397