This paper records the contents of the viewport, and points out the shortcomings.
1, the Viewport
Sometimes a percentage is used to declare the width, such as:
html,body{}div{width:30%;}
Assuming that the DIV is a child element of the body, this CSS means that the 30%.body of the div's body width does not show the width of the declaration, so the body occupies 100% of the width of the parent's containing block HTML element. Similarly, HTML does not show the width of the declaration, so HTML also accounts for 100% of the parent's containing block. Wait a minute.. What is the parent containing block of HTML? is the viewport.
In the CSS standard document, it is called the initial inclusion block. This initial inclusion block is the source of all CSS percent width projections.
On the desktop, the width of the viewport is consistent with the width of the browser window . Except for the margin and padding,html and body elements are consistent with the width of the browser window. That's why the above Div occupies 30% of the browser's width.
2. Layout viewport
On the desktop, the viewport is the same width as the browser window, but on the phone, the viewport is not associated with the mobile browser screen width. Imagine that, in a small screen mobile device, if the viewport and mobile browser screen width Consistent, then the 30% of the body of the div on the phone to show the width is very very small, so the mobile browser manufacturer must ensure that even on the narrow screen under the element display is very good, So the width of the viewport is designed to be much wider than the width of the screen, so the site can be displayed as if it were on the desktop. However, if the site does not optimize the mobile side, then the browser will be as narrow as possible to allow users to see the full picture of the site.
Summary: On the phone, the viewport is not associated with the width of the screen, which is the opposite of the desktop. We call the viewport a layout viewport.
3. Visual Viewport
It is the area of the website that the user is seeing, note the area of the site (important thing to say three times). Users can zoom to see the content of the site. If the user narrows the site, we see the site area will become larger, at this time the visual viewport has become larger, the same, the user magnified the site, we can see the site area will be narrowed, the visual viewport also become smaller. Regardless of how the user scales, the width of the layout viewport is not affected.
4. Ideal Viewport
By default, a phone or tablet browser has a layout width of 768-1024 pixels. This is not ideal for a narrow screen of a mobile phone. In other words, the default width of the layout viewport is not an ideal width. So the ideal viewport was introduced.
The ideal viewport should be used only when the website is ready for the phone. To add the ideal viewport, you need to add a meta viewport label to the page, such as:
| 1 |
<metaname="viewport" content="width=device-width"> |
This line of code is to inform the browser that the width of the layout viewport should be the same as the ideal viewport width. This also shows that the ideal viewport is defined as the work of the browser, not the work of the device or the operating system. As a result, different browsers on the same device have different ideal viewports. The size of the browser's ideal viewport is also dependent on the device it is located in.
Summarize:
1, in the desktop browser, the browser window is the same width as the viewport, while the viewport (known as the "initial inclusion block" in the CSS standard document) is the source of the CSS percent width projection, so the browser window is the viewport that constrains the CSS layout;
2, on the phone, there are two viewports, layout viewport will limit the CSS layout, visual viewport determines the content of the site users see;
3. The mobile browser also has an ideal viewport, which is an ideal size for the layout viewport of a particular browser on a particular device;
4, the layout viewport size can be defined as the ideal viewport. This is also the basis for responsive design.
Mobile three-viewport