The use of mobile devices to surf the web is growing at an astronomical pace, but unfortunately much of the web isn’t optimized for those mobile devices. Mobile devices are often constrained by display size and require a different approach to how content is laid out on the screen.<\/p>\n
A multitude of different screen sizes exist across phones, “phablets,” tablets, desktops, game consoles, TVs, and even wearables. Screen sizes are always changing, so it’s important that your site can adapt to any screen size, today or in the future.<\/p>\n
Pages optimized for a variety of devices must include a meta viewport tag in the head of the document. A meta viewport tag gives the browser instructions on how to control the page’s dimensions and scaling.<\/p>\n
<\/p>\n
Using the meta viewport value\u00a0width=device-width\u00a0instructs the page to match the screen’s width in device-independent pixels. This allows the page to reflow content to match different screen sizes, whether rendered on a small mobile phone or a large desktop monitor.<\/p>\n
In addition to setting an\u00a0initial-scale, you can also set the following attributes on the viewport:<\/p>\n
When set, these can disable the user’s ability to zoom the viewport, potentially causing accessibility issues.<\/p>\n
On both desktop and mobile devices, users are used to scrolling websites vertically but not horizontally; forcing the user to scroll horizontally or to zoom out in order to see the whole page results in a poor user experience.<\/p>\n
When developing a mobile site with a\u00a0meta viewport\u00a0tag, it’s easy to accidentally create page content that doesn’t quite fit within the specified viewport. For example, an image that is displayed at a width wider than the viewport can cause the viewport to scroll horizontally. You should adjust this content to fit within the width of the viewport, so that the user does not need to scroll horizontally.<\/p>\n
Setting large absolute CSS widths for page elements (such as the example below), cause the\u00a0div\u00a0to be too wide for the viewport on a narrower device (for example, a device with a width of 320 CSS pixels, such as an iPhone). Instead, consider using relative width values, such as\u00a0width: 100%. Similarly, beware of using large absolute positioning values that may cause the element to fall outside the viewport on small screens.<\/p>\n