<link href="/style.css" rel="stylesheet" media="all and (color)" type="text/css" />
<!--?xml-stylesheet media="all and (color)" rel="stylesheet" href="example.css" ?-->
在CSS檔裡面設定作用對象
@import url(/style.css) all and (color);
@media all and (color) {
⋮ one or more rule sets…
}
條件的用法
@media [media type] and [(media feature)]
例:如果視窗最小寬度為500px,就套用這些CSS @media screen and (min-width:500px) {.....}
例:如果視窗為直立,就套用這些CSS @media screen and (orientation: portrait) {.....}
例:如果視窗在400px和700px之間(兩者需同時符合),就套用這些CSS @media screen and (min-width: 400px) and (max-width: 700px)
例:如果是彩色螢幕或彩色投影機兩者之一(兩者符合一種即可),就套用這些CSS @media screen and (color), projection and (color) {.....}
屬性和值
media type類型:
比較要注意的是,handheld這個值其實並沒有辦法有效的判別是否為手持設備,現在會讀handheld的手機瀏覽器有:OpenWave, Nokia lite-web browsers, Netfront, Digia, BlackBerry browser, Opera Mini until v4, Opera Mobile until v9,Palm’s Blazer, Nokia S40 browser, IEMobile 6.x and 8.x。所以若是要判別是否是使用iPhone或Android手機,可以使用下面的判斷式:
// target mobile devices
@media only screen and (max-device-width: 480px) {
body { max-width: 100%; }
}
// recent Webkit-specific media query to target the iPhone 4's high-resolution Retina display
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
// CSS goes here
}
// should technically achieve a similar result to the above query,
// targeting based on screen resolution (the iPhone 4 has 326 ppi/dpi)
@media only screen and (min-resolution: 300dpi) {
// CSS goes here
}
media features
* (max-或min-)width:[數字]
* (max-或min-)height:[數字]
* (max-或min-)device-width:[數字]
* (max-或min-)device-height:[數字]
* orientation:portrait 或 landscape
* aspect-ratio:[比值]
* (max-或min-)device-aspect-ratio:[比值]
* color
* color-index
* monochrome
* (max-或min-)resolution:[數字]dpi
* scan(只對tv)
* grid
在JS裡取得螢幕的真實寬度:
function getWindowWidth() {
var windowWidth = 0;
if (typeof(window.innerWidth) == 'number') {
windowWidth = window.innerWidth;
}
else {
if (document.documentElement && document.documentElement.clientWidth) {
windowWidth = document.documentElement.clientWidth;
}
else {
if (document.body && document.body.clientWidth) {
windowWidth = document.body.clientWidth;
}
}
}
return windowWidth;
}