標籤:效果 規則 聲明 title drm nbsp iphone4 方法 實際應用
一、媒體類型(Media Type)
1、link方法引入
<link rel="stylesheet" type="text/css" href="../css/print.css" media="print" />
2、xml方式引入
<?xml-stylesheet rel="stylesheet" media="screen" href="css/style.css" ?>
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>
以上幾種方法都有其各自的利弊,在實際應用中我建議使用第一種和第四種,因為這兩種方法是在項目製作中是常用的方法。
二、媒體特性(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" />
轉換成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如下表所示:
相容的瀏覽器:
一、最大寬度Max Width
<link rel="stylesheet" media="screen and (max-width:600px)" href="small.css" type="text/css" />
上面表示的是:當螢幕小於或等於600px時,將採用small.css樣式來渲染Web頁面。
二、最小寬度Min Width
<link rel="stylesheet" media="screen and (min-width:900px)" href="big.css" 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" 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" 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" />
上面的樣式是專門針對iPhone4的行動裝置寫的。
六、iPad
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css" type="text/css" /> <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css" 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" type="text/css" /> /*360px的寬度*/ <link rel="stylesheet" media="only screen and (min-device-width:241px) and (max-device-width:360px)" href="android360.css" type="text/css" /> /*480px的寬度*/ <link rel="stylesheet" media="only screen and (min-device-width:361px) and (max-device-width:480px)" href="android480.css" type="text/css" />
我們可以使用media query為android手機在不同解析度提供特定樣式,這樣就可以解決螢幕解析度的不同給android手機的頁面重構問題。
八、not關鍵字
<link rel="stylesheet" media="not print and (max-width: 1200px)" href="print.css" type="text/css" />
not關鍵字是用來排除某種制定的媒體類型,換句話來說就是用於排除符合運算式的裝置。
九、only關鍵字
<link rel="stylesheet" media="only screen and (max-device-width:240px)" href="android240.css" 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" type="text/css" />
另外還有使用逗號(,)被用來表示並列或者表示或,如下
<link rel="stylesheet" type="text/css" href="style.css" media="handheld and (max-width:480px), screen and (min-width:960px)" />
上面代碼中style.css樣式被用在寬度小於或等於480px的手持功能上,或者被用於螢幕寬度大於或等於960px的裝置上。
關於Media Query的使用這一節就介紹到此,最後總體規納一下其功能,個人認為就是一句話:Media Queries能在不同的條件下使用不同的樣式,使用頁面達到不同的渲染效果。
轉載自:http://www.w3cplus.com/content/css3-media-queries
CSS3-媒體類型