題外話
本來是想寫篇關於Bootstrap的Scaffolding博文的,不過對於響應式Web設計不是很了解,所以就先有了這篇博文。
博客園原文地址:http://www.cnblogs.com/yhuang/archive/2012/03/29/responsive_web_design.html
目錄
什么是響應式Web設計(Responsive Web design)?
initial-scale=1.0,maximum-scale=1.0
@media all and (device-width:480)
前言
現在使用移動設備人越來越多,移動版的Website隨之也越來越重要;但是移動端設備的大小不一,屏幕分辨率各不相同,我們不可能為BlackBerry,iPhone, iPad等等每個都做單獨的頁面設計。所以我們需要的Website設計要能迎合多種device的要求並且兼容所有的屏幕分辨率,這種設計就叫響應式Web設計
什么是響應式Web設計(Responsive Web design)?
響應式Web設計是一種Web設計和開發能夠根據屏幕大小、平台和方向對用戶的行為和環境做出響應的設計。它包括了靈活的網格和布局,圖像和智能使用CSS的media queries特性。當用戶從Laptop切換到iPad上時,該網站應能自動地切換CSS規則以適應Device分辨率,圖像尺寸和腳本執行。換句話說,響應式Web設計是具備能夠自動響應用戶喜好的技術。這將一勞永逸的滿足對每個新上市的移動終端都要進行不同設計和開發的需求。
需要注意的是:響應式Web設計不僅僅是可調屏幕分辨率和可自動調整大小的圖像,而且更是一種采用全新思考的Website設計方法。
響應式Web設計的用途
隨着越來越多的設備而來的是各式各樣的屏幕分辨率、定義和方向。每天都有正在開發的新屏幕尺寸的設備。每個設備都可以擁有不同的尺寸、功能、甚至顏色。有些是橫向,有些是縱向,甚至還有些完全是正方形的。如圖所示:
解決問題的關鍵就是Media queries和Viewports。下面的內容是關於如何最好的使用Media queries和Viewports的。但是我並不會深入的講怎樣增強響應的細節,如果感興趣,可以查看這篇博文的最后一部分:“參考文章”。
/* Default wide-screen styles */
@media all and (max-width: 1024px) {
/* styles for narrow desktop browsers and iPad landscape */
}
@media all and (max-width: 768px) {
/* styles for narrower desktop browsers and iPad portrait */
}
@media all and (max-width: 480px) {
/* styles for iPhone/Android landscape (and really narrow browser windows) */
}
@media all and (max-width: 320px) {
/* styles for iPhone/Android portrait */
}
@media all and (max-width: 240px) {
/* styles for smaller devices */
}
<meta name="viewport" content="...">
initial-scale=1設置告訴瀏覽器初始化頁面時不要對頁面進行縮放。解決了沒有使用viewport時出現的頁面縮放問題。
但還是有bug,當我們把移動端設備從縱向轉成橫向時,你就會發現這個問題了。這是因為initial-scale只在頁面完全加載后有作用。在我們把移動設備從縱向轉成橫向的過程中,瀏覽器就會認為頁面不變,但scales會設置為1.5,為了使320px的頁面適應480px。但是,因為我們在@media queries中設置了480px這個寬度,那么頁面CSS規則也會是適應480px的。結果就是,頁面CSS規則是適應480px的,另外scale還是1.5。這
個結果並不可怕,但是不可取。
user-scalable=no
設置也會讓用戶不能縮放頁面。所以一般情況下,不要使用以上倆個設置。
- 使用viewport標簽
- 使用media queries來選擇最適合頁面尺寸的CSS
- 在viewport標簽里,使用width=device-width,initial-scale=1或者單獨使用width=device-width
- 不要使用maximum-scale=1和user-scalable=no
- 不要使用width=<specific width>
- 不要使用@media all and (*-device-width: xxx)
參考文章
- Ethan Marcotte. 2010. Responsive Web Design. In A List Apart #306. ISSN: 1534-0295.
- Jeremy Keith. 2010. Responsive Enhancement. In adactio.
- Kayla Knight. 2011. Responsive Web Design: What It Is and How To Use It. In Smashing Magazine.
- Webkit based desktop browsers re-render the page correctly as you resize the browser, however they have a minimum width of 385px (on MacOSX) and I was unable to shrink the browser below this. Firefox 4 re-renders the page correctly until the width gets too narrow to fit the navigation toolbar. At that point the viewport width stays fixed even if you shrink the browser. The page is re-rendered if you type something (anything) into the URL bar. Opera 10/11 re-render correctly at all sizes.
- Peter Paul Koch. 2010. A tale of two viewports — part two. In Quirksmode.
- Using the Viewport on Safari. In Safari Web Content Guide.
- The viewport meta tag. In Safari HTML Reference.
- MDC. 2010. Using the viewport meta tag to control layout on mobile browsers. In Mozilla Developer Network.
- Peter Paul Koch. 2010. Combining meta viewport and media queries. In Quirksmode.
- Willison & Downe. Lanyrd.
- Lie et al. 2010. Media Queries. W3C Candidate Recommendation 27 July 2010.
- If you design your page for the narrow view and expect it to scale when rotated, then use width=device-width and nothing else. If, instead, you design your page for either width, then use width=device-width,initial-scale=1. This is the only way to get the android browser to render a page with the intended width. Mobile Safari will render the page exactly as if initial-scale=1 were specified alone. You will still end up with the zoom on rotate bug.
- David Calhoun. 2010. The viewport metatag (Mobile web part I).