In-depth Understanding of Viewport
Category Programming Techniques
When restructuring or developing web pages on mobile devices, the first thing to understand is the viewport on mobile devices. Only by understanding the concept of the viewport and clarifying the use of the meta tags related to the viewport can we better adapt or respond to various mobile devices with different resolutions.
I. The Concept of Viewport
In layman's terms, the viewport on mobile devices is the area of the device's screen that can be used to display our web pages. To be more specific, it is the part of the browser (or a webview in an app) that is used to display web pages. However, the viewport is not limited to the size of the browser's visible area; it may be larger or smaller than the browser's visible area. By default, the viewport on mobile devices is generally larger than the browser's visible area. This is because mobile devices generally have smaller resolutions compared to desktop computers. To display traditional websites designed for desktop browsers on mobile devices, mobile browsers set their default viewport to 980px or 1024px (or other values, which are determined by the device itself). However, this results in a horizontal scrollbar because the width of the browser's visible area is narrower than the default viewport width. The following figure lists the default viewport widths of browsers on some devices.
II. 1px in CSS is Not Equal to 1px on the Device
In CSS, we generally use px as the unit. In desktop browsers, 1 pixel in CSS often corresponds to 1 physical pixel on the computer screen, which may give us the illusion that the pixels in CSS are the physical pixels of the device. However, the reality is not the case. The pixel in CSS is just an abstract unit, and the physical pixels represented by 1px in CSS vary across different devices or environments. In web pages designed for desktop browsers, we don't need to be overly concerned about this, but on mobile devices, it is essential to understand this point. In earlier mobile devices, the screen pixel density was relatively low, such as the iPhone 3, which had a resolution of 320x480. On the iPhone 3, one CSS pixel indeed equals one screen physical pixel. Later, with technological advancements, the screen pixel density of mobile devices increased. Starting with the iPhone 4, Apple introduced the so-called Retina display, doubling the resolution to 640x960, while the screen size remained the same. This means that on the same screen size, there are twice as many pixels, and at this time, one CSS pixel equals two physical pixels. Other brands of mobile devices follow the same principle. For example, Android devices can be divided into different levels such as ldpi, mdpi, hdpi, xhdpi, etc., according to screen pixel density, and the resolutions are also diverse. How many screen physical pixels a CSS pixel is equivalent to on Android devices varies depending on the device, and there is no definitive answer.
Another factor that can cause changes in px in CSS is user zoom. For example, when a user zooms in on a page by one time, the physical pixels represented by 1px in CSS will also double; conversely, when the page is zoomed out by one time, the physical pixels represented by 1px in CSS will also halve. This point will be discussed in more detail later in the article.
In mobile and some desktop browsers, the window object has a devicePixelRatio property, which is officially defined as the ratio of physical pixels to device-independent pixels, that is, devicePixelRatio = physical pixels / independent pixels. The px in CSS can be considered as the independent pixels of the device, so through devicePixelRatio, we can know how many physical pixels a CSS pixel represents on the device. For example, on a Retina display iPhone, the value of devicePixelRatio is 2, which means 1 CSS pixel is equivalent to 2 physical pixels. However, it should be noted that there are still some compatibility issues with devicePixelRatio in different browsers, so we cannot fully trust this for now. The specific situation can be seen in this article.
Test results of devicePixelRatio:
III. PPK's Theory of Three Viewports
PPK has conducted extensive research on the viewport on mobile devices (first article, second article, third article), and interested students can take a look. Many data and viewpoints in this article also come from there. PPK believes The value can be a numerical value or one of the strings high-dpi, medium-dpi, low-dpi, device-dpi.
It is particularly noted that when target-densitydpi=device-dpi, 1px in CSS will be equal to 1px in physical pixels.
Since this attribute is only supported by Android and Android has decided to deprecate it.
V. Setting the current viewport width to the width of the ideal viewport
To achieve the ideal viewport, the width of the default layout viewport must be set to the screen width of the mobile device. Since the width in the meta viewport can control the width of the layout viewport, we only need to set the width to the special value width-device.
<meta name="viewport" content="width=device-width">
The following is the test result of this code on various mobile browsers:
It can be seen that with width=device-width, all browsers can change the current viewport width to the width of the ideal viewport. However, it should be noted that on the iPhone and iPad, whether in portrait or landscape mode, the width is always the width of the ideal viewport in portrait mode.
This writing method seems that anyone can do it, who hasn't eaten pork, who hasn't seen a pig run? Indeed, when we develop web pages on mobile devices, no matter whether you understand what a viewport is, you may only need this one line of code.
But you must not know
<meta name="viewport" content="initial-scale=1">
This line of code can also achieve the same effect as the previous line of code and can also change the current viewport to the ideal viewport.
Haha, you are confused, because theoretically, the function of this line of code is only not to scale the current page, that is, the page should be how big it is. So why does it have the effect of width=device-width?
To understand this matter clearly, you must first understand what the scaling is relative to, because the scaling value here is 1, which is no scaling, but it has achieved the effect of the ideal viewport. So, there is only one answer, the scaling is relative to the ideal viewport for scaling. When scaling the ideal viewport by 100%, that is, when the scaling value is 1, isn't it the ideal viewport? The facts have proven that it is indeed the case. The following figure is the test result of the major mobile browsers when set
The test results show that initial-scale=1 can also change the current viewport width to the width of the ideal viewport, but this time it is the IE on the Windows Phone that sets the width to the width of the ideal viewport in portrait mode, whether in portrait or landscape mode. But this minor flaw is already irrelevant.
But what if width and initial-scale=1 appear at the same time, and there is a conflict? For example:
<meta name="viewport" content="width=400, initial-scale=1">
width=400 means to set the current viewport width to 400px, and initial-scale=1 means to set the current viewport width to the width of the ideal viewport. So which command should the browser obey? Is it the one written later? No. When encountering this situation, the browser will take the larger of the two values. For example, when width=400 and the width of the ideal viewport is 320, 400 is taken; when width=400 and the width of the ideal viewport is 480, the width of the ideal viewport is taken. (ps: In the UC9 browser, when initial-scale=1, no matter what the value of the width attribute is, the viewport width is always the width of the ideal viewport at this time)
Finally, in summary, to set the current viewport width to the width of the ideal viewport, you can either set width=device-width or set initial-scale=1, but both have a small flaw, that is, the iPhone, iPad, and IE will not distinguish between portrait and landscape, and will all take the width of the ideal viewport in portrait mode as the standard. So, the most perfect writing method should be to write both, so that initial-scale=1 solves the problem of the iPhone and iPad, and width=device-width solves the problem of IE:
<meta name="viewport" content="width=device-width, initial-scale=1">
VI. More knowledge about meta viewport
1. About scaling and the default value of initial-scale
First, let's discuss the issue of scaling. It has been mentioned before that scaling is relative to the ideal viewport. The larger the scaling value, the smaller the width of the current viewport, and vice versa. For example, on the iPhone, the width of the ideal viewport is 320px. If we set initial-scale=2, the width of the viewport will become only 160px at this time, which is also understandable, as it has Why is it necessary to have an ideal viewport? For instance, a mobile phone with a resolution of 320x480 has an ideal viewport width of 320px, and another phone with the same screen size but a resolution of 640x960 also has an ideal viewport width of 320px. Why should the ideal width of the phone with the higher resolution be the same as that of the phone with the lower resolution? This is because only in this way can the same website look the same or almost the same on devices with different resolutions. In fact, although there are so many different types, brands, and resolutions of mobile phones on the market, their ideal viewport widths can be summarized as just a few, such as 320, 360, 384, 400, etc., all of which are very close to each other. The similarity in ideal viewport widths also means that a website designed for the ideal viewport of one device will not look much different or may even look the same on other devices.
>
Original article link: https://www.cnblogs.com/2050/p/3877280.html
**Click to share notes
-
-
-