创建简单的响应式HTML5模版

译文
开发 前端
HTML5目前发展势头良好,已经逐渐得到大部分浏览器不同程度的支持。许多web开发者也已经学习到了不少关于HTML 5的基础知识并开始试图使用HTML 5制作网页。

HTML5目前发展势头良好,已经逐渐得到大部分浏览器不同程度的支持。许多web开发者也已经学习到了不少关于HTML 5的基础知识并开始试图使用HTML 5制作网页。与此同时,目前基于响应式的网页设计理念也得到了广泛的认同,开发者在开发基于HTML 5的网页时,如果能创建响应式的页面,则会增色不少,特别是能适配各类移动终端。在本文中,读者将学习到如何创建一个简单的响应式HTML 5模版。本文的读者需要有一点HTML 5的基础知识。


   
   

创建良好的HTML 5模版的特征有:

  • 新的特性应该只是基于HTMLCSSDOMJavscript
  • 减少使用外部插件(如Flash
  • 良好的容错设计
  • 使用更多的标签而不是太多的脚本
  • HTML 5应该是和设备无关的
  • 开发过程应该是可视化的

在本文中,使用Adobe Macromedia Dreamweaver进行开发

步骤1 创建空白的HTML 5模版

首先,我们创建一个空白的模版,代码很简单,如下所示:

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4. <title></title> 
  5. </head> 
  6.  
  7. <body> 
  8. </body> 
  9.  
  10. </html> 

步骤2  增加HTML 5新标签

HTML 5中新增加了不少标签,如:

article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section

在页面模版中,我们需要确保每个区域都能正确地对齐,因此需要使用HEADER NAVIGATION CONTENT SIDEBARFooter这些标签。代码如下所示:

  1. <!DOCTYPE HTML> 
  2. <html> 
  3. <head> 
  4. <title></title> 
  5. </head> 
  6.  
  7. <body> 
  8. <div id="wrapper"> 
  9.  
  10. <!—开始区域 --> 
  11. <header></header> 
  12. <nav></nav> 
  13. <section class="content"></section> 
  14. <aside class="sidebar"></aside> 
  15. <footer></footer> 
  16. <!—结束区域--> 
  17.  
  18. </div> 
  19. </body> 
  20.  
  21. </html> 

读者可能留意到这里使用的div id=”wrapper”,这个是稍候用来做meida query的时候调整全局CSS样式调整用的
步骤3  往HTML 5标签中增加代码
a) 首先往标题中增加如下代码:

  1. <header> 
  2. <hgroup> 
  3.  <h1 class="site-title"><a href="#">Simple HTML5 Template</a></h1></hgroup> 
  4. </header> 

b) <nav>导航标签中添加如下代码,这样很方便地构件了一个简单的页面分类导航:

  1. <nav> 
  2.  
  3. <ul> 
  4.  
  5. <li><a href="#">Home</a></li> 
  6.  
  7. <li><a href="#">About</a></li> 
  8.  
  9. <li><a href="#">Parent Page</a> 
  10.  
  11. <ul> 
  12.  
  13. <li><a href="#">Child One</a></li> 
  14.  
  15. <li><a href="#">Child Two with child</a> 
  16.  
  17. <ul> 
  18.  
  19. <li><a href="#">Child One</a></li> 
  20.  
  21. <li><a href="#">Child Two</a></li> 
  22.  
  23. <li><a href="#">Child Three</a></li> 
  24.  
  25. </ul> 
  26.  
  27. </li> 
  28.  
  29. <li><a href="#">Child Three</a></li> 
  30.  
  31. </ul> 
  32.  
  33. </li> 
  34.  
  35. <li><a href="#">Contact</a></li> 
  36.  
  37. </ul> 
  38.  
  39. </nav> 

#p#

b) 使用<article>标签来描述每一个要展示的内容实体,比如要展示的是多篇文章列表,其中的每一篇文章的具体内容就可以使用<article>标签了。如下代码所示:

  1. <section class="content"> 
  2. <!—文章1--> 
  3. <article class="post"> 
  4. <h1 class="post-title"><a href=#">This is a title for post</a></h1> 
  5. <!-- 文章元数据--> 
  6. <div class="entry post-meta"> 
  7. <span class="post-author">Richard KS</span> 
  8. <span class="post-date">20th March 2013</span> 
  9. <span class="post-category">Tutorials</span> 
  10. <span class="post-tag">HTML5, CSS3 and Responsive</span> 
  11. <span class="post-comment">10 Comments</span> 
  12. </div> 
  13. <!-- 文章的内容 content --> 
  14. <div class="entry post-content"> 
  15. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s 
  16. </div> 
  17. </article> 
  18. <!—文章1结束--> 
  19. </section> 

d) 添加<aside class=’sidebar’>标签

HTML5提供的<aside>元素标签用来表示当前页面或文章的附属信息部分,可以包含与当前页面或主要内容相关的引用、侧边栏、广告、nav元素组,以及其他类似的有别与主要内容的部分。

根据目前的规范,<aside>元素有两种使用方法:

1 被包含在<article>中作为主要内容的附属信息部分,其中的内容可以是与当前文章有关的引用、词汇列表等。

2 <article>之外使用,作为页面或站点全局的附属信息部分;最典型的形式是侧边栏(sidebar),其中的内容可以是友情链接、附属导航或广告单元等。

代码如下:

  1. <aside class="sidebar"> 
  2. <ul class="widget-sidebar"> 
  3.  
  4. <!-- some sample list --> 
  5. <li class="widget widget_categories"> 
  6. <h3 class="widget-title">Categories</h3> 
  7. <ul> 
  8. <li><a href="#">Category 1</a></li> 
  9. <li><a href="#">Category 2</a></li> 
  10. <li><a href="#">Parent Category</a> 
  11. <ul class="children"> 
  12. <li><a href="#">Child One</a></li> 
  13. <li><a href="#">Child Two</a> 
  14. <ul class="children"> 
  15. <li><a href="#">Grandchild One</a></li> 
  16. <li><a href="#">Grandchild Two</a></li> 
  17. <li><a href="#">Grandchild Three</a></li> 
  18. </ul> 
  19. </li> 
  20. <li><a href="#">Child Three</a></li> 
  21. </ul> 
  22. </li> 
  23. <li><a href="#">Category 3</a></li> 
  24. </ul> 
  25. </li> 
  26.  
  27. <!-- some sample text block --> 
  28. <li class="widget widget_text"> 
  29. <h3 class="widget-title">Text</h3> 
  30. <div class="textwidget"> 
  31. Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
  32. </div> 
  33. </li> 
  34.  
  35. </ul> 
  36. </aside> 

e) 加上最后的<footer>标签,代码为:

  1. <footer> 
  2. <div class="footer-left">Copyright@ 2013 HTML5.com</div> 
  3. <div class="footer-right">Privacy Policy - About Us</div> 
  4. </footer> 

最后运行的效果如下:  

步骤4  增加CSS样式
  
首先创建一个空白的样式,如下:

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

然后在http://necolas.github.com/normalize.css/中下载这个css,然后将其内容复制到该空白的文件中代码如下:

#p#

  1. body { 
  2. font-familyarialsans-serif
  3. font-size100%/* best for all browser using em */ 
  4. padding:0;  
  5. margin:0
  6.  
  7. *, html { line-height1.6em; } 
  8.  
  9. article img { width:automax-width:100%height:auto; } 
  10.  
  11. .sidebar a, article a, header a, footer a { color#C30; } 
  12. header a { text-decorationnone; } 
  13.  
  14. #wrapper { 
  15. font-size0.8em/* 13px from 100% global font-size */ 
  16. max-width960px/* standard 1024px wide */ 
  17. margin0 auto
  18.  
  19. /* css for <header> */ 
  20. header { 
  21.     padding1em 0
  22.     margin0px
  23.     floatleft
  24.     width100%
  25. header hgroup { width100%font-weight:normal; } 
  26.  
  27.  
  28. /* css for <nav> */ 
  29. nav { 
  30.     displayblock
  31.     margin0 0 2em
  32.     padding0px
  33.     floatleft
  34.     width100%
  35.     background-color#181919
  36. nav ul ul {displaynone;} 
  37. nav ul li:hover > ul {displayblock;} 
  38.      
  39. nav ul { 
  40.     padding0
  41.     list-stylenone
  42.     positionrelative
  43.     displayinline-table
  44.     z-index9999
  45.     margin0px
  46.     floatleft
  47.     width100%
  48. nav ul:after {content""clearbothdisplayblock;} 
  49.  
  50. nav ul li {floatleft;} 
  51.  
  52. nav ul li:hover a {color#fff;} 
  53.  
  54. nav ul li a { 
  55.     displayblock
  56.     padding1em
  57.     font-size1.125em
  58.     color#ccc
  59.     text-decorationnone
  60.     margin0px
  61.     background-color#000
  62.     border-right1px solid #333
  63. nav ul li:last-of-type a {border-right1px solid transparent !important;} 
  64.  
  65. nav ul ul { 
  66.     background#5f6975
  67.     border-radius: 0px
  68.     padding0
  69.     positionabsolute
  70.     top: 100%
  71.     widthauto
  72.     floatnone
  73. nav ul li:hover { 
  74.     background#5f6975
  75.     color#FFF
  76. nav ul ul li a:hover { 
  77.     background-color#4b545f
  78.  
  79. nav ul ul li { 
  80. floatnone
  81. border-bottom1px solid #444240
  82. positionrelative
  83.  
  84. nav ul ul li a { 
  85. padding0.5em 1em
  86. font-size1em
  87. width:10em
  88. color#fff
  89. }    
  90.  
  91.  
  92. nav ul ul ul { 
  93. positionabsolute; left: 100%; top:0
  94.  
  95. /* css for <section class='content'> */ 
  96. section.content { width70%float:left; } 
  97. .content article { width:100%float:leftpadding0 0 1emmargin0 0 1emborder-bottom1px solid #ddd; } 
  98. article .entry { clear:bothpadding0 0 1em; } 
  99. h1.post-title { font-size1.8emmargin:0padding:0;} 
  100. .entry.post-meta { color#888; } 
  101. .entry.post-meta span { padding0 1em 0 0; } 
  102. .entry.post-content { font-size1.125emmargin:0padding:0;} 
  103.  
  104. /* css for <aside class='sidebar'> */ 
  105. aside.sidebar { width25%float:right; } 
  106. aside.sidebar ul { 
  107.     width:100%
  108.     margin0px
  109.     padding0px
  110.     floatleft
  111.     list-stylenone
  112. aside.sidebar ul li { 
  113.     width:100%
  114.     margin0px 0px 2em
  115.     padding0px
  116.     floatleft
  117.     list-stylenone
  118. aside.sidebar ul li ul li { 
  119.     margin0px 0px 0.2em
  120.     padding0px
  121. aside.sidebar ul li ul li ul li { 
  122.     margin0px
  123.     padding0px 0px 0px 1em
  124.     width90%
  125.     font-size0.9em
  126. aside.sidebar ul li h3.widget-title { 
  127.     width:100%
  128.     margin0px
  129.     padding0px
  130.     floatleft
  131.     font-size1.45em
  132.  
  133. /* css for <footer> */ 
  134. footer { 
  135.     width98%
  136.     float:left
  137.     padding1%
  138.     background-colorwhite
  139.     margin-top2em
  140. footer .footer-left { width45%float:lefttext-align:left; } 
  141. footer .footer-right { width45%float:righttext-align:right; } 

步骤5  为移动应用使用@media query查询
  为了进行响应式设计,最佳的方案是使用@media query去进行查询,在上面的CSS代码中添加如下代码:

  1. /* ipad 768px */ 
  2. @media only screen and (min-width:470px) and (max-width:770px){ 
  3. body { background-colorred; } #wrapper { width:96%font-size0.6875em; } 
  4. section.content, aside.sidebar { width:100%; } 
  5.  
  6. /* iphone 468px */ 
  7. @media only screen and (min-width:270px) and (max-width:470px){ 
  8. body { background-color: yellow; } #wrapper { width:96%font-size0.6875em; } 
  9. section.content, aside.sidebar { width:100%; } 

步骤6 增加jquery,modernizerhtml5shiv<head>标签中
  这里推荐使用Modernizr html5shiv,它们都是一个能在多种浏览器中通过运行各种脚本兼容运行支持大部分HTML 5标签的插件。我们将它们和jQuery库放在</head>标签前,

代码如下:

 

  1.  <!--[if lt IE 9]> 
  2. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> 
  3. <![endif]--> 
  4. <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
  5. <script src="modernizr-latest.js" type="text/javascript"></script> 

 

最后运行效果如图:

 


读者可以将自己的屏幕分辨率调整到768px或者468px,会发现页面依然能随着分辨率的改变而改变自适应,没出现任何问题。读者可以在这个基础上进行扩充调整这个HTML 5模版,以适应自己的需求。

原文链接: http://www.dezzain.com/tutorials/creating-a-simple-responsive-html5-template/

 

责任编辑:陈四芳 来源: 51CTO
相关推荐

2013-07-09 09:24:29

响应式HTML5CSS3

2013-09-05 09:55:06

HTML5响应式

2015-04-21 14:30:57

HTML5免费响应式HTML5框架

2013-03-04 14:13:13

HTML5CSS3响应式

2015-11-30 11:02:35

HTML5网站模板

2012-04-18 15:36:33

HTML5Canvas交互式

2015-05-07 09:39:01

2013-01-24 10:26:04

HTML5HTML 5HTML5的未来

2011-05-13 17:36:05

HTML

2016-06-16 17:34:59

html5高招

2016-01-15 09:25:45

Web开发HTML5框架

2023-03-16 09:00:00

HTML5HTML语言

2013-10-21 15:24:49

html5游戏

2013-06-06 10:42:25

2011-05-13 17:41:40

2011-01-14 17:53:33

HTML5cssweb

2017-01-03 18:09:33

HTML5本地存储Web

2011-05-12 15:42:16

HTML5

2012-08-27 10:00:06

HTML5

2011-05-11 12:59:18

HTML5
点赞
收藏

51CTO技术栈公众号