转自:http://www.360doc.com/content/13/0219/12/21412_266515449.shtml
<strong>一、”自适应网页设计”的概念 </strong> <br>
<strong></strong> 2010年,Ethan Marcotte提出了“自适应网页设计”(Responsive Web Design)--这个名词,指可以自动识别屏幕宽度、并做出相应调整的网页设计。<br>
<br>
<strong>二、允许网页宽度自动调整 </strong> <br>
<strong></strong> “自适应网页设计”到底是怎么做到的?其实并不难。 <br>
首先,在网页代码的头部,加入一行viewport元标签。 <br>
<br>
<ol>
<li>
<meta name=”viewport” content=”width=device-width, initial-scale=1″ />
</li>
</ol>
<br>
viewport是网页默认的宽度和高度,上面这行代码的意思是,网页宽度默认等于屏幕宽度(width=device-width),原始缩放比例(initial-scale=1)为1.0,即网页初始大小占屏幕面积的100%。
所有主流浏览器都支持这个设置,包括IE9。对于那些老式浏览器(主要是IE6、7、8),需要使用<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>3-mediaqueries.<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/js">js</a>。 <br>
<br>
<ol>
<li>
<!–[if lt IE 9]>
<script src=”http://<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>3-mediaqueries-<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/js">js</a>.googlecode.com/svn/trunk/<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>3-mediaqueries.<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/js">js</a>”></script>
<![endif]–>
</li>
</ol>
<strong><br>
</strong> <strong>三、不使用绝对宽度 <br>
</strong> 由于网页会根据屏幕宽度调整布局,所以不能使用绝对宽度的布局,也不能使用具有绝对宽度的元素。这一条非常重要。 <br>
具体说,<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>代码不能指定像素宽度: <br>
width:xxx px; <br>
只能指定百分比宽度: <br>
width: xx%; <br>
或者 <br>
width:auto; <br>
<br>
<strong>四、相对大小的字体 </strong> 字体也不能使用绝对大小(px),而只能使用相对大小(em)。 <br>
body {
font: normal 100% Helvetica, Arial, sans-serif;
} <br>
<br>
上面的代码指定,字体大小是页面默认大小的100%,即16像素。 <br>
h1 {
font-size: 1.5em;
} <br>
<br>
然后,h1的大小是默认大小的1.5倍,即24像素(24/16=1.5)。 <br>
small {
font-size: 0.875em;
} <br>
small元素的大小是默认大小的0.875倍,即14像素(14/16=0.875)。 <br>
<br>
<strong>五、流动布局(fluid grid)</strong> <br>
<br>
“流动布局”的含义是,各个区块的位置都是浮动的,不是固定不变的。 <br>
<br>
<ol>
<li>
.main {
float: right;
width: 70%;
}
</li>
<li>
.leftBar {
float: left;
width: 25%;
}
</li>
</ol>
<br>
float的好处是,如果宽度太小,放不下两个元素,后面的元素会自动滚动到前面元素的下方,不会在水平方向overflow(溢出),避免了水平滚动条的出现。
另外,绝对定位(position: absolute)的使用,也要非常小心。 <br>
<br>
<strong>六、选择加载<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a> </strong><br>
“自适应网页设计”的核心,就是<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>3引入的Media Query模块。
它的意思就是,自动探测屏幕宽度,然后加载相应的<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>文件。 <br>
<br>
<ol>
<li>
<link rel=”stylesheet” type=”text/<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>”
media=”screen and (max-device-width: 400px)”
href=”tinyScreen.<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>” />
</li>
</ol>
<br>
上面的代码意思是,如果屏幕宽度小于400像素(max-device-width: 400px),就加载tinyScreen.<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>文件。 <br>
<br>
<ol>
<li>
<link rel=”stylesheet” type=”text/<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>”
media=”screen and (min-width: 400px) and (max-device-width: 600px)”
href=”smallScreen.<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>” />
</li>
</ol>
<br>
如果屏幕宽度在400像素到600像素之间,则加载smallScreen.<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>文件。
除了用html标签加载<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>文件,还可以在现有<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>文件中加载。 <br>
<br>
<ol>
<li>
@import url(“tinyScreen.<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/css">css</a>”) screen and (max-device-width: 400px);
</li>
</ol>
<strong><br>
</strong> <strong>七、<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>的@media规则 </strong> 同一个<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>文件中,也可以根据不同的屏幕分辨率,选择应用不同的<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>规则。 <br>
<br>
<ol>
<li>
@media screen and (max-device-width: 400px) {
.column {
float: none;
width:auto;
}
#sidebar {
display:none;
}
}
</li>
</ol>
<br>
上面的代码意思是,如果屏幕宽度小于400像素,则column块取消浮动(float:none)、宽度自动调节(width:auto),sidebar块不显示(display:none)。 <br>
<br>
<strong>八、图片的自适应(fluid image) </strong><br>
除了布局和文本,”自适应网页设计”还必须实现图片的自动缩放。 <br>
这只要一行<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/CSS">CSS</a>代码: <br>
<br>
<ol>
<li>
img { max-width: 100%;}
</li>
</ol>
<br>
这行代码对于大多数嵌入网页的视频也有效,所以可以写成:
img, object { max-width: 100%;} <br>
老版本的IE不支持max-width,所以只好写成: <br>
img { width: 100%; } <br>
此外,windows平台缩放图片时,可能出现图像失真现象。这时,可以尝试使用IE的专有命令: <br>
<br>
<ol>
<li>
img { -ms-interpolation-mode: bicubic; }
</li>
<li>
或者,Ethan Marcotte的imgSizer.<a href="http://www.diaoyude.cn/index.php/tags/index/tagname/js">js</a>。
</li>
<li>
addLoadEvent(function() {
var imgs = document.getElementById(“content”).getElementsByTagName(“img”);
imgSizer.collate(imgs);
});</li></ol>