Safari on iPhone OS displays webpages at a scale that works for most web content originally designed for the desktop. If these default settings don’t work for your webpages, it is highly recommended that you change the settings by configuring the viewport. You especially need to configure the viewport if you are designing webpages specifically for iPhone OS. Configuring the viewport is easy—just add one line of HTML to your webpage—but understanding how viewport properties affect the presentation of your webpages on iPhone OS is more complex. Before configuring the viewport, you need a deeper understanding of what the visible area and viewport are on iPhone OS.
If you are already familiar with the viewport on iPhone OS, read “Using the Viewport Meta Tag” for details on the viewport tag and “Viewport Settings for Web Applications” for web application tips. Otherwise, read the sections in this chapter in the following order:
Read “Layout and Metrics on iPhone and iPod touch” to learn about the available screen space for webpages on small devices.
Read “What Is the Viewport?” for a deeper understanding of the viewport on iPhone OS.
Read “Default Viewport Settings” and “Using the Viewport Meta Tag” for how to use the viewport meta tag.
Read “Changing the Viewport Width and Height” and “How Safari Infers the Width, Height, and Initial Scale” to understand better how setting viewport properties affects the way webpages are rendered on iPhone OS.
Read “Viewport Settings for Web Applications” if you are designing a web application for iPhone OS.
See “Supported Meta Tags” for a complete description of the viewport meta tag.
Because Safari on iPhone OS adds controls above and below your web content, you don’t have access to the entire screen real estate. In portrait orientation, the visible area for web content on iPhone and iPod touch is 320 x 356 pixels as shown in Figure 1-1. In landscape orientation, the visible area is 480 x 208 pixels.
Note that if the URL text field is not in use, it is anchored above the webpage and moves with the webpage when the user pans. This adds 60 pixels to the height of the visible area. However, since the URL text field can appear at any time, you should not rely on this extra real estate when designing your webpage. Video playback uses the entire screen on small devices.
Read “Laying Out Forms” in “Designing Forms” for more metrics when the keyboard is displayed for user input.
Note: Although it is helpful to know the metrics on small devices like iPhone and iPod touch, you should avoid using these values in your code. Read “Using the Viewport Meta Tag” for how to use the viewport meta tag constants.
The viewport on the desktop and the viewport on iPhone OS are slightly different.
Safari on iPhone OS has no windows, scroll bars, or resize buttons as shown on the right in Figure 3-2. The user pans by flicking a finger. The user zooms in by double-tapping and pinch opening, and zooms out by pinch closing—gestures that are not available for Safari on the desktop. Because of the differences in the way users interact with web content, the viewport on the desktop and on iPhone OS are not the same. Note that these differences between the viewports may affect some of the HTML and CSS instructions on iPhone OS.
The viewport on the desktop is the visible area of the webpage as shown in Figure 3-3. The user resizes the viewport by resizing the window. If the webpage is larger than the viewport, then the user scrolls to see more of the webpage. When the viewport is resized, Safari may change the document’s layout—for example, expand or shrink the width of the text to fit. If the webpage is smaller than the viewport, it is filled with white space to fit the size of the viewport.
For Safari on iPhone OS, the viewport is the area that determines how content is laid out and where text wraps on the webpage. The viewport can be larger or smaller than the visible area.
When the user pans a webpage on iPhone OS, gray bars appear on the right and bottom sides of the screen as visual feedback to show the user the size of the visible area as compared to the viewport (similar to the length of scroll bars on the desktop). Using the double tap, pinch open, and pinch close gestures, users can change the scale of the viewport but not the size. The only exception is when the user changes from portrait to landscape orientation—under certain circumstances, Safari on iPhone OS may adjust the viewport width and height, and consequently, change the webpage layout.
You can set the viewport size and other properties of your webpage. Mostly, you do this to improve the presentation the first time iPhone OS renders the webpage.
The viewport on iPhone OS is best illustrated using a few examples. Figure 3-4 shows a webpage on iPhone, containing a single 320 x 356 pixel image, that is rendered for the first time using the default viewport settings.
Figure 3-5 shows the same webpage with the viewport set to the size of the visible area, which is also the size of the image.
However, the viewport can be larger or smaller than the visible area. If the viewport is larger than the visible area, as shown in Figure 3-6, then the user pans to see more of the webpage.
Figure 3-7 show the webpage when it is smaller than the viewport and filled with white space.
The user can also zoom in and out using gestures. When zooming in and out, the user changes the scale of the viewport, not the size of the viewport. Consequently, panning and zooming do not change the layout of the webpage. Figure 3-8 shows the same webpage when the user zooms in to see details.
Safari on iPhone OS sets the size and scale of the viewport to reasonable defaults that work well for most webpages, as shown on the left in Figure 3-9. The default width is 980 pixels. However, these defaults may not work well for your webpages, particularly if you are tailoring your website for a particular device. For example, the webpage on the right in Figure 3-9 appears too narrow. Because Safari on iPhone OS provides a viewport, you can change the default settings.
Use the viewport
meta tag to improve the presentation of your web content on iPhone OS. Typically, you use the viewport
meta tag to set the width and initial scale of the viewport. For example, if your webpage is narrower than 980 pixels, then you should set the width of the viewport to fit your web content. If you are designing an iPhone or iPod touch-specific web application, then set the width to the width of the device. Refer to “Additional meta Tag Keys” in Safari HTML Reference for a detailed description of the viewport
meta tag.
Because iPhone OS runs on devices with different screen resolutions, you should use the constants instead of numeric values when referring to the dimensions of a device. Use device-width
for the width of the device and device-height
for the height in portrait orientation.
You do not need to set every viewport property. If only a subset of the properties are set, then Safari on iPhone OS infers the other values. For example, if you set the scale to 1.0
, Safari assumes the width is device-width
in portrait and device-height
in landscape orientation. Therefore, if you want the width to be 980 pixels and the initial scale to be 1.0, then set both of these properties.
For example, to set the viewport width to the width of the device, add this to your HTML file:
<meta name = "viewport" content = "width = device-width"> |
To set the initial scale to 1.0
, add this to your HTML file:
<meta name = "viewport" content = "initial-scale = 1.0"> |
To set the initial scale and to turn off user scaling, add this to your HTML file:
<meta name = "viewport" content = "initial-scale = 2.3, user-scalable = no"> |
Use the Safari on iPhone OS console to help debug your webpages as described in “Debugging.” The console contains tips to help you choose viewport values—for example, it reminds you to use the constants when referring to the device width and height.
Typically, you set the viewport width to match your web content. This is the single most important optimization that you can do for iPhone OS—make sure your webpage looks good the first time it is displayed on iPhone OS.
The majority of webpages fit nicely in the visible area with the viewport width set to 980
pixels in portrait orientation, as shown in Figure 3-10. If Safari on iPhone OS did not set the viewport width to 980
pixels, then only the upper-left corner of the webpage, shown in gray, would be displayed. However, this default doesn’t work for all webpages, so you’ll want to use the viewport
meta tag if your webpage is different.
If your webpage is narrower than the default width, as shown on the left in Figure 3-11, then set the viewport width to the width of your webpage, as shown on the right in Figure 3-11. To do this, add the following to your HTML file inside the <head>
block, replacing 590
with the width of your webpage:
<meta name = "viewport" content = "width = 590"> |
It is particularly important to change the viewport width for web applications designed for devices with smaller screens such as iPhone and iPod touch. Figure 3-12 shows the effect of setting the viewport width to device-width
. Read “Viewport Settings for Web Applications” for more web application tips.
Similarly you can set the viewport height to match your web content.
If you set only some of the properties, then Safari on iPhone OS infers the values of the other properties with the goal of fitting the webpage in the visible area. For example, if just the initial scale is set, then the width and height are inferred. Similarly, if just the width is set, then the height and initial scale are inferred, and so on. If the inferred values do not work for your webpage, then set more viewport properties.
Since any of the width, height, and initial scale may be inferred by Safari on iPhone OS, the viewport may resize when the user changes orientation. For example, when the user changes from portrait to landscape orientation by rotating the device, the viewport width may expand. This is the only situation where a user action might resize the viewport, changing the layout on iPhone OS.
Specifically, the goal of Safari on iPhone OS is to fit the webpage in the visible area when completely zoomed out by maintaining a ratio equivalent to the ratio of the visible area in either orientation. This is best illustrated by setting the viewport properties independently, and observing the effect on the other viewport properties. The following series of examples shows the same web content with different viewport settings.
Figure 3-13 shows a typical webpage displayed with the default settings where the viewport width is 980 and no initial scale is set.
Figure 3-14 shows the same webpage when the initial scale is set to 1.0 on iPhone. Safari on iPhone OS infers the width and height to fit the webpage in the visible area. The viewport width is set to device-width
in portrait orientation and device-height
in landscape orientation.
Similarly, if you specify only the viewport width, the height and initial scale are inferred. Figure 3-15 shows the rendering of the same webpage when the viewport width is set to 320 on iPhone. Notice that the portrait orientation is rendered in the same way as in Figure 3-14, but the landscape orientation maintains a width equal to device-width
, which changes the initial scale and has the effect of zooming in when the user changes to landscape orientation.
You can also set the viewport width to be smaller than the visible area with a minimum value of 200 pixels. Figure 3-16 shows the same webpage when the viewport width is set to 200 pixels on iPhone. Safari on iPhone OS infers the height and initial scale, which has the effect of zooming in when the webpage is first rendered.
Finally, Figure 3-17 shows the same webpage when both the width and initial scale are set on iPhone. Safari on iPhone OS infers the height by maintaining a ratio equivalent to the ratio of the visible area in either orientation. Therefore, if the width is set to 980 and the initial scale is set to 1.0 on iPhone, the height is set to 1091 in portrait and 425 in landscape orientation.
The minimum-scale
and maximum-scale
properties also affect the behavior when changing orientations. The range of these property values is from >0 to 10.0. The default value for both these properties is 0.25
.
If you are designing a web application specifically for iPhone OS, then the recommended size for your webpages is the size of the visible area on iPhone OS. Apple recommends that you set the width to device-width
so that the scale is 1.0 in portrait orientation and the viewport is not resized when the user changes to landscape orientation.
If you do not change the viewport properties, Safari on iPhone OS displays your webpage in the upper-left corner as shown in Figure 3-18. Setting the viewport width should be the first task when designing web applications for iPhone OS to avoid the user zooming in before using your application.
By setting the width to device-width
in portrait orientation, Safari on iPhone OS displays your webpage as show in Figure 3-19. Users can pan down to view the rest of the webpage if it is taller than the visible area. Add this line to your HTML file to set the viewport width to device-width
:
<meta name = "viewport" content = "width=device-width"> |
You may not want users to scale web applications designed specifically for iPhone OS. In this case, set the width and turn off user scaling as follows:
<meta name = "viewport" content = "user-scalable=no, width=device-width"> |
Last updated: 2010-03-24