天天看點

CSS3 Media Queries 媒體查詢

Media Queries直譯過來就是“媒體查詢”,在我們平時的Web頁面中head部分常看到這樣的一段代碼:

<link href="css/reset.css" target="_blank" rel="external nofollow"  rel="stylesheet" type="text/css" media="screen" />
<link href="css/style.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  rel="stylesheet" type="text/css" media="all" />
<link href="css/print.css" target="_blank" rel="external nofollow"  rel="stylesheet" type="text/css" media="print" />
           

或者這樣的形式:

<style type="text/css" media="screen">
    @import url("css/style.css");
</style>
           

不知道大家留意沒有,其中兩種方式引入CSS樣式都有一個共同的屬性“media”,而這個“media”就是用來指定特定的媒體類型,在HTML4和CSS2中充許你使用“media”來指定特定的媒體類型,如螢幕(screen)和列印(print)的樣式表,當然還有其他的,比如說“TV”,“handheld”等,其中“all”表示的是支援所有媒體媒體。有關于更多的Media類型,可以點選這裡。

上面簡單說了一下HTML4和CSS2的“Media Queries”,而今天的主要是來學習CSS3中的"Media Queries"的更多使用方法和相關知識,下面我們開始進入今天的主題。CSS3中的Media Queries增加了更多的媒體查詢,同時你可以添加不同的媒體類型的表達式用來檢查媒體是否符合某些條件,如果媒體符合相應的條件,那麼就會調用對應的樣式表。換句簡單的說,“在CSS3中我們可以設定不同類型的媒體條件,并根據對應的條件,給相應符合條件的媒體調用相對應的樣式表”。現在最常見的一個例子,你可以同時給PC機的大螢幕和移動裝置設定不同的樣式表。這功能是非常強大的,他可以讓你定制不同的分辨率和裝置,并在不改變内容的情況下,讓你制作的web頁面在不同的分辨率和裝置下都能顯示正常,并且不會是以而丢失樣式。

首先來看一個簡單的執行個體:

<link rel="stylesheet" media="screen and (max-width: 600px)" href="small.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  />
           

上面的media語句表示的是:當頁頁寬度小于或等于600px,調用small.css樣式表來渲染你的Web頁面。首先來看media的語句中包含的内容:

1、screen:這個不用說大家都知道,指的是一種媒體類型;

2、and:被稱為關鍵詞,與其相似的還有not,only,稍後會介紹;

3、(max-width:600px):這個就是媒體特性,說得通俗一點就是媒體條件。

前面這個簡單的執行個體引出兩個概念性的東西,一個就是媒體類型(Media Type)和 媒體特性(Media Query),首先一起來了解一下這兩個概念:

一、媒體類型(Media Type)

媒體類型(Media Type)在css2中是一個常見的屬性,也是一個非常有用的屬性,可以通過媒體類型對不同的裝置指定不同的樣式,在css2中我們常碰到的就是all(全部),screen(螢幕),print(頁面列印或打邱預覽模式),其實在媒體類型不止這三種,w3c總共列出了10種媒體類型。

頁面中引入媒體類型方法也有多種:

1、link方法引入

<link rel="stylesheet" type="text/css" href="../css/print.css" target="_blank" rel="external nofollow"  media="print" />
           

2、xml方式引入

<?xml-stylesheet rel="stylesheet" media="screen" href="css/style.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  ?>
           

3、@import方式引入

@import引入有兩種方式,一種是在樣式檔案中通過@import調用别一個樣式檔案;另一種方法是在<head></head>中的<style>...</style>中引入,單這種使用方法在ie6-7都不被支援 如

樣式檔案中調用另一個樣式檔案:

@import url("css/reset.css") screen;
@import url("css/print.css") print;
           

在<head></head>中的<style>...</style>中調用:

<head>
    <style type="text/css">
	@import url("css/style.css") all;
    </style>
</head>	
           

4、@media引入

這種引入方式和@import是一樣的,也有兩種方式:

樣式檔案中使用:

@media screen{
     選擇器{
	屬性:屬性值;
     }
}
           

在<head>>/head>中的<style>...</style>中調用:

<head>
    <style type="text/css">
	@media screen{
           選擇器{
	     屬性:屬性值;
	   }
	}
    </style>
  </head>	
           

以上幾種方法都有其各自的利弊,在實際應用中我建議使用第一種和第四種,因為這兩種方法是在項目制作中是常用的方法,對于他們的具體差別,我就不說了,想了解的大家可以去找度娘或G爸,他們能幫你解決。

二、媒體特性(Media Query)

前面有簡單的提到,Media Query是CSS3 對Media Type的增強版,其實可以将Media Query看成Media Type(判斷條件)+CSS(符合條件的樣式規則),常用的特性w3c共列出來13種。具體的可以參閱:Media features。為了更能了解Media Query,我們在次回到前面的執行個體上:

<link rel="stylesheet" media="screen and (max-width: 600px)" href="small.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  />
           

轉換成css中的寫法為:

@media screen and (max-width: 600px) {
    選擇器 {
      屬性:屬性值;
    }
  }
           

其實就是把small.css檔案中的樣式放在了@media srceen and (max-width;600px){...}的大括号之中。在語句上面的語句結構中,可以看出Media query和css的屬性集合很相似,主要差別在:

1、Media query隻接受單個的邏輯表達式作為其值,或者沒有值;

2、css屬性用于聲明如何表現頁頁的資訊;而Media Query是一個用于判斷輸出裝置是否滿足某種條件的表達式;

3、Media Query其中的大部分接受min/max字首,用來表示其邏輯關系,表示應用于大于等于或者小于等于某個值的情況

4、CSS屬性要求必須有屬性值,Media Query可以沒有值,因為其表達式傳回的隻有真或假兩種

常用的Media Query如下表所示:

CSS3 Media Queries 媒體查詢

相容的浏覽器:

CSS3 Media Queries 媒體查詢

下面我們一起來看看Media Queries的具體使用方式

一、最大寬度Max Width

<link rel="stylesheet" media="screen and (max-width:600px)" href="small.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  type="text/css" />
           

上面表示的是:當螢幕小于或等于600px時,将采用small.css樣式來渲染Web頁面。

二、最小寬度Min Width

<link rel="stylesheet" media="screen and (min-width:900px)" href="big.css" target="_blank" rel="external nofollow"  type="text/css"  />
           

上面表示的是:當螢幕大于或等于900px時,将采用big.css樣式來渲染Web頁面。

三、多個Media Queries使用

<link rel="stylesheet" media="screen and (min-width:600px) and (max-width:900px)" href="style.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  type="text/css" />
           

Media Query可以結合多個媒體查詢,換句話說,一個Media Query可以包含0到多個表達式,表達式又可以包含0到多個關鍵字,以及一種Media Type。正如上面的其表示的是當螢幕在600px-900px之間時采用style.css樣式來渲染web頁面。

四、裝置螢幕的輸出寬度Device Width

<link rel="stylesheet" media="screen and (max-device-width: 480px)" href="iphone.css" target="_blank" rel="external nofollow"  type="text/css" />
           

上面的代碼指的是iphone.css樣式适用于最大裝置寬度為480px,比如說iPhone上的顯示,這裡的max-device-width所指的是裝置的實際分辨率,也就是指可視面積分辨率

五、iPhone4

<link rel="stylesheet" media="only screen and (-webkit-min-device-pixel-ratio: 2)" type="text/css" href="iphone4.css" target="_blank" rel="external nofollow"  />
           

上面的樣式是專門針對iPhone4的移動裝置寫的。

六、iPad

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css" target="_blank" rel="external nofollow"  type="text/css" /> 
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css" target="_blank" rel="external nofollow"   type="text/css" />
           

在大數情況下,移動裝置iPad上的Safari和在iPhone上的是相同的,隻是他們不同之處是iPad聲明了不同的方向,比如說上面的例子,在縱向(portrait)時采用portrait.css來渲染頁面;在橫向(landscape)時采用landscape.css來渲染頁面。

七、android

/*240px的寬度*/
  <link rel="stylesheet" media="only screen and (max-device-width:240px)" href="android240.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  type="text/css" />
  /*360px的寬度*/
  <link rel="stylesheet" media="only screen and (min-device-width:241px) and (max-device-width:360px)" href="android360.css" target="_blank" rel="external nofollow"  type="text/css" />
  /*480px的寬度*/
  <link rel="stylesheet" media="only screen and (min-device-width:361px) and (max-device-width:480px)" href="android480.css" target="_blank" rel="external nofollow"  type="text/css" />
           

我們可以使用media query為android手機在不同分辨率提供特定樣式,這樣就可以解決螢幕分辨率的不同給android手機的頁面重構問題。

八、not關鍵字

<link rel="stylesheet" media="not print and (max-width: 1200px)" href="print.css" target="_blank" rel="external nofollow"  type="text/css" />
           

not關鍵字是用來排除某種制定的媒體類型,換句話來說就是用于排除符合表達式的裝置。

九、only關鍵字

<link rel="stylesheet" media="only screen and (max-device-width:240px)" href="android240.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  type="text/css" />
           

only用來定某種特定的媒體類型,可以用來排除不支援媒體查詢的浏覽器。其實only很多時候是用來對那些不支援Media Query但卻支援Media Type的裝置隐藏樣式表的。其主要有:支援媒體特性(Media Queries)的裝置,正常調用樣式,此時就當only不存在;對于不支援媒體特性(Media Queries)但又支援媒體類型(Media Type)的裝置,這樣就會不讀了樣式,因為其先讀only而不是screen;另外不支援Media Qqueries的浏覽器,不論是否支援only,樣式都不會被采用。

十、其他

在Media Query中如果沒有明确指定Media Type,那麼其預設為all,如:

<link rel="stylesheet" media="(min-width: 701px) and (max-width: 900px)" href="medium.css" target="_blank" rel="external nofollow"  type="text/css" />
           

另外還有使用逗号(,)被用來表示并列或者表示或,如下

<link rel="stylesheet" type="text/css" href="style.css" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  media="handheld and (max-width:480px), screen and (min-width:960px)" />
           

上面代碼中style.css樣式被用在寬度小于或等于480px的手持裝置上,或者被用于螢幕寬度大于或等于960px的裝置上。

關于Media Query的使用這一節就介紹到此,最後總體規納一下其功能,個人認為就是一句話:Media Queries能在不同的條件下使用不同的樣式,使用頁面達到不同的渲染效果。

如需轉載煩請注明出處: W3CPLUS