xTable of Contents [1] Layout viewport [2] visual viewport [3] Ideal viewport [4]meta label [5] summarize the previous words
In the CSS standard document, viewport viewport is called the initial containment block. This initial inclusion block is the source of all CSS percent width projections, which limits the CSS layout to a maximum width. On the desktop, the width of the viewport is the same as the width of the browser window. On the mobile side, viewports are divided into layout viewports (layouts viewport), Visual viewports (visual viewport), and ideal viewports (ideal viewport)
Layout viewport
The problem with mobile devices is that if the width of the viewport is the same as the width of the browser window, it can result in ugly results. Mobile browsers typically have widths of 240 to 640 pixels, and most Web sites designed to desktop are at least 800px in width. So website content looks very narrow on the phone
is the Sina network in the mobile phone side of the display state, if not to zoom operation, the text is almost impossible to see
On the phone, the viewport is no longer associated with the mobile browser screen width, but is completely independent. We call it layout viewport--css layout is calculated based on it and constrained by it
To accommodate Web sites designed for desktop browsers, the default layout viewport width of the mobile device is much larger than the width of the screen, set to 980px or 1024px (and possibly other values, as determined by the device itself), but the result is that the browser will appear with a horizontal scroll bar, Because the width of the viewable area of the browser is smaller than the width of this default viewport. Lists the width of the default viewport for browsers on some devices
Document.documentelement.clientwidth/height returns the dimensions of the layout viewport
Visual viewport
While the creation of independent layout viewports has largely helped the transfer of desktop websites to mobile phones, we cannot completely ignore the screen size of mobile devices. Some CSS declarations are related to what the user sees, regardless of the initial containing block of the CSS. And sometimes it's helpful to know what parts of the site are visible to web developers.
The visual viewport is the area of the site that the user is seeing, and for the JavaScript attribute is Window.innerwidth/height
[note] There are compatibility issues with Android Webkit2 and proxy browsers
Scaling affects the size of the visual viewport. When the zoom level is 100%, the visual viewport is as wide as the device screen. Zooming in makes the visual viewport smaller because the CSS pixels on the screen are smaller, and shrinking makes the visual viewport larger because the CSS pixels on the screen are more. So the zoom level and the size of the visual viewport are inverse Related: the larger the place, the smaller the visual viewport
In Iphone5, for example, the width of the browser layout viewport is 1024px by default, the screen width is only 640 device pixels, and DPR is 2, so the CSS pixel is 320px. Now the user zooms in from 100% to 200%,css pixels and is magnified until there are only 160 css pixels on the screen. However, the layout viewport remains at 1024px, so the elements in the page do not change size.
[note] When the user zooms in, only the dimensions of the visual viewport change, and the layout viewport does not change. Zooming on the mobile side does not cause the CSS layout to be recalculated. Since scaling is frequent on the phone, and the phone's processor works very slowly and the battery consumes quickly, there is a lot of performance benefit in not re-layout
The following code represents 3 seconds after the page is scaled from 100% to 200%
<Script>test.innerhtml= 'Screen Width:' +Screen.width+ '<br> Visual Viewports:' +Window.innerwidth;settimeout (function(){ varMeta=document.getElementsByTagName ('Meta')[1]; Meta.setattribute ('content','initial-scale=2'); Test.innerhtml= 'Screen Width:' +Screen.width+ '<br> Visual Viewports:' +window.innerwidth;}, the);</Script>Ideal Viewport
By default, the layout width of a phone or tablet browser is 980 or 1024 pixels. While this can keep the desktop site from being squashed, it's not ideal, especially for mobile users, because it's better for a narrow web site on a narrow screen.
In other words, the default width of the layout viewport is not an ideal width. That's why Apple and other browser makers who emulate Apple will introduce the ideal viewport. It is the ideal layout viewport size for the device. The Web site that is displayed in the ideal viewport has the best browsing and reading widths, and users no longer need to zoom when they first enter the page
The ideal viewport should be used only when the website is ready for the phone. The ideal eloquence will take effect only if you actively add meta viewport labels to the page. Without a meta-viewport label declaration, the layout viewport will maintain its default width, and the ideal viewport will only have an effect if it is explicitly used.
// This line of code tells the browser that the width of the layout viewport should be consistent with the width of the ideal viewport <meta name="device" content= " Width=device-width">
Defining the ideal viewport is 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. For example, the ideal viewport for Android WebKit on the Samsung Galaxy Pocket is 320*427px, while opera Mobile12 is 240*320px. However, the size of the browser's ideal viewport depends on the device in which it is located. The ideal viewport for Chrome on the Samsung Galaxy S4 is 360*640px, but on NEXUS7, it's 601*962px. The reason is obvious: Nexus7 is a flat plate that has a wider screen, so the ideal viewport should also be wider
Screen.width/height return is the size of the ideal viewport
[note] When the device orientation changes, the value of the ideal viewport screen.width/height in the iphone will not change, but the Android device will change. While the values of the layout viewport document.documentElement.clientWidth and visual viewport window.innerwidth, both Apple and Android will change
Meta viewport
The primary purpose of the Meta viewport label is to match the size of the layout viewport to the size of the ideal viewport. It was invented by Apple, and other phones and tablets copied most of its content. Desktop browsers do not support or need it because they do not have the concept of an ideal viewport. IE is an exception: it supports meta viewport tags on your phone, but it's best to use @-ms-viewport
The Meta viewport label should be placed in the
<meta name="viewport" content="name=value,name=value " >
Each name/value pair is an instruction that gives the browser a command to send a number. They are separated by commas and have a total of 6
1. Width: Sets the width of the layout viewport to a specific value
2. Init-scale: Set the initial zoom level of the page and the width of the layout viewport
3, Minimum-scale: Set the minimum zoom level (user can reduce the extent)
4, Maxmum-scale: Set the maximum zoom level (the extent to which the user can enlarge)
5, User-scalabel: whether to prevent users from scaling
6, Height: Set the elevation of the layout viewport (not implemented)
Width
"0" does not set the width
For example, if Iphone4s does not set viewport, he will default to 980px, just like dividing the screen into 980 copies. If you set an element to 100px*100px, it looks like 100/980 of the screen
"1" Sets the size of the layout viewport to an ideal value
<meta name="viewport" content="width=device-width">
If, iphone4s set viewport width=device-width, he would be 320px, just like dividing the screen into 320 parts. If you set an element to 100px*100px, it looks like 100/320 of the screen
"2" Sets the size of the layout viewport to a fixed width
<meta name="viewport" content="width=400">
The maximum value supported by the browser is 10,000 pixels, and the minimum value is approximately 20% of the ideal viewport, and the Android WebKit does not allow any less than the width of the layout viewport. If you specify a value like this, it is automatically converted to the default layout viewport, usually 980px. IE10 does not allow any value beyond 480px to be automatically converted to the default width of the layout viewport 1024px
Initial-scale
The initial-scale instruction sets the initial zoom level of the page. 1 represents the 100%,2 representative of 200%. The degree of scaling is calculated based on the ideal viewport.
Current Zoom value = Ideal viewport width/visual viewport width
[Note] Android's WebKit browser only behaves correctly when Initial-scale = 1 and the Width property is not set
The zoom level is inverse related to the width of the visual viewport, and the higher the zoom level means the smaller the visual viewport. So the visual viewport size is the same as the ideal viewport size when initial-scale=1. The initial-scale=2 is magnified to 200%, so the aspect of the visual viewport is half the ideal viewport
"Default Value"
Initial-scale on your Android device doesn't have a default value, and on iphone and ipad, regardless of the width of the viewport setting, if you don't specify a default zoom value, the iphone and ipad will automatically calculate this scaling value. To reach the current page there is no horizontal scroll bar (or viewport width is the width of the screen).
"1" width=device-width,initial-scale=1
In IE10, when Initial-scale is 1 o'clock, it maintains a width of 320px in landscape mode, but it changes from 320px to 480px when Width=device-width.
So in order to solve this problem on all browsers, you need to use
<meta name="viewport" content="width=device-width,initial-scale=1 ">
"2" width=400,initial-scale=1
<meta name="viewport" content="width=400,initial-scale=1" >
Set the width of the layout viewport to 400px, and then set it to the width of the ideal viewport. The result browser selects the largest size in each direction. As a result, the layout viewport width of the early iphone in portrait mode is 400px (larger values in 320px and 400px) and 480px (higher values for 480px and 400px) in landscape mode
Therefore, you can set a minimum width for the layout viewport and allow the browser to set the layout viewport wider when there is demand in the device and direction
Minimum-scale and Maximum-scale
Without these instructions, the browser allows the user to zoom to 5 (20%-500%), and with these commands, the range can be expanded to 10 (10% to 1000%). The higher zoom level is not supported, so setting the maxmum-scale=20 and setting the maxmum-scale=10 effect is the same. Android WebKit does not support Minimum-scale. Similarly, it has a scale range of 4 (25%-400%) and does not support changing this range
User-scalable
User-scalable=no indicates that scaling is prohibited
"Change meta viewport label"
In most browsers, you can change the Meta viewport label, assuming that the meta viewport is the first META tag in the document
var meta = document.getelementsbytagname (' meta ') [0];meta.setattribute (' content ', ' width=400 ');
We cannot change the layout viewport back to its default width by removing the meta tag. However, it can be set to a fixed value. For example, if you want to provide a "switch to Desktop Layout" feature, you can set the width to 980px or 1024px
Common Meta Viewport labels
<name= "Viewport" content= "Width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0 ">
The META tag's purpose is to make the current viewport width equal to the width of the device, while not allowing the user to manually scale
Summarize
In a desktop browser, a browser window is a viewport that constrains the layout of the CSS. On the phone side, the layout viewport restricts the CSS layout, and the visual viewport represents the viewable area of the browser, determining what the user sees; ideal viewport is an ideal size for a specific browser-specific layout viewport for a particular device
Viewport viewport for mobile web development