css样式自适应分辨率

html-css013

css样式自适应分辨率,第1张

高度和宽度尽量使用百分百,像素px换成em、rem这种,网页会根据大小来自适应,要想使用效果好,就需要根据不同分辨率来设置层的高宽、字体大小,设置几套样式来应用

@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)">