html5怎么制作一个响应式网页?

html-css011

html5怎么制作一个响应式网页?,第1张

HTML5 制作响应式网页,首先需要考虑是全平台适配还是只是移动端适配。这里以移动端响应式网站为例,讲述如何制作响应式网页。

1、选定基本设计尺寸,一般以1080为基准。确定响应式web设计的应用场景之后,和美工(或设计师)沟通,之前,一般需要美工出几套主流移动设备屏幕分辨率的设计图,现在,使用流式布局以及rem等可以使用一套设计图,以最常用的移动设备屏幕分辨率为基准。

2、当美工完成设计图之后,前端工程师的工作就开始了。这时你就可以使用PS或是FW进行切图了。一般说来,Fireworks cs6切图更快,但是Fireworks有时会有图片失真的情况发生,所以,有时需要使用PS进行配合,PS有切片工具可以专门用来切图。

具体代码:

(function (doc, win) {

var docEl = doc.documentElement,

resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',

recalc = function () {

var clientWidth = docEl.clientWidth

if (!clientWidth) return

docEl.style.fontSize = 20 * (clientWidth / 320) + 'px'

}

举个实现HTML5响应式表格的实例,仅供参考:

HTML结构如下:

<table id="miyazaki">

<caption>The Films of Miyazaki</caption>

<thead>

<tr><th>Film<th>Year<th>Honor

<tbody>

<tr>

<td data-th="Film">My Neighbor Totoro

<td data-th="Year">1988

<td data-th="Honor">Blue Ribbon Award (Special)

<tr>

<td data-th="Film">Princess Mononoke

<td data-th="Year">1997

<td data-th="Honor">Nebula Award (Best Script)

<tr>

<td data-th="Film">Spirited Away

<td data-th="Year">2001

<td data-th="Honor">Academy Award (Best Animated Feature)

<tr>

<td data-th="Film">Howl’s Moving Castle

<td data-th="Year">2004

<td data-th="Honor">Hollywood Film Festival (Animation OTY)

</table>

注意代码中的data属性,每一个单元格的data属性都与表格的header相对应。

CSS样式

表格基本的CSS样式如下:

table#miyazaki caption {

font-size: 2remcolor: #444

margin: 1rem

background-image: url(miyazaki.png), url(miyazaki2.png)

background-size: contain

background-repeat: no-repeat

background-position: center left, center right

}

table#miyazaki {

border-collapse: collapse

font-family: Agenda-Lightfont-weight: 100

background: #333color: #fff

text-rendering: optimizeLegibility

border-radius: 5px

}

table#miyazaki thead th { font-weight: 600}

table#miyazaki thead th, table#miyazaki tbody td {

padding: .8remfont-size: 1.4rem

}

table#miyazaki tbody td {

padding: .8remfont-size: 1.4rem

color: #444background: #eee

}

table#miyazaki tbody tr:not(:last-child) {

border-top: 1px solid #ddd

border-bottom: 1px solid #ddd

}

下面是响应式表格的CSS代码:

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

table#miyazaki caption { background-image: none}

table#miyazaki thead { display: none}

table#miyazaki tbody td { display: blockpadding: .6rem}

table#miyazaki tbody tr td:first-child { background: #333color: #fff}

table#miyazaki tbody td:before {

content: attr(data-th)font-weight: bold

display: inline-blockwidth: 6rem

}

}

media query代码中隐藏表格的头部单元,并且将每一个单元格的data-th作为标签显示在单元格内容的前面。每一行的第一个单元格都设置了特别的背景色和前景色,使之更为清晰。

扩展

你现在可以缩放浏览器来看看效果,非常不错。但是上面的代码是不可扩展的:要添加一个新行必须手动为每个单元格添加一个data-th属性。要想做到自动化,可以在服务器端实现,如PHP。也可以通过javascript来实现它。

首先,将整个表格都进行简化:

<table id="miyazaki">

<caption>The Films of Hayao Miyazaki</caption>

<thead>

<tr><th>Film<th>Year<th>Honor

<tbody>

<tr>

<td>My Neighbor Totoro

<td>1988

<td>Blue Ribbon Award (Special)

<tr>

<td>Princess Mononoke

<td>1997

<td>Nebula Award (Best Script)

<tr>

<td>Spirited Away

<td>2001

<td>Academy Award (Best Animated Feature)

<tr>

<td>Howl’s Moving Castle

<td>2004

<td>Hollywood Film Festival (Animation OTY)

</table>

然后在文档的底部添加下面的javascript代码:

<script>

var headertext = []

var headers = document.querySelectorAll("#miyazaki th"),

tablerows = document.querySelectorAll("#miyazaki th"),

tablebody = document.querySelector("#miyazaki tbody")

for(var i = 0i <headers.lengthi++) {

var current = headers[i]

headertext.push( current.textContent.replace( /\r?\n|\r/,"") )

}

for (var i = 0, rowrow = tablebody.rows[i]i++) {

for (var j = 0, colcol = row.cells[j]j++) {

col.setAttribute("data-th", headertext[j])

} }

</script>

上面的代码的意思是:获取每一个<th>中的文本内容,然后分别剔除它们的回车和换行符。然后将这些文本分别添加到适当的单元格的data属性上,添加的规则与CSS样式的规则相一致。(使用setAttribute要比dataset要好,后者只有在IE 11中得到支持。)

1.调整视口

代码实例:

<!DOCTYPE html>

<head>

    <meta charset="UTF-8" />

    <title>布局之路-移动端开发实例</title>

    <meta name="viewport" content="width=device-width,user-scalable = no" />

    <link rel="stylesheet" type="text/css" href="css/reset.css" />

</head>

<body>

    <div class="wrap"></div>

</body>

</html>

代码解析:由于使用不同设备打开网页时,宽度均有所不同,所以不能讲视口设置为固定值,应当为width=device-width,即将视口设置为当前设备的宽度。

2.确定设计图的最小字体

浏览器(部分)能够显示的最小字体未12px,当移动端页面宽度为320px时,要保证最小字体为12px,那么在1080px的设计图中,最小字体应当为42px。

代码实例:

    <style type="text/css">

        html {

            font-size: 42px

        }

    </style> 3.浮动布局

各个区块都是浮动的,不是固定不变的。为了能自适应各个窗口。

代码实例:

.main {

  float: left

  width: 70%

}

.box {

    float: left

    width: 60.93%

    font-size: 1.71rem

    text-align: center

    line-height: 4.64rem

}

float浮动的好处就是,如果宽度不够放置下这个元素,元素会自动滚动到下方。

4.通过媒介查询,为不同设备加载相应样式

有条件应用样式:

 <style>

    @media all and(min-width:500px){ ... }

    @media (orientation){ ... }

</style>

代码解析:

第一行媒体查询代码指的是:为宽度大于等于500px的设备设置样式。

第二行媒体查询代码指的是:为纵屏状态(可见区域大于或等于宽度)下的移动端设备设置样式。

有条件的加载样式表:

<head>

    <link rel="stylesheet" href="wide.css" media="screen and(min-width:1024)" />

    <link rel="stylesheet" href="mobile.css" media="screen and(max-width:320)" />

</head>

代码解析:

第一行媒体查询代码指的是:为宽度大于等于1024px的设备,加载wide.css文件。

第二行媒体查询代码指的是:为宽度小于等于320px的设备,加载mobile.css文件。

5.使用百分比和rem替换px

除了布局和文本,"自适应网页设计"还必须实现图片的自动缩放。

代码效果对比:

/*使用固定像素*/

.box {

    float: left

    width: 658px

    font-size: 72px

    text-align: center

    line-height: 195px

}

/*使用百分比和rem*/

.box {

    float: left

    width: 60.93%

    font-size: 1.71rem

    text-align: center

    line-height: 4.64rem

}

代码解析:

水平方向的值,将具体像素调整为百分比。百分比的计算是根据父级的内容区宽度进行计算的。

例如,父级宽度为1080px, 子级元素为197px,那么子元素转换为百分比为:197/1080*100%=18.24%。需要注意的是百分比根据父级计算,当标签结构级别不同时,计算公式中的“分母”也有所不同,在开发时这个地方很容易出现问题,请务必注意。

垂直方向的值,将具体像素调整为rem,与水平方向相比,垂直方向的计算就比较简单。例如,行高为195px,HTML标签当前的字体大小为42px,将行高转换为rem单位,即195/42= 4. 64rem。