首页 > 编程知识 正文

网页设计自适应布局,html 自适应

时间:2023-05-03 16:02:48 阅读:219108 作者:429

第一步:在网页代码的头部,加入一行viewport元标签

<meta name="viewport" content="width=device-width, initial-scale=1" />
viewport是网页默认的宽度和高度,
上面这行代码的意思是:网页宽度默认等于屏幕宽度(width=device-width),
原始缩放比例(initial-scale=1)为1.0,即网页初始大小占屏幕面积的100%。


所有主流浏览器都支持这个设置,包括IE9。对于那些老式浏览器(主要是IE6、7、8),需要使用css3-mediaqueries.js

<!--[if lt IE 9]>    <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->



第二步:(注意)不使用绝对宽度,字体大小

width:auto; / width:XX%;



第三步:(注意)字体大小

字体大小是页面默认大小的100%,即16像素
字体不要使用绝对大小"PX",要使用相对大小“REM”

html{font-size:62.5%;}body {font:normal 100% Arial,sans-serif;font-size:14px; font-size:1.4rem; }


第四步:流动布局

"流动布局"的含义是,各个区块的位置都是浮动的,不是固定不变的

.left{ width:30%; float:left}.right{ width:70%; float:right;}其好处是,如果宽度太小,放不下两个元素,后面的元素会自动滚动到前面元素的下方,不会在水平方向overflow(溢出),避免了水平滚动条的出现


第五步:选择加载CSS

"自适应网页设计"的核心,就是CSS3引入的Media Query模块。自动探测屏幕宽度,然后加载相应的CSS文件

<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 600px)"rel="external nofollow" href="style/css/css600.css" />上面的代码意思是,如果屏幕宽度小于600像素(max-device-width: 600px),就加载css600.css文件。

如果屏幕宽度在600像素到980像素之间,则加载css600-980.css文件

<link rel="stylesheet" type="text/css" media="screen and (min-width: 600px) and (max-device-width: 980px)"rel="external nofollow" href="css600-980.css" />
另有(不建议使用):除了用html标签加载CSS文件,还可以在现有CSS文件中加载

@import url("css600.css") screen and (max-device-width: 600px);



第六步:CSS的@media规则

@media screen and (max-device-width: 400px) { .left{ float:none;} }当屏幕小于400时,left取消了浮动



第七步:图片的自适应

"自适应网页设计"还必须实现图片的自动缩放。

img, object {max-width: 100%;}
老版本的IE不支持max-width,所以只好写成:

img {width: 100%;}
windows平台缩放图片时,可能出现图像失真现象。这时,可以尝试使用IE的专有命令

img { width:100%; -ms-interpolation-mode: bicubic;}
或使用js--imgSizer.js

addLoadEvent(function() {    var imgs = document.getElementById("content").getElementsByTagName("img");    imgSizer.collate(imgs);  });

注:如有条件的话,最好还是根据不同大小的屏幕,加载不同分辨率的图片


简易式操作:

<style type="text/css">img{ max-width:100%;}video{ max-width:100%; height:auto;}header ul li{ float:left; list-style:none; list-style-type:none; margin-right:10px;}header select{display:none;}@media (max-width:960px){header ul{ display:none;}header select{ display:inline-block;}}</style><body><header><ul> <li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" class="active">Home</a></li> <li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#">AAA</a></li> <li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#">BBB</a></li> <li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#">CCC</a></li> <li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#">DDD</a></li> </ul> <select> <option class="selected"><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#">Home</a></option> <option value="/AAA">AAA</option> <option value="/BBB">BBB</option> <option value="/CCC">CCC</option> <option value="/DDD">DDD</option> </select></header></body>


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。