SlideShare a Scribd company logo
1 of 124
Download to read offline
Optimizing your layout
for phones and tablets
using viewport and
media queries
Andreas 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 Caballero


Patrick Lauke
                     Zi Bin Cheah                     Vadim Makeev           Mike Taylor
“Patterns for
Multiscreen
Strategies”
                                                 Coherence                      Syncronization                   Screen sharing



By 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                     Simultaneity

These patterns should help understand and define strategies for the multiscreen world.

                                                                                  http://slidesha.re/kiip5y
“A digital product or
service looks and
works coherently
across devices.
Features are optimized
for specific device
characteristics and
usage 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 commonly
though, you’ll want to
focus on controlling
page 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 stuff
maximum-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 stuff
maximum-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 stuff
maximum-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 stuff
maximum-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 as
delimiters between viewport values!
Remember this, especially when syntax-googling ;-)
http://www.flickr.com/photos/33284937@N04/4771132618/
So, how to use this?
Sniffing for mobile
browsers maybe?
So, how to use this?
Sniffing for mobile
browsers 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 users
to make corrections.
So, avoid sniffing,
but if you really have to*,
provide a way for users
to make corrections.

* always ask yourself why the mobile site should be
different from the desktop site
* always ask yourself why the mobile site should be
     different from the desktop site


customer requir
                   ements
                                          tim e restrictions

                  budget limitations

capabilities are different
                                  different layout needed
* always ask yourself why the mobile site should be
     different from the desktop site


customer requir
                   ements
                                          tim e restrictions

                   budget limitations

capabilities are different
                                  different layout needed
     capability detection
* always ask yourself why the mobile site should be
     different from the desktop site


customer requir
                   ements
                                          tim e restrictions

                   budget limitations

capabilities are different
                                  different layout needed
     capability detection
                                     viewport + media queries
Let’s talk about
viewport + media queries
media queries
Media queries are conditional hooks
for applying different CSS rules,
depending on e.g. browser width,
screen height, aspect ratio, etc.
Differently said, media queries
allow you to optimize your site
layout 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 a
default viewport width (e.g. 850px).
For your mobile specific media queries
to 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 queries
combo allows you to create just
one responsive site that works
everywhere.
Q: What about max/min-device-width/height?

A: There are use cases for this, but note that your
media queries won’t be applied when resizing the
browser on desktop.
Q: Should I start from small to large, or vice versa?

A: Do mobile first, then enhance for desktop. This
will make the mobile experience faster. Deal with
old 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 different
elements’ CSS properties when needed.
http://www.flickr.com/photos/33284937@N04/5445675024/
Dealing with
high-DPI screens
A pixel is not what it seems
Thus far, we’ve talked about
pixels in terms of “CSS pixels”.
One CSS pixel can be multiple
device pixels.
HVGA portrait




almost WVGA portrait
HVGA portrait




almost WVGA portrait
almost WVGA portrait
Device is 480px
wide, but browser
applies default
zoom of 150%.

480px/1.5 = 320px
In most scenarios, you won’t
have to worry about this.
It just works.
However, if you want to
control DPI related stuff,
these are the things you can do:
almost WVGA portrait
(1)
Use high resolution
images to preserve
crispiness.
almost WVGA portrait
(2)
Use the device-
pixel-ratio media
query to serve
DPI-specific CSS.
almost WVGA portrait
I’ve set the
1500×1500px
background-image
to repeat every
1000px, making it
crispy again. The
rest of the content is
still 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 meta
viewport’s target-
densitydpi property
to device-dpi.
almost WVGA portrait
(3)
Everything is shown
at 100%. One CSS
pixel is equal to one
device pixel.
NB:
There are other target-densitydpi values, such as
high-dpi, medium-dpi, low-dpi, as well as actual DPI
numbers, but don’t bother with them. Just use
device-dpi, and then only if really needed.
http://www.flickr.com/photos/33284937@N04/5588693890/
@viewport
CSS Device Adaptation ED
Why doing all this viewport
stuff with <meta> tags? Isn’t
this 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 :-)
                    @andreasbovens


Thanks to ~yc for the wooden background: http://yc.deviantart.com/art/Wood-Rays-62468397

More Related Content

Viewers also liked

Introduction marketing-industriel
Introduction marketing-industrielIntroduction marketing-industriel
Introduction marketing-industriel
Mémoire Mémoire
 
Video marketing en B2B - Outil de Content Marketing
Video marketing en B2B - Outil de Content MarketingVideo marketing en B2B - Outil de Content Marketing
Video marketing en B2B - Outil de Content Marketing
Christelle Austruy
 
Sniffer for detecting lost mobiles
Sniffer for detecting lost mobilesSniffer for detecting lost mobiles
Sniffer for detecting lost mobiles
akhila immadisetty
 
Sniffer for detecting lost mobile ppt
Sniffer for detecting lost mobile pptSniffer for detecting lost mobile ppt
Sniffer for detecting lost mobile ppt
asmita tarar
 

Viewers also liked (15)

Introduction marketing-industriel
Introduction marketing-industrielIntroduction marketing-industriel
Introduction marketing-industriel
 
Video marketing en B2B - Outil de Content Marketing
Video marketing en B2B - Outil de Content MarketingVideo marketing en B2B - Outil de Content Marketing
Video marketing en B2B - Outil de Content Marketing
 
9 tips to boost your innovation project (by @nickdemey @boardofinno)
9 tips to boost your innovation project (by @nickdemey @boardofinno)9 tips to boost your innovation project (by @nickdemey @boardofinno)
9 tips to boost your innovation project (by @nickdemey @boardofinno)
 
Sniffer for detecting lost mobiles
Sniffer for detecting lost mobilesSniffer for detecting lost mobiles
Sniffer for detecting lost mobiles
 
Things to Watch: Music Edition (October 2011)
Things to Watch: Music Edition (October 2011)Things to Watch: Music Edition (October 2011)
Things to Watch: Music Edition (October 2011)
 
Sniffer for detecting lost mobiles
Sniffer for detecting lost mobilesSniffer for detecting lost mobiles
Sniffer for detecting lost mobiles
 
Sniffer for the mobile phones
Sniffer for the mobile phonesSniffer for the mobile phones
Sniffer for the mobile phones
 
Axure for dummies, that don't even know they are
Axure for dummies, that don't even know they areAxure for dummies, that don't even know they are
Axure for dummies, that don't even know they are
 
Sniffer ppt
Sniffer pptSniffer ppt
Sniffer ppt
 
How to deal with tech media by @mikebutcher
How to deal with tech media by @mikebutcherHow to deal with tech media by @mikebutcher
How to deal with tech media by @mikebutcher
 
Sniffer for Detecting Lost Mobile
Sniffer for Detecting Lost MobileSniffer for Detecting Lost Mobile
Sniffer for Detecting Lost Mobile
 
Sniffer for detecting lost mobile ppt
Sniffer for detecting lost mobile pptSniffer for detecting lost mobile ppt
Sniffer for detecting lost mobile ppt
 
Responsive Design with Axure 7.0’s Adaptive Views
Responsive Design with Axure 7.0’s Adaptive ViewsResponsive Design with Axure 7.0’s Adaptive Views
Responsive Design with Axure 7.0’s Adaptive Views
 
How to Extend Axure's Animation Capability
How to Extend Axure's Animation CapabilityHow to Extend Axure's Animation Capability
How to Extend Axure's Animation Capability
 
Best Of Marketing B2B 2011
Best Of Marketing B2B 2011Best Of Marketing B2B 2011
Best Of Marketing B2B 2011
 

More from Andreas Bovens

More from Andreas Bovens (10)

Progressive Web Apps: Opera's perspective
Progressive Web Apps: Opera's perspectiveProgressive Web Apps: Opera's perspective
Progressive Web Apps: Opera's perspective
 
Intro to @viewport & other new Responsive Web Design CSS features
Intro to @viewport & other new Responsive Web Design CSS featuresIntro to @viewport & other new Responsive Web Design CSS features
Intro to @viewport & other new Responsive Web Design CSS features
 
Responsive design: techniques and tricks to prepare your websites for the mul...
Responsive design: techniques and tricks to prepare your websites for the mul...Responsive design: techniques and tricks to prepare your websites for the mul...
Responsive design: techniques and tricks to prepare your websites for the mul...
 
Fast & Furious: Speed in the Opera browser
Fast & Furious: Speed in the Opera browserFast & Furious: Speed in the Opera browser
Fast & Furious: Speed in the Opera browser
 
SVG in Opera’s desktop, mobile & TV browsers — edition 2011
SVG in Opera’s desktop, mobile & TV browsers  — edition 2011SVG in Opera’s desktop, mobile & TV browsers  — edition 2011
SVG in Opera’s desktop, mobile & TV browsers — edition 2011
 
Building rich apps with HTML5 and friends
Building rich apps with HTML5 and friendsBuilding rich apps with HTML5 and friends
Building rich apps with HTML5 and friends
 
SVG in the Opera browser
SVG in the Opera browserSVG in the Opera browser
SVG in the Opera browser
 
Mobile web development techniques (and Opera's developer tools)
Mobile web development techniques (and Opera's developer tools)Mobile web development techniques (and Opera's developer tools)
Mobile web development techniques (and Opera's developer tools)
 
Opera and the Open Web platform
Opera and the Open Web platformOpera and the Open Web platform
Opera and the Open Web platform
 
Opera Widgets for Desktop and Opera Dragonfly
Opera Widgets for Desktop and Opera DragonflyOpera Widgets for Desktop and Opera Dragonfly
Opera Widgets for Desktop and Opera Dragonfly
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Recently uploaded (20)

Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 

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

  • 1. Optimizing your layout for phones and tablets using viewport and media queries Andreas Bovens - Opera Software
  • 2. about me http://www.flickr.com/pho tos/thomaaas/4783952591 /
  • 3. about me http://www.flickr.com/pho tos/thomaaas/4783952591 /
  • 4. about me http://www.flickr.com/pho tos/thomaaas/4783952591 / ccio1/119626133/ om/photos/bocca http://www.flickr.c
  • 5. about me http://www.flickr.com/pho tos/thomaaas/4783952591 / ccio1/119626133/ om/photos/bocca http://www.flickr.c
  • 6. 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/
  • 7. Bruce Lawson Daniel Davis Divya Manian Chris Mills Karl Dubost Shwetank Dixit Tiffany Brown Andreas Bovens Luz Caballero Patrick Lauke Zi Bin Cheah Vadim Makeev Mike Taylor
  • 8. “Patterns for Multiscreen Strategies” Coherence Syncronization Screen sharing By Precious http://slidesha.re/kiip5y Device shifting Complementarity Simultaneity These patterns should help understand and define strategies for the multiscreen world.
  • 9. Coherence Syncronization Screen sharing Device shifting Complementarity Simultaneity These patterns should help understand and define strategies for the multiscreen world. http://slidesha.re/kiip5y
  • 10. “A digital product or service looks and works coherently across devices. Features are optimized for specific device characteristics and usage scenarios.” Coherence http://slidesha.re/kiip5y
  • 11. Coherence http://slidesha.re/kiip5y
  • 12. ?
  • 14.
  • 15.
  • 16. small screen rendering aka single column view
  • 17.
  • 18.
  • 19.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28. That’s all nice, but what if I want to control this zooming behavior?
  • 30.
  • 31. This is the viewport
  • 33. Most commonly though, you’ll want to focus on controlling page width...
  • 34. <meta ...> ... using a "viewport" meta tag in the <head> of the page.
  • 35. works in: with some exceptions ;-)
  • 39. HVGA portrait no viewport defined in <head>, so fallback to default of 850px, which is squeezed inside 320px.
  • 40. HVGA portrait <meta name="viewport" content="width=320">
  • 41. HVGA portrait <meta name="viewport" content="width=320">
  • 42. HVGA portrait HVGA landscape <meta name="viewport" content="width=320">
  • 43. HVGA portrait <meta name="viewport" content="width=device-width">
  • 44. HVGA portrait <meta name="viewport" content="width=device-width">
  • 45. HVGA portrait HVGA landscape <meta name="viewport" content="width=device-width">
  • 46. HVGA landscape HVGA landscape 480px 320px × 1.5 zoom
  • 47. HVGA landscape HVGA landscape 480px 320px × 1.5 zoom
  • 48. HVGA portrait <meta name="viewport" content="initial-scale=1">
  • 49. HVGA portrait <meta name="viewport" content="initial-scale=1">
  • 50. HVGA portrait HVGA landscape <meta name="viewport" content="initial-scale=1">
  • 51. HVGA portrait HVGA landscape <meta name="viewport" content="initial-scale=1"> (same result as width=device-width)
  • 52. HVGA portrait HVGA landscape <meta name="viewport" content="initial-scale=0.5">
  • 53. HVGA portrait HVGA landscape <meta name="viewport" content="initial-scale=2">
  • 55. HVGA portrait <meta name="viewport" content="width=device-width"> + <div style="width: 600px">
  • 56. HVGA portrait <meta name="viewport" content="width=device-width, initial-scale=1"> + <div style="width: 600px">
  • 57. other stuff maximum-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">
  • 58. other stuff maximum-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">
  • 59. other stuff maximum-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">
  • 60. other stuff maximum-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">
  • 61. NB: Make sure to use commas, not semi-colons as delimiters between viewport values! Remember this, especially when syntax-googling ;-)
  • 63. So, how to use this? Sniffing for mobile browsers maybe?
  • 64. So, how to use this? Sniffing for mobile browsers maybe? Better don’t sniff.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72. Opera/9.80 (Macintosh; Intel Mac OS X 10.5.8; U; en) Presto/2.9.220 Version/12.00
  • 73. Opera/9.80 (Macintosh; Intel Mac OS X 10.5.8; U; en) Presto/2.9.220 Version/12.00
  • 74.
  • 75.
  • 76.
  • 77.
  • 78. So, avoid sniffing, but if you really have to, provide a way for users to make corrections.
  • 79. So, avoid sniffing, but if you really have to*, provide a way for users to make corrections. * always ask yourself why the mobile site should be different from the desktop site
  • 80. * always ask yourself why the mobile site should be different from the desktop site customer requir ements tim e restrictions budget limitations capabilities are different different layout needed
  • 81. * always ask yourself why the mobile site should be different from the desktop site customer requir ements tim e restrictions budget limitations capabilities are different different layout needed capability detection
  • 82. * always ask yourself why the mobile site should be different from the desktop site customer requir ements tim e restrictions budget limitations capabilities are different different layout needed capability detection viewport + media queries
  • 83. Let’s talk about viewport + media queries media queries
  • 84. Media queries are conditional hooks for applying different CSS rules, depending on e.g. browser width, screen height, aspect ratio, etc.
  • 85. Differently said, media queries allow you to optimize your site layout for all kinds of form factors. Via http://mediaqueri.es/
  • 86. @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; } }
  • 87. @media screen and (min-width: 400px) and (max-width: 800px) { article { /* css for browsers with width ≥ 400px and ≤ 800px */ } }
  • 88. @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 */ } }
  • 89. @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 */ } }
  • 90. @media screen and (max-width: 400px) { article { /* css for browsers with width ≤ 400px */ } } Remember that mobile browsers have a default viewport width (e.g. 850px). For your mobile specific media queries to work, you need to set the viewport!
  • 91.
  • 92. default viewport. width=device-width. mobile specific media mobile specific media queries not applied. queries applied.
  • 93. The viewport and media queries combo allows you to create just one responsive site that works everywhere.
  • 94.
  • 95. Q: What about max/min-device-width/height? A: There are use cases for this, but note that your media queries won’t be applied when resizing the browser on desktop.
  • 96. Q: Should I start from small to large, or vice versa? A: Do mobile first, then enhance for desktop. This will make the mobile experience faster. Deal with old IE using a polyfill, such as Respond.js https://github.com/scottjehl/Respond
  • 97. Q: Which screen sizes should I design for? A: All of them! No, seriously, don’t limit yourself. Resize your browser window and tweak different elements’ CSS properties when needed.
  • 100. A pixel is not what it seems Thus far, we’ve talked about pixels in terms of “CSS pixels”. One CSS pixel can be multiple device pixels.
  • 103. almost WVGA portrait Device is 480px wide, but browser applies default zoom of 150%. 480px/1.5 = 320px
  • 104. In most scenarios, you won’t have to worry about this. It just works.
  • 105. However, if you want to control DPI related stuff, these are the things you can do:
  • 106. almost WVGA portrait (1) Use high resolution images to preserve crispiness.
  • 107.
  • 108. almost WVGA portrait (2) Use the device- pixel-ratio media query to serve DPI-specific CSS.
  • 109. almost WVGA portrait I’ve set the 1500×1500px background-image to repeat every 1000px, making it crispy again. The rest of the content is still scaled 150%.
  • 110.
  • 112. almost WVGA portrait (3) Set the meta viewport’s target- densitydpi property to device-dpi.
  • 113. almost WVGA portrait (3) Everything is shown at 100%. One CSS pixel is equal to one device pixel.
  • 114.
  • 115. NB: There are other target-densitydpi values, such as high-dpi, medium-dpi, low-dpi, as well as actual DPI numbers, but don’t bother with them. Just use device-dpi, and then only if really needed.
  • 118. Why doing all this viewport stuff with <meta> tags? Isn’t this something for CSS?
  • 119.
  • 120. <meta name="viewport" content="width=device- width, initial-scale=1, user-scalable=no"> becomes @viewport { width: device-width; zoom: 1; user-zoom: fixed; }
  • 121. <meta name="viewport" content="width=device- width, initial-scale=1, user-scalable=no"> becomes @-o-viewport { width: device-width; zoom: 1; user-zoom: fixed; }
  • 122. @-o-viewport { width: device-width; } @media screen and (max-device-width: 480px) { @-o-viewport { width: 480px; } }
  • 123. Craz y stu ff @-o-viewport { width: device-width; } @media screen and (max-device-width: 480px) { @-o-viewport { width: 480px; } }
  • 124. Phew, that was all! Thanks for listening :-) @andreasbovens Thanks to ~yc for the wooden background: http://yc.deviantart.com/art/Wood-Rays-62468397