1.先看一个实例:当浏览器宽度改变时,应用的CSS发生变化。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Media Query Demos</title> <style type="text/css"> .wrapper { border: solid 1px #666; padding: 5px 10px; margin: 40px; } .viewing-area span { color: #666; display: none; } /* max-width */ @media screen and (max-width: 600px) { .one { background: #F9C; } span.lt600 { display: inline-block; } } /* min-width */ @media screen and (min-width: 900px) { .two { background: #F90; } span.gt900 { display: inline-block; } } /* min-width & max-width */ @media screen and (min-width: 600px) and (max-width: 900px) { .three { background: #9CF; } span.bt600-900 { display: inline-block; } } /* max device width */ @media screen and (max-device-width: 480px) { .iphone { background: #ccc; } } </style> </head> <body> <div class="wrapper one">This box will turn to pink if the viewing area is less than 600px</div> <div class="wrapper two">This box will turn to orange if the viewing area is greater than 900px</div> <div class="wrapper three">This box will turn to blue if the viewing area is between 600px and 900px</div> <div class="wrapper iphone">This box will only apply to devices with max-device-width: 480px (ie. iPhone)</div> <p class="viewing-area"><strong>Your current viewing area is:</strong> <span class="lt600">less than 600px</span> <span class="bt600-900">between 600 - 900px</span> <span class="gt900">greater than 900px</span></p> <p>Tutorial by <a href="http://www.webdesignerwall.com">Web Designer Wall</a></p> </body> </html>
2.看看 link 标签的媒体查询写法,代码:
<link rel="stylesheet" type="text/css" href="swordair.css" media="screen and (min-width: 400px)">
在media属性里:
screen 是媒体类型里的一种,CSS2.1定义了10种媒体类型
and 被称为关键字,其他关键字还包括 not(排除某种设备),only(限定某种设备)
(min-width: 400px) 就是媒体特性,其被放置在一对圆括号中。完整的特性参看 相关的Media features部分
媒体特性共13种,可以说是一个类似CSS属性的集合。但和CSS属性不同的是,媒体特性只接受单个的逻辑表达式作为其值,或者没有值。并且其中的大部分接受 min/max 的前缀,用来表示 大于等于/小于等于 的逻辑,以此避免使用 < 和 > 这些字符。

一个 Media Query 包含一种媒体类型,如果媒体类型没有指定,那么就是默认类型all,比如:
代码:
<link rel="stylesheet" type="text/css" href="example.css"
media="(max-width: 600px)">
一个 Media Query 包含0到多个表达式,表达式又包含0到多个关键字,以及一种媒体特性,比如:
代码:
<link rel="stylesheet" type="text/css" href="example.css"
media="handheld and (min-width:20em) and (max-width:50em)">
逗号(,)被用来表示并列,表示或者。比如下面的例子表示此CSS被应用于宽度小于20em的手持,或者宽度小于30em的屏幕:
代码:
<link rel="stylesheet" type="text/css" href="example.css"
media="handheld and (max-width:20em), screen and (max-width:30em)">
not 关键字用来排除符合表达式的设备,比如:
代码:
<link rel="stylesheet" type="text/css" href="example.css"
media="not screen and (color)">
代码:
<link rel="stylesheet" type="text/css" href="styleA.css"
media="screen">
<link rel="stylesheet" type="text/css" href="styleB.css"
media="screen and (max-width: 800px)">
<link rel="stylesheet" type="text/css" href="styleC.css"
media="screen and (max-width: 600px)">
并非所有的浏览器都支持Media Queries,那么这些浏览器怎么看待Media Queries?
Media Queries是CSS3对于Media Type的一个扩展,所以不支持Media Queries的浏览器,应该仍然要识别Media Type,但是IE只是简单的忽略了样式。only 关键字可能显得有些多余,对支持Media Queries的浏览器来说确实是这样,因为加不加 only 没有影响。only的作用,很多时候是用来对那些不支持Media Queries但是却读取Media Type的设备隐藏样式表的。比如:
代码:
<link rel="stylesheet" type="text/css" href="example.css"
media="only screen and (color)">
支持Media Queries的设备,正确应用样式,就仿佛only不存在
不支持Media Queries但正确读取Media Type的设备,由于先读取到only而不是screen,将忽略这个样式
不支持Media Queries的IE不论是否有only,都忽略样式
最后再来看看 Media Queries 的支持情况。不出意外的,IE678全部出局,但是IE9幸免。根据IEBlog上的这篇 HTML5 and Same Markup 来看,IE9支持Media Queries。至于其他浏览器,同样不出意外的,全部支持Media Queries。
浙公网安备 33010602011771号