<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
轉(zhuǎn)載于 Lew's Blog
什么是Viewport
手機瀏覽器是把頁面放在一個虛擬的“窗口”(viewport)中烹看,通常這個虛擬的“窗口”(viewport)比屏幕寬,這樣就不用把每個網(wǎng)頁擠到很小的窗口中(這樣會破壞沒有針對手機瀏覽器優(yōu)化的網(wǎng)頁的布局)仪壮,用戶可以通過平移和縮放來看網(wǎng)頁的不同部分。移動版的 Safari 瀏覽器最新引進了 viewport 這個 meta tag如贷,讓網(wǎng)頁開發(fā)者來控制 viewport 的大小和縮放厚宰,其他手機瀏覽器也基本支持。
Viewport 基礎(chǔ)
一個常用的針對移動網(wǎng)頁優(yōu)化過的頁面的 viewport meta 標簽大致如下:
<meta name=”viewport” content=”width=device-width, initial-scale=1, maximum-scale=1″>
width
:控制 viewport 的大小鲫竞,可以指定的一個值辐怕,如果 600,或者特殊的值从绘,如 device-width 為設備的寬度(單位為縮放為 100% 時的 CSS 的像素)
height
:和 width 相對應寄疏,指定高度
initial-scale
:初始縮放比例,也即是當頁面第一次 load 的時候縮放比例
maximum-scale
:允許用戶縮放到的最大比例
minimum-scale
:允許用戶縮放到的最小比例
user-scalable
:用戶是否可以手動縮放
shrink-to-fit=no
下面的一行代碼可以讓網(wǎng)頁的寬度自動適應手機屏幕的寬度:
<meta name="viewport" content="width=device-width, initial-scale=1">
但在iOS9中要想起作用僵井,得加上"shrink-to-fit=no"
陕截,原因如下:
Viewport meta tags using "width=device-width" cause the page to scale down to fit content that overflows the viewport bounds. You can override this behavior by adding "shrink-to-fit=no" to your meta tag as shown below. The added value will prevent the page from scaling to fit the viewport.
Reference