@media screen and (min-width:640px) {
/*屏幕大于640像素应用该样式*/
}
@media screen and (min-width:460px) and (max-width:640px) {
/*屏幕小于640大于460像素应用该样式*/
}
@media screen and (max-width:460px) {
/*屏幕小于460像素应用该样式*/
}
一般情况下,根据分辨率加载pc端 wap端 pad端三个css文件,示例:
<link rel="stylesheet" type="text/css" href="./css/style.css" media="all"><link rel="stylesheet" href="./css/phone.css" media="(max-width:620px)">
<link rel="stylesheet" href="./css/pad.css" media="screen and (max-width:1024px) and (min-width:621px)">
只有一个css文件情况下,根据分辨率调整css样式,示例:
@media screen and (max-width:620px){.logo{width: 300pxmargin-left: -140px}
}
@media screen and (max-width:1024px) and (min-width:621px) {
.logo{width: 220pxmargin-left: -99px}
.nav li:nth-of-type(2),.nav li:nth-of-type(3){width: 8%}
.nav li:nth-of-type(5),.nav li:nth-of-type(6){width: 12%}
}
css具有媒体响应功能,可以判断屏幕宽度,从而加载设置的css
如下代码,当宽度小于1000px的时候加载css下的css-mobile.css
<link href="css/css-mobile.css" rel="stylesheet" type="text/css" media="screen and (max-width: 1000px)">如下代码,当宽度大于1000px的时候加载css下的css-pc.css
<link href="css/css-pc.css" rel="stylesheet" type="text/css" media="screen and (min-width: 1000px)">