有戏

 找回密码
 立即注册
简历下载
搜索
热搜: 活动 交友 discuz
查看: 618|回复: 0

如何通过css的media属性,适配不同分辨率的终端设备?

[复制链接]

2

主题

2

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
14390
发表于 2016-4-28 18:20:01 | 显示全部楼层 |阅读模式
如何通过css的media属性,适配不同分辨率的终端设备,示例如下:

  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>首页</title>
  5.     <meta name="viewport" content="width=device-width, initial-scale=1">
  6.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  7.     <meta http-equiv="description" content="this is my page">
  8.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  9.     <style>
  10.     @media (max-width:768px) {
  11.         #div1 {
  12.             width: 400px;
  13.             margin: 0 auto;
  14.             background:url(‘../img/1.JPG‘);
  15.         }
  16.         #img2,#img3 {
  17.             width: 100px;
  18.         }
  19.     }
  20.     @media (min-width:768px) {
  21.         #div1 {
  22.             width: 600px;
  23.             margin: 0 auto;
  24.             background:url(‘../img/1.JPG‘);
  25.         }
  26.         #img2,#img3 {
  27.             width: 200px;
  28.         }
  29.     }
  30.     @media (min-width:992px) {
  31.        #div1 {
  32.             width: 800px;
  33.             margin: 0 auto;
  34.             background:url(‘../img/1.JPG‘);
  35.         }
  36.         #img2,#img3 {
  37.             width: 300px;
  38.         }
  39.     }

  40.     @media (min-width:1200px) {
  41.         #div1 {
  42.             width: 1000px;
  43.             margin: 0 auto;
  44.             background:url(‘../img/1.JPG‘);
  45.         }
  46.         #img2,#img3 {
  47.             width: 400px;
  48.         }
  49.     }
  50.     </style>
  51.   </head>
  52.   <body>
  53.     <div id="div1">
  54.         <img id="img2" alt="img2" src="../img/2.JPG">
  55.         <img id="img3" alt="img3" src="../img/3.JPG">
  56.     </div>
  57.   </body>
  58. </html>
复制代码
当然,也可以根据终端分辨率的变化去引用不同的css文件,示例如下:

  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>首页</title>
  5.     <meta name="viewport" content="width=device-width, initial-scale=1">
  6.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  7.     <meta http-equiv="description" content="this is my page">
  8.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  9.     <link rel="stylesheet" media="(max-width: 768px)" href="../css/max-768px.css">
  10.     <link rel="stylesheet" media="(min-width: 768px)" href="../css/min-768px.css">
  11.     <link rel="stylesheet" media="(min-width: 992px)" href="../css/min-992px.css">
  12.     <link rel="stylesheet" media="(min-width: 1200px)" href="../css/min-1200px.css">
  13.   </head>
  14.   <body>
  15.     <div id="div1">
  16.         <img id="img2" alt="img2" src="../img/2.JPG">
  17.         <img id="img3" alt="img3" src="../img/3.JPG">
  18.     </div>
  19.   </body>
  20. </html>
复制代码
注意: 只需要将第一个文件里面的ccs写入四个css文件。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|有戏 粤ICP备2020111303号

GMT+8, 2025-12-6 12:26 , Processed in 0.072899 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表