如何制作自适应网页

2025-10-30 11:31:24

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

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

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

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

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

width:xxx px;

只能指定百分比宽度:

width: xx%;

或者

width:auto;

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

例如:

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

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

4、流动布局(fluid grid)

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

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

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

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

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文件。

1、<!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>

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>)

</div>

<div id="content">

<h2>Content</h2>

text

text

text

text

text

text

text

text

text

text

</div>

<div id="sidebar">

<h3>Sidebar</h3>

textg

fgs

fgsg

dg

dfgfd

是否感到反感

sgrtg

分公司

sgf

text

</div>

<div id="footer">

<h4>Footer</h4>

</div>

</div>

</body>

</html>

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢