響應式web設計之CSS3 Media Queries

來源:互聯網
上載者:User

開始研究響應式web設計,CSS3 Media Queries是入門。

Media Queries,其作用就是允許添加運算式用以確定媒體的環境情況,以此來應用不同的樣式表。換句話說,其允許我們在不改變內容的情況下,改變頁面的布局以精確適應不同的裝置。

那麼,Media Queries是如何工作的?

兩種方式,一種是直接在link中判斷裝置的尺寸,然後引用不同的css檔案:

<link rel="stylesheet" type="text/css" href="styleA.css" media="screen and (min-width: 400px)">

意思是當螢幕的寬度大於等於400px的時候,應用styleA.css

在media屬性裡:

  • screen 是媒體類型裡的一種,CSS2.1定義了10種媒體類型
  • and 被稱為關鍵字,其他關鍵字還包括 not(排除某種裝置),only(限定某種裝置)
  • (min-width: 400px) 就是媒體特性,其被放置在一對圓括弧中。完整的特性參看 相關的Media features部分
<link rel="stylesheet" type="text/css" href="styleB.css"  media="screen and (min-width: 600px) and (max-width: 800px)">

意思是當螢幕的寬度大於600小於800時,應用styleB.css

其它屬性可看這裡:http://www.swordair.com/blog/2010/08/431/

另一種方式,即是直接寫在<style>標籤裡:

@media screen and (max-width: 600px) { /*當螢幕尺寸小於600px時,應用下面的CSS樣式*/  .class {    background: #ccc;  }}

寫法是前面加@media,其它跟link裡的media屬性相同

其實基本上就是樣式覆蓋~,判斷裝置,然後引用不同的樣式檔案覆蓋。

要注意的是由於網頁會根據螢幕寬度調整布局,所以不能使用絕對寬度的布局,也不能使用具有絕對寬度的元素。這一條非常重要,否則會出現橫向捲軸。

----------------------------------華麗的分割線-----------------------------------------------------------

以下是demo

一個三欄布局的,在不同的尺寸下,變為兩欄,再變為一欄~

代碼:

<!DOCTYPE HTML><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1" /><title>css3-media-queries-demo</title><style>body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, input, textarea, p, blockquote, th, td {padding: 0;margin: 0;}.content{zoom:1;}.content:after{content: ".";display: block;height: 0;clear: both;visibility: hidden; }.leftBox, .rightBox{float: left;width: 20%;height: 500px;margin: 5px;background: #ffccf7;display: inline;-webkit-transition: width 1s ease;-moz-transition: width 1s ease;-o-transition: width 1s ease;-ms-transition: width 2s ease;transition: width 1s ease;}.middleBox{float: left;width: 50%;height: 800px;margin: 5px;background: #b1fffc;display: inline;-webkit-transition: width 1s ease;-moz-transition: width 1s ease;-o-transition: width 1s ease;-ms-transition: width 1s ease;transition: width 1s ease;}.rightBox{background: #fffab1;}@media only screen and (min-width: 1024px){.content{width: 1000px;margin: auto}}@media only screen and (min-width: 400px) and (max-width: 1024px){.rightBox{width: 0;}.leftBox{ width: 30%}.middleBox{ width: 65%}}@media only screen and (max-width: 400px){.leftBox, .rightBox, .middleBox{ width: 98%;height: 200px;}}</style></head><body><div class="content">  <div class="leftBox"></div>  <div class="middleBox"></div>  <div class="rightBox"></div></div></body></html>

 

參考文章:http://www.swordair.com/blog/2010/08/431/

     http://www.zhangxinxu.com/wordpress/2011/08/css3-media-queries%E7%9A%84%E4%BA%9B%E9%87%8E%E5%8F%B2%E5%A4%96%E4%BC%A0/

     http://webdesignerwall.com/tutorials/css3-media-queries

     http://www.ruanyifeng.com/blog/2012/05/responsive_web_design.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.