blob: 7e023405fc0cfa1dac2bd0c9735b9bebfccdb71a [file] [log] [blame]
Scott Main5342f652013-09-10 10:54:46 -07001page.title=Supporting Different Screens in Web Apps
Scott Main65e62f42010-09-20 12:46:34 -07002@jd:body
3
4<div id="qv-wrapper">
5<div id="qv">
Scott Main65e62f42010-09-20 12:46:34 -07006<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>
Scott Main08810efc2010-10-11 15:37:14 -070015<li><a href="#DensityCSS">Targeting Device Density with CSS</a></li>
16<li><a href="#DensityJS">targeting Device Density with JavaScript</a></li>
Scott Main65e62f42010-09-20 12:46:34 -070017</ol>
18
Scott Main5342f652013-09-10 10:54:46 -070019<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
Scott Main65e62f42010-09-20 12:46:34 -070029</div>
30</div>
31
32
Scott Main5342f652013-09-10 10:54:46 -070033<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>
Scott Main08810efc2010-10-11 15:37:14 -070036
Scott Main08810efc2010-10-11 15:37:14 -070037
Scott Main5342f652013-09-10 10:54:46 -070038<p>When targeting your web pages for Android devices, there are two major factors that you
Scott Main08810efc2010-10-11 15:37:14 -070039should account for:</p>
Scott Main65e62f42010-09-20 12:46:34 -070040
41<dl>
Scott Main5342f652013-09-10 10:54:46 -070042 <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>
Scott Main65e62f42010-09-20 12:46:34 -070048
Scott Main5342f652013-09-10 10:54:46 -070049 <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
Scott Main65e62f42010-09-20 12:46:34 -070057</dl>
58
Scott Main65e62f42010-09-20 12:46:34 -070059
60
61
Scott Main65e62f42010-09-20 12:46:34 -070062
Scott Main5342f652013-09-10 10:54:46 -070063<h2 id="Viewport">Specifying Viewport Properties</h2>
Scott Main65e62f42010-09-20 12:46:34 -070064
Scott Main5342f652013-09-10 10:54:46 -070065<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>
Scott Main65e62f42010-09-20 12:46:34 -070074
Scott Main5342f652013-09-10 10:54:46 -070075<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 &lt;meta name="viewport" ...>} tag in your document
82{@code &lt;head&gt;}.</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 &lt;meta&gt;} tag specifies that the viewport width
100should exactly match the device screen's width and that the ability to zoom should be disabled:</p>
Scott Main65e62f42010-09-20 12:46:34 -0700101
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
Scott Main65e62f42010-09-20 12:46:34 -0700109
Scott Main5342f652013-09-10 10:54:46 -0700110<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.
Scott Main65e62f42010-09-20 12:46:34 -0700113
Scott Main5342f652013-09-10 10:54:46 -0700114<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>
Scott Main65e62f42010-09-20 12:46:34 -0700116
117
118
119
Scott Main65e62f42010-09-20 12:46:34 -0700120
121
Scott Main08810efc2010-10-11 15:37:14 -0700122<h2 id="DensityCSS">Targeting Device Density with CSS</h2>
Scott Main65e62f42010-09-20 12:46:34 -0700123
Scott Main08810efc2010-10-11 15:37:14 -0700124<p>The Android Browser and {@link android.webkit.WebView} support a CSS media feature that allows
125you to create styles for specific
Scott Main65e62f42010-09-20 12:46:34 -0700126screen 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;
Scott Main65e62f42010-09-20 12:46:34 -0700136</pre>
137
138
Scott Main65e62f42010-09-20 12:46:34 -0700139<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) {
Scott Main6669f252011-04-15 17:55:33 -0700147 /* CSS for high-density screens */
Scott Main65e62f42010-09-20 12:46:34 -0700148 #header {
149 background:url(high-density-image.png);
150 }
151}
152
153&#64;media screen and (-webkit-device-pixel-ratio: 0.75) {
Scott Main6669f252011-04-15 17:55:33 -0700154 /* CSS for low-density screens */
Scott Main65e62f42010-09-20 12:46:34 -0700155 #header {
156 background:url(low-density-image.png);
157 }
158}
159</pre>
160
Scott Main65e62f42010-09-20 12:46:34 -0700161
Scott Main5342f652013-09-10 10:54:46 -0700162<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>
Scott Main65e62f42010-09-20 12:46:34 -0700165
166
Scott Main08810efc2010-10-11 15:37:14 -0700167<h2 id="DensityJS">Targeting Device Density with JavaScript</h2>
Scott Main65e62f42010-09-20 12:46:34 -0700168
Scott Main08810efc2010-10-11 15:37:14 -0700169<p>The Android Browser and {@link android.webkit.WebView} support a DOM property that allows you to
170query the density of the current
Scott Main65e62f42010-09-20 12:46:34 -0700171device&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
Scott Main08810efc2010-10-11 15:37:14 -07001770.75x by default. Of course, the scaling that the Android Browser and {@link android.webkit.WebView}
178apply is based on the web page's
Scott Main65e62f42010-09-20 12:46:34 -0700179target 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");
Scott Main6669f252011-04-15 17:55:33 -0700188} else if (window.devicePixelRatio == 0.75) {
Scott Main65e62f42010-09-20 12:46:34 -0700189 alert("This is a low-density screen");
190}
191</pre>
192
193