device-width这个媒体查询可以测量设备像素。
width这个媒体查询可以测量页面的总宽度,css pixels。
1)HTML4和CSS2的“媒体查询”:
<link href = "css/reset.css" rel = "stylesheet" type="text/css" media="screen" />
或者这样的:
<style type="text/css" meida="screen"></style>
media这个属性,可以指定特定的媒体类型,如屏幕screen,打印print,还有“TV”,"handheld",“all”等。
所有类型详解:http://www.w3.org/TR/CSS2/media.html#media-types
2)CSS3增强版的"媒体查询":
比如:<link rel="stylesheet" media="screen and (max-width:600px)" href="small.css" />
首先来看media的语句中包含的内容:
1、screen:这个不用说大家都知道,指的是一种媒体类型;
2、and:被称为关键词,与其相似的还有not,only,稍后会介绍;
3、(max-width:600px):这个就是媒体特性,说得通俗一点就是媒体条件。
a)媒体类型Media Type
页面中引入媒体类型的方法有四种:
第一种:link方式
<link rel = "stylesheet" type="text/css" href = "css/print.css" media = "print" />
第二种:xml方式
<?xml - stylesheet rel="stylesheet" media="screen" href="css/style.css" ?>
第三种:@import方式
在样式文件中调用另一个样式文件:@import url("pirnt.css") print;
在<style>中调用:<style type="text/css"> @import url("style.css") all; </style>
第四种:@meida引入
在样式文件中使用:@media screen{}
在<style>中调用:<style type="text/css">
@media screen{}
</style>
b)媒体特性Media Query
媒体特性Media Query是css3对媒体类型的增强版,可以将Media Query看成Media Type(判断条件)+CSS(符合条件的样式规则)。
比如:
<link rel = "stylesheet" type="text/css" href = "css/small.css" media = "screen and (max-width:600px)" />
或者:
@media screen and (max-width:600px){
选择器{属性:属性值;}
}
not:
<link rel = "stylesheet" type="text/css" href = "css/print.css" media = "not print and (max-width:1200px)" />
only:
<link rel = "stylesheet" type="text/css" href = "css/android240.css" media = "only screen and (max-device-width:240px)" />
3)媒体查询的具体使用:
a)多个Media query使用
<link rel = "stylesheet" type="text/css" href = "css/style.css" media = "screen and (max-width:900px) and (min-width:600px)" />
Media Query可以结合多个媒体查询,换句话说,一个Media Query可以包含0到多个表达式,表达式又可以包含0到多个关键字,以及一种Media Type。正如上面的其表示的是当屏幕在600px-900px之间时采用style.css样式来渲染web页面。
b)设备屏幕的输出宽度Device width
<link rel = "stylesheet" type="text/css" href = "css/style.css" media = "screen and (max-device-width:480px)" />
上面的代码指的是iphone.css样式适用于最大设备宽度为480px,比如说iPhone上的显示,这里的max-device-width所指的是设备的实际分辨率,也就是指可视面积分辨率
c)iPhone4
<link rel="stylesheet" type="text/css" media="only screen and (-webkit-min-device-pixel-ratio:2)" href="iphone4.css" />
d)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来渲染页面。
e)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手机的页面重构问题。
f)其他
在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如下表所示: