分享CSS3中的Media Queries的學習總結

來源:互聯網
上載者:User
CSS3中的Media Queries經常被用來製作前端的響應式設計頁面,這裡分享CSS3中的Media Queries的學習總結,包括IE8中的相容問題解決,需要的朋友可以參考下

一、Media Queries 支援的屬性

二、關鍵字
and - 結合多個媒體查詢 not - 排除某種制定的媒體類型 only - 用來定某種特定的媒體類型

三、引用樣式樣本

<link rel="stylesheet" media="all" href="style.css" />   <link rel="stylesheet" media="screen and (min-width:980px)" href="desktop.css" />   <link rel="stylesheet" media="screen and (min-width:768px) and (max-width:980px)" href="pad.css" />   <link rel="stylesheet" media="screen and (min-width:480) and (max-width: 768px)" href="phone.css" />   <link rel="stylesheet" media="screen and (max-width:320px)" href="small.css" />


四、內聯樣式樣本

@media screen and (min-width: 980px) {       //css code}   @screen and (min-width:768px) and (max-width:980px) {       //css code}   @screen and (min-width:480) and (max-width: 768px) {       //css code}   @screen and (max-width:320px) {       //css code}   @media screen and (max-device-width: 480px) {       //max-device-width等於480px}   @media screen and (device-aspect-ratio: 16/9), screen and (device-aspect-ratio: 16/10) {       //裝置寬高比   }   @media all and (orientation:portrait) {       //豎屏   }   @media all and (orientation:landscape) {       //橫屏   }



五、I8的相容性問題解決
問題根源:
在項目的CSS檔案中採用了media這東東來根據視窗的大小自動調整布局,但是,但是IE8及以下版本瀏覽器不支援CSS3 media queries,也就是@media screen and (max-width: 900px) 裡面的css代碼沒有執行,

@media screen and (max-width: 900px) {     ...   }


這如何是好啊,網上倒是有不少人提出解決方案,最簡單的方法就是:
IE8和之前的瀏覽器不支援CSS3 media queries,你可以在頁面中添加css3-mediaqueries.js來解決這個問題。

<!--[if lt IE 9]>    <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->


原來如此,還挺簡單嘛,結果大失所望啊,項目中怎麼試怎麼就不行呢,還望試過可行的同仁點撥點撥啊,沒辦法只能採用另一種稍微複雜些的方法,也是從網上學來,這裡要考慮兩個問題,一是只有IE8及其低版本才做此處理,二是只有瀏覽器縮小到某一個大小範圍後才做此處理。方法如下:
原理:採用jquery,其實不懂,會用就行,然後在html中根據需要來改變對應的CSS檔案
解決方案:
在所有頁面的共用js檔案後面添加如下代碼:

/*Adjust the layout in IE8 and lower than IE8 when the browser is narrow*/function processLowerIENavigate()   {      var isIE = document.all ? 1 : 0;      if (isIE == 1)      {          if(navigator.userAgent.indexOf("MSIE7.0") > 0 || navigator.userAgent.indexOf("MSIE 8.0") > 0)          {         //  var doc=document;            var link=document.createElement("link");              link.setAttribute("rel", "stylesheet");              link.setAttribute("type", "text/css");              link.setAttribute("id", "size-stylesheet");              link.setAttribute("href", "");              var heads = document.getElementsByTagName("head");              if(heads.length)                  heads[0].appendChild(link);              else               document.documentElement.appendChild(link);              document.write("<script type='text/javascript' src='jquery.min.js'></script>");              document.write("<script type='text/javascript' src='media_screen.js'></script>");          }      }    }   var lowerIE8 = processLowerIENavigate();   media_screen.js檔案內容如下,直接從網上copy:   function adjustStyle(width) {       width = parseInt(width);       if (width < 902) {   //alert("<900");//alert(width);        $("#size-stylesheet").attr("href", "navigateLowerIE8.css");       } else {         // alert(">900");  //alert(width);       $("#size-stylesheet").attr("href", "");        }   }   $(function() {       adjustStyle($(this).width());       $(window).resize(function() {           adjustStyle($(this).width());       });   });


navigateLowerIE8.css檔案就是IE8其低版本瀏覽器在縮小時的頁面配置了。OK,一切都確實搞定。

聯繫我們

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