HTML5几个设计和修改的页面范例分享


Posted in HTML / CSS onSeptember 29, 2015

要了解和熟悉 HTML5 中的新的语义元素,最好的方式就是拿一经典的 HTML 文档作例子,然后把 HTML5 的一些新鲜营养充实进入。如下就是我们要改造的页面,该页面很简单,只包含一篇文章。

ApocalypsePage_Original.html,这是一个格式非常规范的页面,所有的样式均来自于外部样式表。

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>  
  2. <html lang="zh-CN">  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>Apocalypse Now</title>  
  6.   <link rel="stylesheet" href="ApocalypsePage_Original.css">  
  7. </head>  
  8.   
  9. <body>  
  10. <div class="Header">  
  11.   <h1>How the World Could End</h1>  
  12.   <p class="Teaser">Scenarios that spell the end of life as we know</p>  
  13.   <p class="Byline">by Ray N. Carnation</p>  
  14. </div><!-- end Header -->  
  15.   
  16. <div class="Content">  
  17.   <p><span class="LeadIn">Right now</span>, you're probably feeling pretty good. After all, life in the developed world is comfortable<span class="style1"></span>probably more comfortable than it's been for the average human being throughout all of recorded history.</p>  
  18.   <p>But don't get too smug. There's still plenty of horrific ways it could all fall apart. In this article, you'll learn about a few of our favorites.</p>  
  19.      
  20.   <h2>Mayan Doomsday</h2>  
  21.   <p>Skeptics suggest that the Mayan calendar simply rolls to a new 5,126-year era after 2012, and doesn't actually predict a life-ending apocalypse. But given that the long-dead Mayans were wrong about virtually everything else, why should we trust them on this?</p>  
  22.      
  23.   <h2>Robot Takeover</h2>  
  24.   <p>Not quite as frightening as a Vampire Takeover or Living-Dead Takeover, a robot rebellion is still a disquieting thought. We are already outnumbered by our technological gadgets, and even Bill Gates fears the day his Japanese robot slave turns him over by the ankles and asks (in a suitably robotic voice) "Who's your daddy now?"</p>  
  25.      
  26.   <h2>Unexplained Singularity</h2>  
  27.   <p>We don't know how the universe started, so we can't be sure it won't just end, maybe today, and maybe with nothing more exciting than a puff of anti-matter and a slight fizzing noise.</p>  
  28.      
  29.   <h2>Runaway Climate Change</h2>  
  30.   <p>Dismissed by some, Al Gore's prophecy of doom may still come true. If it does, we may have to contend with vicious storms, widespread food shortages, and surly air conditioning repairmen.</p>  
  31.      
  32.   <h2>Global Epidemic</h2>  
  33.   <p>Some time in the future, a lethal virus could strike. Predictions differ about the source of the disease, but candidates include monkeys in the African jungle, bioterrorists, birds and pigs with the flu, warriors from the future, an alien race, hospitals that use too many antibiotics, vampires, the CIA, and unwashed brussel sprouts. Whatever the source, it's clearly bad news.</p>  
  34.   
  35. </div><!-- end Content -->  
  36.   
  37. <div class="Footer">  
  38.   <p class="Disclaimer">These apocalyptic predictions do not reflect the views of the author.</p>  
  39.   <p>  
  40.     <a href="AboutUs.html">About Us</a>  
  41.     <a href="Disclaimer.html">Disclaimer</a>  
  42.     <a href="ContactUs.html">Contact Us</a>  
  43.   </p>  
  44.   <p>Copyright © 2014</p>  
  45. </div><!-- end Footer -->  
  46. </body>  
  47. </html>  

在不增加任何 CSS 样式表之前,效果如下:
HTML5几个设计和修改的页面范例分享
上面通过三个 <div> 将页面分成了三个部分,顶部的页眉,中部的内容和底部的页脚。

这个例子中的样式表很简单,整个页面最大宽度设置为 800 像素,避免文本在宽屏显示器上显示过长。页眉位于一个带有蓝色边框的盒子中,内容区的两侧都增加了内边距,而页脚在整个页面的底部居中。

ApocalypsePage_Original.css样式文件内容如下:

XML/HTML Code复制内容到剪贴板
  1. @charset "utf-8";   
  2. /* CSS Document */   
  3. body{   
  4.   /*font-family 要使用安全字体,按照先特殊后一般的原则,   
  5.   先给出你想要的字体,然后是保险一些的字体,   
  6.   最后以 sans-serif 字体结尾*/   
  7.   font-family: "Lucida sans Unicode", "Lucida Grande", Geneva, sans-serif;   
  8.   max-width: 800px; /*最大宽度不超过 800 像素*/   
  9. }   
  10. /*页面顶部的标题区样式*/   
  11. .Header {   
  12.   background-color: #7695FE; /*可接受任何颜色值*/   
  13.   border: thin #336699 solid; /*多合一的 border 属性*/   
  14.   padding: 10px; /* 10像素的内边距,边框与内容之间的距离*/   
  15.   margin: 10px; /* 10像素的外边距,边框与周围元素之间的距离*/   
  16.   text-align: center; /*头部文本居中*/   
  17. }   
  18. /*页眉中标题<h1>样式*/   
  19. .Header h1{   
  20.   margin: 0px;   
  21.   color: white;   
  22.   font-size: xx-large; /*精确控制可以用像素或者em单位*/   
  23. }   
  24. /*页眉中子标题样式*/   
  25. .Header .Teaser{   
  26.   margin: 0px;   
  27.   font-weight: bold;   
  28. }   
  29. /*页眉中署名行样式*/   
  30. .Header .Byline{   
  31.   font-style: italic;   
  32.   font-size: small;   
  33.   margin: 0px;   
  34. }   
  35. .Content{   
  36.   font-size: medium;   
  37.   font-family: Cambria, Cochin, Georgia, "Times New Roman", Times, serif;   
  38.   /*左右内边距最大*/   
  39.   padding-top: 20px;   
  40.   padding-right: 50px;   
  41.   padding-bottom: 5px;   
  42.   padding-left: 50px;   
  43.   line-height: 120%; /*相邻两个文本行之间的距离*/   
  44. }   
  45. .Content .LeadIn{   
  46.   font-weight: bold;   
  47.   font-size: large;   
  48.   font-variant: small-caps;   
  49. }   
  50. .Content .h2{   
  51.   color: #24486C;   
  52.   margin-bottom: 2px;   
  53.   font-size: medium;   
  54. }   
  55. .Content p{   
  56.   margin-top: 0px;   
  57. }   
  58. .Footer{   
  59.   text-align: center;   
  60.   font-size: x-small;   
  61. }   
  62. .Footer .Disclaimer{   
  63.   font-style: italic;   
  64. }   
  65. .Footer p{   
  66.   margin: 3px;   
  67. }  

这样我们的样式表就弯沉过了,现在去看看结果会怎样呢?如下图:
HTML5几个设计和修改的页面范例分享
使用 HTML5 来构造页面

<div> 目前仍旧是 Web 设计的必备元素,它是一个直观、多用途的容器,可以通过它为页面中的任何区块应用样式。但 <div> 的问题在于,它本身不反映与页面相关的任何信息。

要通过 HTML5 改进这种情况,可以把 <div> 替换成更具有描述性语义的元素。

ApocalypsePage_Revised.html中已经将 class 属性为 Header 和 Footer 两个 <div> 替换为 <header> 和 <footer>, 部分代码如下:

XML/HTML Code复制内容到剪贴板
  1. <header>  
  2.   <h1>How the World Could End</h1>  
  3.   <p class="Teaser">Scenarios that spell the end of life as we know</p>  
  4.   <p class="Byline">by Ray N. Carnation</p>  
  5. </header>  
  6. ...   
  7. <footer>  
  8.   <p class="Disclaimer">These apocalyptic predictions do not reflect the views of the author.</p>  
  9.   <p>  
  10.     <a href="AboutUs.html">About Us</a>  
  11. ...   
  12.   </p>  
  13.   <p>Copyright © 2014</p>  
  14. </footer>  

当然,对应的 ApocalypsePage_Revised.css 文件也需要进行修改,将其中的 .Header 和 .Footer 替换为 header 和 footer 。部分代码如下:

XML/HTML Code复制内容到剪贴板
  1. /*页面顶部的标题区样式*/   
  2. header {   
  3.   background-color: #7695FE; /*可接受任何颜色值*/   
  4.   border: thin #336699 solid; /*多合一的 border 属性*/   
  5.   padding: 10px; /* 10像素的内边距,边框与内容之间的距离*/   
  6.   margin: 10px; /* 10像素的外边距,边框与周围元素之间的距离*/   
  7.   text-align: center; /*头部文本居中*/   
  8. }   
  9. /*页眉中标题<h1>样式*/   
  10. header h1{   
  11.   margin: 0px;   
  12.   color: white;   
  13.   font-size: xx-large; /*精确控制可以用像素或者em单位*/   
  14. }  

最后还有一个元素需要用在示例文件中,就是 <article> 元素,表示一个完整的、自成一体的内容。

<ariticle>元素应该包含新闻报道或文章的内容,包括标题、署名和正文。因此添加了 <article> 元素后的结构如下:

XML/HTML Code复制内容到剪贴板
  1. <article>  
  2.   <header>  
  3.   <h1>How the World Could End</h1>  
  4.   <p class="Teaser">Scenarios that spell the end of life as we know</p>  
  5.   <p class="Byline">by Ray N. Carnation</p>  
  6.   </header>  
  7.   <div class="Content">  
  8.   <p><span class="LeadIn">Right now</span>, you're probably feeling pretty good. After all, life in the developed world is comfortable<span class="style1"></span>probably more comfortable than it's been for the average human being throughout all of recorded history.</p>  
  9. ...   
  10.   </div><!-- end Content -->  
  11. </article>  

重新设计后,页面结构如下:
HTML5几个设计和修改的页面范例分享

用 <figure> 添加插图

很多页面都是包含图片的。但是,插图 (figure) 与图片的概念还不完全一样。插图虽然独立于文本,但是文本中会提到它。

一般来说插图应该是浮动的,还会有浮动图题。下面是在文章中添加插图的 HTML 标记,在正文的第一段和第二段之间的位置,部分代码如下:

XML/HTML Code复制内容到剪贴板
  1. ...   
  2. <div class="Content">  
  3. <p><span class="LeadIn">Right now</span>, you're ...</p>  
  4. <div class="FloatFigure">  
  5. <img src="human_skull.jpg" alt="Human skull">  
  6. <p>Will you be the last person standing if one of these apocalyptic   
  7. scenarios plays out?</p>  
  8. </div>  
  9. <p>But don't get too smug. There's...</p>  
  10. ...  

相应的 样式表规则如下:

XML/HTML Code复制内容到剪贴板
  1. .FloatFigure{   
  2.   float: left;   
  3.   margin: 0px 20px 0px 0px;   
  4. }   
  5. .FloatFigure p{   
  6.   max-width: 300px;   
  7.   font-size: small;   
  8.   font-style: italic;   
  9.   margin-bottom: 5px;   
  10. }  

下图展示了这个示例的外观,插图恰好在第一段文本之后,浮动在后面文本的左侧,图题的文本的宽度我们限制住了,让图题显示很充实、很优雅。
HTML5几个设计和修改的页面范例分享

HTML5 中提供了一个 <figure> 元素,图题可以放在 <figure> 中的 <figcaption> 元素里,经过改造,代码如下:

XML/HTML Code复制内容到剪贴板
  1. <figure class="FloatFigure">  
  2.   <img src="human_skull.jpg" alt="Human skull">  
  3.   <figcaption>Will you be the last person standing if one of these apocalyptic   
  4.    scenarios plays out?</figcaption>  
  5. </figure>  

当然样式表中的选择符,相应修改一下即可。

XML/HTML Code复制内容到剪贴板
  1. .FloatFigure{   
  2.   float: left;   
  3.   margin: 0px 20px 0px 0px;   
  4. }   
  5. .FloatFigure figcaption{   
  6.   max-width: 300px;   
  7.   font-size: small;   
  8.   font-style: italic;   
  9.   margin-bottom: 5px;   
  10. }  

最后还有就是 <img> 元素中的 alt 属性可以删除掉,因为图题中包含了图片的完整说明。

用 <aside> 添加附注

新的 <aside> 元素表示与它周围的文本没有密切关系的内容。可以通过它介绍另一个相关的话题,或者对主文档中提出的某个观点进行解释。还可以用来放置广告、相关内容链接。

下面的示例中将用作醒目引文(pull quote),使用 <div> 元素可以创造这种效果,但是用 <aside> 元素让标记更有意义:
HTML5几个设计和修改的页面范例分享
部分代码如下:

XML/HTML Code复制内容到剪贴板
  1.   <p>... (in a suitably robotic voice) "Who's your daddy now?"</p>  
  2.   
  3. <aside class="PullQuote">  
  4.   <img src="quotes_start.png" alt="Quote">  
  5.   We don't know how the universe started, so we can't be sure it won't just end, maybe today.   
  6.   <img src="quotes_end.png" alt="End quote">  
  7. </aside>  
  8.   
  9. <h2>Unexplained Singularity</h2>  

对应的样式表内容如下:

XML/HTML Code复制内容到剪贴板
  1. .PullQuote{   
  2.   float: right;   
  3.   max-width: 300px;   
  4.   border-top: thin black solid;   
  5.   border-bottom: thick black solid;   
  6.   font-size: 30px;   
  7.   line-height: 130%;   
  8.   font-style: italic;   
  9.   padding-top: 5px;   
  10.   padding-bottom: 5px;   
  11.   margin-left: 15px;   
  12.   margin-bottom: 10px;   
  13. }   
  14. .PullQuote img{   
  15.   vertical-align: bottom;   
  16. }  
HTML / CSS 相关文章推荐
CSS3 绘制BMW logo实的现代码
Apr 25 HTML / CSS
前端隐藏出边界内容的实现方法
Apr 14 HTML / CSS
HTML5 WebGL 实现民航客机飞行监控系统
Jul 25 HTML / CSS
HTML5中的新元素介绍
Oct 17 HTML / CSS
H5最强接口之canvas实现动态图形功能
May 31 HTML / CSS
canvas实现手机的手势解锁的步骤详细
Mar 16 HTML / CSS
h5移动端调用支付宝、微信支付的实现
Jun 08 HTML / CSS
六种css3实现的边框过渡效果
Apr 22 HTML / CSS
HTML+css盒子模型案例(圆,半圆等)“border-radius” 简单易上手
May 10 HTML / CSS
Unicode中的CJK(中日韩统一表意文字)字符小结
Dec 06 HTML / CSS
css filter和getUserMedia的联合使用
Feb 24 HTML / CSS
使用CSS实现音波加载效果
May 07 HTML / CSS
简单的HTML5初步入门教程
Sep 29 #HTML / CSS
基于html5 DeviceOrientation 实现微信摇一摇功能
Sep 25 #HTML / CSS
简单介绍HTML5中audio标签的使用
Sep 24 #HTML / CSS
利用HTML5实现使用按钮控制背景音乐开关
Sep 21 #HTML / CSS
深入解析HTML5的IndexedDB索引数据库
Sep 14 #HTML / CSS
使用HTML5的表单验证的简单示例
Sep 09 #HTML / CSS
详解HTML5中的manifest缓存使用
Sep 09 #HTML / CSS
You might like
PHP开发环境配置(MySQL数据库安装图文教程)
2010/04/28 PHP
php截取字符串函数substr,iconv_substr,mb_substr示例以及优劣分析
2014/06/10 PHP
php获取错误信息的方法
2015/07/17 PHP
微信公众号开发之获取位置信息php代码
2018/06/13 PHP
css把超出的部分显示为省略号的方法兼容火狐
2008/07/23 Javascript
javascript 学习之旅 (3)
2009/02/05 Javascript
HTA版JSMin(省略修饰语若干)基于javascript语言编写
2009/12/24 Javascript
jQuery判断当前点击的是第几个li的代码
2014/09/26 Javascript
js中hash和ico的关联分析
2015/02/05 Javascript
介绍一个简单的JavaScript类框架
2015/06/24 Javascript
js实现人民币大写金额形式转换
2016/04/27 Javascript
JavaScript排序算法动画演示效果的实现方法
2016/10/18 Javascript
基于JavaScript实现自动更新倒计时效果
2016/12/19 Javascript
jquery uploadify如何取消已上传成功文件
2017/02/08 Javascript
Fundebug支持监控微信小程序HTTP请求错误的方法
2019/02/21 Javascript
vue中使用mxgraph的方法实例代码详解
2019/05/17 Javascript
vue实现二级导航栏效果
2019/10/19 Javascript
js实现消灭星星(web简易版)
2020/03/24 Javascript
javaScript实现一个队列的方法
2020/07/14 Javascript
解决vue项目获取dom元素宽高总是不准确问题
2020/07/29 Javascript
浅析Python3爬虫登录模拟
2018/02/07 Python
python网络爬虫学习笔记(1)
2018/04/09 Python
Flask框架学习笔记之使用Flask实现表单开发详解
2019/08/12 Python
详解Python中openpyxl模块基本用法
2021/02/23 Python
米兰网婚纱礼服法国网上商店:Milanoo法国
2016/08/20 全球购物
2014信息公开实施方案
2014/02/22 职场文书
高中军训感想800字
2014/02/23 职场文书
法人代表身份证明书及授权委托书
2014/09/16 职场文书
2014年职称评定工作总结
2014/11/26 职场文书
三峡人家导游词
2015/01/31 职场文书
财务部岗位职责
2015/02/03 职场文书
社区活动总结
2015/02/04 职场文书
研究生给导师的自荐信
2015/03/06 职场文书
食品安全责任书范本
2015/05/09 职场文书
vue中data改变后让视图同步更新的方法
2021/03/29 Vue.js
告别网页搜索!教你用python实现一款属于自己的翻译词典软件
2021/06/03 Python