css3媒體查詢

來源:互聯網
上載者:User

標籤:io   使用   ar   div   sp   cti   代碼   on   c   

最常見的辦法就是基類(最常用的網站布局)+擴充類(幾種不同的網站布局類)來實現不同的布局。 

<!–使用說明:
網站基本布局,使用class="layout";
使用ipad訪問時,追加class="layout-ipad";
使用iphone訪問時,追加class="layout-iphone";
使用iphone橫屏訪問時,追加class="layout-iphone-h";
使用行動裝置解析度小於320px*480px訪問時,追加class="layout-miscreen";
–>
<div class="layout layout-ipad">
   <header>header</header>
   <section>main</section>
   <footer>footer</footer>
</div> 

針對不同布局編寫不同的css代碼,通過js判斷裝置、不同解析度調用不同的配置樣式,從而實現同一套前端Html代碼適配不同裝置和情境,給使用者帶來最佳的操作體驗。 

自從響應式布局的概念誕生以來,它便火了起來。 

官方是這麼定義響應式布局設計的: 


響應式布局是Ethan Marcotte在2010年5月份提出的一個概念,簡而言之,就是一個網站能夠相容多個終端——而不是為每個終端做一個特定的版本。
這個概念是為解決移動互連網瀏覽而誕生的。
響應式布局可以為不同終端的使用者提供更加舒適的介面和更好的使用者體驗,而且隨著目前大螢幕行動裝置的普及,用大勢所趨來形容也不為過。 

用一句話來說:
使用同一套Html代碼來適配不同裝置和滿足不同情境不同使用者使用。 

關鍵專業術語:
Media Query(css3媒介查詢) 

文法結構及用法:
@media 裝置名稱 only (選取條件) not (選取條件) and(裝置選取條件),裝置二{sRules} 

實際應用一 判斷裝置橫豎屏:
/* 這是匹配橫屏的狀態,橫屏時的css代碼 */
@media all and (orientation :landscape){} 
/* 這是匹配豎屏的狀態,豎屏時的css代碼 */
@media all and (orientation :portrait){}
  
實際應用二 判斷裝置類型:
@media X and (min-width:200px){} 
X為裝置類型》比如print/screen/TV等等


實際應用三 判斷裝置寬高:
/* 寬度大於600px小於960之間時,隱藏footer結構 */
@media all and (min-height:640px) and (max-height:960px){
    footer{display:none;}
} 

實際應用四 判斷裝置像素比:
/* 像素比為1時,頭部顏色為綠色 */
.header { background:red;display:block;}或
@media only screen and (-moz-min-device-pixel-ratio: 1), only screen and (-o-min-device-pixel-ratio: 1), only screen and (-webkit-min-device-pixel-ratio: 1), only screen and (min-device-pixel-ratio:1) {
.header{background:green;} } 
/* 像素比為1.5時,頭部背景為紅色 */
@media only screen and (-moz-min-device-pixel-ratio: 1.5), only screen and (-o-min-device-pixel-ratio: 1.5), only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio:1.5) {
.header{background:red;} }
/*像素比為2,頭部背景為藍色 */
@media only screen and (-moz-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2), only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio:2){
.header{background:blue;} }  

關於裝置像素比, 您可以參考:
HOW TO UNPREFIX -WEBKIT-DEVICE-PIXEL-RATIO?
Device pixel density tests What‘s My Device Pixel Ratio?
PPI、裝置像素比devicePixelRatio簡單介紹、 在各種高解析度裝置中使用像素 

開發中,可使用Chrome emulation類比行動裝置的真實具體參數值。

css3媒體查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.