在CSS2中允许使用“media”来指定特定的媒体类型,如屏幕(screen),打印(print),TV, handheld等,其中all表示的是支持所有媒体介质。
CSS3中的Media Queries增加了更多的媒体查询,同时可以添加不同的媒体类型的表达式用来检查媒体是否符合某些条件,如果媒体符合相应的条件,那么就会调用对应的样式表。这功能是非常强大的,可以定制不同的分辨率和设备,并在不改变内容的情况下,让web页面在不同的分辨率和设备下都能显示正常,并且不会因此而丢失样式。
一、媒体类型(Media Type)
媒体类型(Media Type)在css2中是一个常见的属性,w3c总共列出了10种媒体类型。
页面中引入媒体类型方法也有多种:
1、link方法引入
<link rel="stylesheet" type="text/css" href="../css/print.css" media="print" />
2、xml方式引入
3、@import方式引入
@import引入有两种方式,一种是在样式文件中通过@import调用别一个样式文件;另一种方法是在
中的中引入,单这种使用方法在ie6-7都不被支持 如
样式文件中调用另一个样式文件:
@import url("css/reset.css") screen; @import url("css/print.css") print;
在
中的中调用:
<head> <style type="text/css"> @import url("css/style.css") all; style> head>
4、@media引入
这种引入方式和@import是一样的,也有两种方式:
样式文件中使用:
@media screen{
选择器{
属性:属性值;
}
}
在
>/head>中的中调用:
<head> <style type="text/css"> @media screen{ 选择器{ 属性:属性值; } } style> head>
以上几种方法都有其各自的利弊,在实际应用中我建议使用第一种和第四种,因为这两种方法是在项目制作中是常用的方法。
二、媒体特性(Media Query)
常用的Media Query如下表所示:
兼容的浏览器:
Media Queries的具体使用方式
一、最大宽度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" />
四、设备屏幕的输出宽度Device Width
<link rel="stylesheet" media="screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
这里的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" />
在纵向(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" />
八、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的设备上。
转至W3CPLUS