• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Supporting Different Screens in Web Apps
2@jd:body
3
4<div id="qv-wrapper">
5<div id="qv">
6<h2>In this document</h2>
7<ol>
8<li><a href="#Metadata">Using Viewport Metadata</a>
9  <ol>
10    <li><a href="#ViewportSize">Defining the viewport size</a></li>
11    <li><a href="#ViewportScale">Defining the viewport scale</a></li>
12    <li><a href="#ViewportDensity">Defining the viewport target density</a></li>
13  </ol>
14</li>
15<li><a href="#DensityCSS">Targeting Device Density with CSS</a></li>
16<li><a href="#DensityJS">targeting Device Density with JavaScript</a></li>
17</ol>
18
19<h2>See also</h2>
20<ul>
21  <li><a href="https://developers.google.com/chrome/mobile/docs/webview/pixelperfect"
22  >Pixel-Perfect UI in the WebView</a></li>
23  <li><a href="http://www.html5rocks.com/en/mobile/responsivedesign/" class="external-link">Creating
24  a Mobile-First Responsive Web Design</a></li>
25  <li><a href="http://www.html5rocks.com/en/mobile/high-dpi/" class="external-link">High
26  DPI Images for Variable Pixel Densities</a></li>
27</ul>
28
29</div>
30</div>
31
32
33<p>Because Android is available on devices with a variety of screen sizes and pixel densities, you
34should account for these factors in your web design so your web pages always appear at the
35appropriate size.</p>
36
37
38<p>When targeting your web pages for Android devices, there are two major factors that you
39should account for:</p>
40
41<dl>
42  <dt><a href="#Viewport">The viewport</a></dt>
43    <dd>The viewport is the rectangular area that provides a drawable region for your web page.
44    You can specify several viewport properties, such as its size and initial scale. Most important
45    is the view port width, which defines the total number of horizontal pixels available from the web page's point of
46    view (the number of CSS pixels available).
47   </dd>
48
49  <dt><a href="#DensityCSS">The screen density</a></dt>
50<dd>The {@link android.webkit.WebView} class and most web browsers on Android convert your CSS
51pixel values to density-independent pixel values, so your web page appears at the same perceivable
52size as a medium-density screen (about 160dpi). However, if graphics are an important element of
53your web design, you should pay close attention to the scaling that occurs on different densities,
54because a 300px-wide image on a 320dpi screen will be scaled up (using more physical pixels per CSS
55pixel), which can produce artifacts (blurring and pixelation).</dd>
56
57</dl>
58
59
60
61
62
63<h2 id="Viewport">Specifying Viewport Properties</h2>
64
65<p>The viewport is the area in which your web page is drawn. Although the viewport's total visible
66area matches the size of the screen when zoomed all the way out, the viewport has its own pixel
67dimensions that it makes available to a web page. For example, although a device screen might
68have physical a width
69of 480 pixels, the viewport can have a width of 800 pixels. This allows a web page designed at 800
70pixels wide to be completely visible on the screen when the viewport scale is 1.0. Most web browsers on
71Android (including Chrome) set the viewport to a large size by default (known as "wide viewport
72mode" at about 980px wide). Many browsers also zoom out as far as possible, by default, to
73show the full viewport width (known as "overview mode").</p>
74
75<p class="note"><strong>Note:</strong>
76When your page is rendered in a {@link android.webkit.WebView}, it does not use wide viewport mode
77(the page appears at full zoom) by default. You can enable wide viewport mode
78with {@link android.webkit.WebSettings#setUseWideViewPort setUseWideViewPort()}.</p>
79
80<p>You can define properties of the viewport for your web page, such as the width and initial zoom
81level, using the {@code <meta name="viewport" ...>} tag in your document
82{@code <head>}.</p>
83
84<p>The following syntax shows all of the
85supported viewport properties and the types of values accepted by each one:</p>
86
87<pre>
88&lt;meta name="viewport"
89      content="
90          <b>height</b> = [<em>pixel_value</em> | "device-height"] ,
91          <b>width</b> = [<em>pixel_value</em> | "device-width"] ,
92          <b>initial-scale</b> = <em>float_value</em> ,
93          <b>minimum-scale</b> = <em>float_value</em> ,
94          <b>maximum-scale</b> = <em>float_value</em> ,
95          <b>user-scalable</b> = ["yes" | "no"]
96          " /&gt;
97</pre>
98
99<p>For example, the following {@code <meta>} tag specifies that the viewport width
100should exactly match the device screen's width and that the ability to zoom should be disabled:</p>
101
102<pre>
103&lt;head&gt;
104    &lt;title&gt;Example&lt;/title&gt;
105    &lt;meta name="viewport" content="width=device-width, user-scalable=no" /&gt;
106&lt;/head&gt;
107</pre>
108
109
110<p>When optimizing your site for mobile devices, you should usually set the width to
111{@code "device-width"} so the size fits exactly on all devices, then use CSS media queries to
112flexibly adapt layouts to suit different screen sizes.
113
114<p class="note"><strong>Note:</strong> You should disable user scaling only when you're certain
115that your web page layout is flexible and the content will fit the width of small screens.</p>
116
117
118
119
120
121
122<h2 id="DensityCSS">Targeting Device Density with CSS</h2>
123
124<p>The Android Browser and {@link android.webkit.WebView} support a CSS media feature that allows
125you to create styles for specific
126screen densities&mdash;the <code>-webkit-device-pixel-ratio</code> CSS media feature. The
127value you apply to this feature should be either
128"0.75", "1", or "1.5", to indicate that the styles are for devices with low density, medium density,
129or high density screens, respectively.</p>
130
131<p>For example, you can create separate stylesheets for each density:</p>
132
133<pre>
134&lt;link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio: 1.5)" href="hdpi.css" /&gt;
135&lt;link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio: 1.0)" href="mdpi.css" /&gt;
136</pre>
137
138
139<p>Or, specify the different styles in one stylesheet:</p>
140
141<pre class="no-pretty-print">
142#header {
143    background:url(medium-density-image.png);
144}
145
146&#64;media screen and (-webkit-device-pixel-ratio: 1.5) {
147    /* CSS for high-density screens */
148    #header {
149        background:url(high-density-image.png);
150    }
151}
152
153&#64;media screen and (-webkit-device-pixel-ratio: 0.75) {
154    /* CSS for low-density screens */
155    #header {
156        background:url(low-density-image.png);
157    }
158}
159</pre>
160
161
162<p>For more information about handling different screen densities, especially images, see
163<a href="http://www.html5rocks.com/en/mobile/high-dpi/" class="external-link">High
164  DPI Images for Variable Pixel Densities</a>.</li>
165
166
167<h2 id="DensityJS">Targeting Device Density with JavaScript</h2>
168
169<p>The Android Browser and {@link android.webkit.WebView} support a DOM property that allows you to
170query the density of the current
171device&mdash;the <code>window.devicePixelRatio</code> DOM property. The value of this property
172specifies the scaling factor used for the current device. For example, if the value
173of <code>window.devicePixelRatio</code> is "1.0", then the device is considered a medium density
174device and no scaling is applied by default; if the value is "1.5", then the device is
175considered a high density device and the page is scaled 1.5x by default; if the value
176is "0.75", then the device is considered a low density device and the page is scaled
1770.75x by default. Of course, the scaling that the Android Browser and {@link android.webkit.WebView}
178apply is based on the web page's
179target density&mdash;as described in the section about <a href="#ViewportDensity">Defining the
180viewport target density</a>, the default target is medium-density, but you can change the
181target to affect how your web page is scaled for different screen densities.</p>
182
183<p>For example, here's how you can query the device density with JavaScript:</p>
184
185<pre>
186if (window.devicePixelRatio == 1.5) {
187  alert("This is a high-density screen");
188} else if (window.devicePixelRatio == 0.75) {
189  alert("This is a low-density screen");
190}
191</pre>
192
193
194