首页 > 技术文章 > CSS响应式:根据分辨率加载不同CSS的几个方法

yuan9580 2019-09-12 17:20 原文

一般情况下,根据分辨率加载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)">
//其中 media 是媒体查询的范围,当最大宽度是1200,这里就是手机平板一下的尺寸 加载手机css ,反之电脑css
<link rel="stylesheet" type="text/css" href="../css/m_wuqinglan.css" media="screen and (max-width:1200px)"/> <link rel="stylesheet" type="text/css" href="../css/pc_wuqinglan.css" media="screen and (min-width:1201px)"/>

只有一个css文件情况下,根据分辨率调整css样式,示例:

@media screen and  (max-width:620px){
    .logo{width: 300px;margin-left: -140px;}
}
 
@media screen and  (max-width:1024px) and (min-width:621px) {
    .logo{width: 220px;margin-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%;}
}

 

推荐阅读