如何制作自适应网页(转)

  随着网络的快熟发展,越来越多的人使用手机上网。移动设备正超过桌面设备,成为访问互联网的最常见终端。于是,网页设计师不得不面对一个难题:如何才能在 不同大小的设备上呈现同样的网页?手机的屏幕比较小,宽度通常在600像素以下;PC的屏幕宽度,一般都在1000像素以上,有的还达到了2000像素。 同样的内容,要在大小迥异的屏幕上,都呈现出满意的效果,并不是一件容易的事。很多网站的做法是对不同终端设计多个网页,但这样会有很多维护的问题,在这 里我们可以设计一个简单的盒子,这个盒子可以识别不同的终端而显示不同的效果

工具/原料

  • dm网页设计软件

  • ps图像处理软件

方法/步骤

  1. 1

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

    <meta name="viewport" content="width=device-width,initial-scale=1" />

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

  2. 2

    由于网页会根据屏幕宽度调整布局,所以不能使用绝对宽度的布局,也不能使用具有绝对宽度的元素。对图像来说也是这样。

    具体说,CSS代码不能指定像素宽度:

    width:xxx px;

    只能指定百分比宽度:

    width: xx%;

    或者

    width:auto;

  3. 3

    字体也不能使用绝对大小(px),而只能使用相对大小(em)。

    例如:

    body {font: normal 100% Helvetica, Arial,sans-serif;}

    上面的代码指定,字体大小是页面默认大小的100%,即16像素。

  4. 4

    流动布局(fluid grid)

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

    .main {float: right;width: 70%; }

    .leftBar {float: left;width: 25%;}

    float的好处是,如果宽度太小,放不下两个元素,后面的元素会自动滚动到前面元素的下方,不会在水平方向overflow(溢出),避免了水平滚动条的出现。

  5. 5

    "自适应网页设计"的核心,就是CSS3引入的MediaQuery模块。

    它的意思就是,自动探测屏幕宽度,然后加载相应的CSS文件。

    <link rel="stylesheet" type="text/css"media="screen and (max-device-width:400px)"href="tinyScreen.css" />

    上面的代码意思是,如果屏幕宽度小于400像素(max-device-width: 400px),就加载tinyScreen.css文件。

    <link rel="stylesheet" type="text/css"media="screen and (min-width: 400px)and (max-device-width: 600px)"href="smallScreen.css" />

    如果屏幕宽度在400像素到600像素之间,则加载smallScreen.css文件。

    END

参考下面的例子——我称它为“盒子”

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<!-- viewport meta to reset iPhone inital scale -->

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Demo: Responsive Design in 3 Steps</title>

<!-- css3-mediaqueries.js for IE8 or older -->

<!--[if lt IE 9]>

<script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>

<![endif]-->

<style type="text/css">

body {

font: 1em/150% Arial, Helvetica, sans-serif;

}

a {

color: #669;

text-decoration: none;

}

a:hover {

text-decoration: underline;

}

h1 {

font: bold 36px/100% Arial, Helvetica, sans-serif;

}

/************************************************************************************

STRUCTURE

*************************************************************************************/

#pagewrap {

padding: 5px;

width: 960px;

margin: 20px auto;

}

#header {

height: 180px;

}

#content {

width: 600px;

float: left;

}

#sidebar {

width: 300px;

float: right;

}

#footer {

clear: both;

}

/************************************************************************************

MEDIA QUERIES

*************************************************************************************/

/* for 980px or less */

@media screen and (max-width: 980px) {

 

#pagewrap {

width: 94%;

}

#content {

width: 65%;

}

#sidebar {

width: 30%;

}

}

/* for 700px or less */

@media screen and (max-width: 700px) {

#content {

width: auto;

float: none;

}

#sidebar {

width: auto;

float: none;

}

}

/* for 480px or less */

@media screen and (max-width: 480px) {

#header {

height: auto;

}

h1 {

font-size: 24px;

}

#sidebar {

display: none;

}

}

/* border & guideline (you can ignore these) */

#content {

background: #f8f8f8;

}

#sidebar {

background: #f0efef;

}

#header, #content, #sidebar {

margin-bottom: 5px;

}

#pagewrap, #header, #content, #sidebar, #footer {

border: solid 1px #ccc;

}

</style>

</head>

<body>

<div id="pagewrap">

<div id="header">

<h1>Header</h1>

<p>Tutorial by <a href="http://webdesignerwall.com">Web Designer Wall</a> (read <a href="http://webdesignerwall.com/tutorials/responsive-design-in-3-steps">related article</a>)</p>

</div>

<div id="content">

<h2>Content</h2>

<p>text</p>

<p>text</p>

<p>text</p>

<p>text</p>

<p>text</p>

<p>text</p>

<p>text</p>

<p>text</p>

<p>text</p>

<p>text</p>

</div>

<div id="sidebar">

<h3>Sidebar</h3>

<p>textg</p>

<p>fgs</p>

<p>fgsg</p>

<p>dg</p>

<p>dfgfd</p>

<p>测试测试测试</p>

<p>sgrtg</p>

<p>测试</p>

<p>sgf</p>

<p>text</p>

</div>

 

<div id="footer">

<h4>Footer</h4>

</div>

</div>

</body>

</html>

posted on 2014-07-27 11:58  混元真人  阅读(202)  评论(0编辑  收藏  举报