Recently I saw the HTML5 Boilerplate template and studied and understood it systematically. Today, with various CSS libraries and JS frameworks emerging one after another, it feels great to see such a good HTML template. Write a blog and recommend it to everyone.
1: What is HTML5 Boilerplate? What problem was solved?
I must have this question when I first heard about this person! I looked online and found that many people think this is the same thing as Bootstrap. This is really wrong.
In fact, HTML5 Boilerplate is just a simple HTML template.
What? HTML template? What is it used for?
Here I have to mention the problems that all front-end development will encounter. How do you do it every time you want to create a new page? The doctype, html, head, body, and meta tags are so annoying to write. Or copy it from a previous project, or copy the template recommended by Bootstrap, etc. But when doing these things, have you ever thought about whether your writing method is the best? Or does the industry have a more unified recommendation on this? Well, the answer is yes.
HTML5 Boilerplate solves such a problem. It provides a very complete HTML template, so perfect that all pages seem to abide by this rule.
It sounds so miraculous, so we still have to see what happens. After downloading it from the official website, the most important thing is an index.html file, which is not big. Let’s take a look at its source code
<!DOCTYPE html><!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--><!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--><!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--><!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Place favicon.ico and apple-touch-icon.png in the root directory --> <link rel="stylesheet" href="css/normalize.css"> <link rel="stylesheet" href="css/main.css"> <script src="js/vendor/modernizr-2.6.2.min.js"></script> </head> <body> <!--[if lt IE 7]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <!-- Add your site or application content here --> <p>Hello world! This is HTML5 Boilerplate.</p> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script> <script src="js/plugins.js"></script> <script src="js/main.js"></script> <!-- Google Analytics: change UA-XXXXX-X to be your site's ID. --> <script> (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]= function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date; e=o.createElement(i);r=o.getElementsByTagName(i)[0]; e.src='//www.google-analytics.com/analytics.js'; r.parentNode.insertBefore(e,r)}(window,document,'script','ga')); ga('create','UA-XXXXX-X');ga('send','pageview'); </script> </body></html>
This can be said That’s all for HTML5 Boilerplate. If you take a quick look, you will definitely find that some of them are written in the same way as you have done before, and some of them are written in ways that you have never seen before, or you may say that you also write in this way but have never thought about why. Next, let’s “dissect” this HTML file first.
2: Brief analysis of index.html
First of all, the document type uses HTML5 document declaration, compared to HTML4 Of the long list, this one is obviously simple and clear. Moreover, it is compatible with all browsers. Because when designing IE, it will also enter the standards mode for this writing method. Therefore, future document declarations will be written like this to save worry.
Then, there is such a large section
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--><!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--><!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--><!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
This code is very classic.
First, let’s look at these conditional judgments, which mean lower than IE7, equal to IE7, equal to IE8, and higher than IE8.
Then there are corresponding class names in the conditional comments. For example, in lt IE 7, there will be three classes lt-ie9 lt-ie8 lt-ie7 on the html tag, which means below ie7, 8, 9. What's the use? In fact, the biggest problem is when writing CSS HACK, because if you write like this, you don't need CSS HACK. For example, if it is ie6, then there will be the lt-ie7 class on the html tag, and you can directly use CSS priority to overwrite the previous settings. That’s it.
Then the special part should be in the last sentence. The last sentence means that all browsers larger than ie8 and non-ie browsers use This html header. If you look closely, you will find that there are several incomplete annotation tags added inside. What's the use? For IE browsers larger than IE8, these tags are completely ignored. For non-ie browsers. Since [if gt IE 8] is not recognized, then together with the following comments, you will find that the entire part is commented. In this way, the most perfect browser identification is achieved.
Then there is a no-js class. This is mainly used together with modernizr.js later. Because modernizr will replace no-js with js when js is enabled in the browser. Simply put, this class can be used to determine whether the browser has enabled js.
Then, there is
<meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title></title><meta name="description" content=""><meta name="viewport" content="width=device-width, initial-scale=1">
首先,先设置文档编码,记住这个放最前面(特别注意别放title后面),以免后面代码出现乱码。
接下来便是设置IE使用最新版本来渲染
然后是描述,便于SEO。viewport指定移动端不对网页进行缩放。
这些个元标签基本都是一个网页必须要有的,所以大家可以检查下自己的网站是否漏了什么。
之后,引入了normalize、main两个css。modernizr这个js。关于这3个文件,后面再详细说明。
进入主体部分。
首先,看到这么一段
<!--[if lt IE 7]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p><![endif]-->
对于使用低于IE7版本的用户,给出升级提示,当然,我们可以选择删除这一段或者换成一个中文提示
然后呢,便是这一段脚本
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script><script src="js/plugins.js"></script><script src="js/main.js"></script>
首先,通过CDN引入jquery。这里用的是谷歌的CDN。如果这段照抄,那么,,嘿嘿,网站肯定杯具了。所以这里换成国内的jqueryCDN把,比如七牛的。
然后,判断jQuery对象是否存在。因为CDN有可能挂了。如果jQuery对象不存在,那么我们就可以用自己服务器的jquery把。
然后引入了plugins.js还有main.js。main.js是空的,plugins.js后面详细说明。
最后一段代码就是引入google统计了。这里,根据自己的需要换成百度统计或者是别的把。就不详细说了。
至此,HTML5 Boilerplate的最关键的模版HTML算是讲完了。以后要新弄一个页面,就照着这个copy把。
不过,HTML5 Boilerplate提供的还不止这些,下面讲讲单个文件的作用把。
三:静态文件
打开项目代码,可以看到有挺多的文件的,有些是说明文件,比如doc/路径下的,就不讲了,有些是值得讲讲的,比如css/ js/下的部分文件。挑几个有趣的说说把。
首先 css目录下有main和normalize
normalize也许大家都听过,就是一个浏览器重置,里面的每一条css都是进过千千万万的人精挑细选的,基本上这个重置属于公认的了。
里面的具体每条规则就不细讲了,可以百度查看这个项目的文档,或者直接看注释也ok。
main就是改项目对normalize的补充,可以看到提供了一些基础类名方便大家,比如图片置换,清除浮动等等。
js提供了个plugins.js
代码如下
// Avoid `console` errors in browsers that lack a console.(function() { var method; var noop = function () {}; var methods = [ 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn' ]; var length = methods.length; var console = (window.console = window.console || {}); while (length--) { method = methods[length]; // Only stub undefined methods. if (!console[method]) { console[method] = noop; } }}());
比较简单,就不说明了。解决的主要问题就是用console调试的时候IE报错。这个问题我想大家都遇见过,调试代码忘记删除,线上IE报错,导致js无法继续执行。加了这个,就可以避免掉这问题了。
还有就是modernizr了,这是个强大的浏览器功能检查js,具体使用可以在官网上看看教程,这里就不说了。
然后,还提供了一些个文件,比如apache的配置htaccess、 404页面、flash跨域需要的文件crossdomain.xml、爬虫过滤文件robots.txt等,大家按需使用。
至此,HTML5 Boilerplate算是全部理完了,很简单的一个项目,但是很实用,也很漂亮。可以作为开发标配。
转载本站文章请注明作者和出处 ,请勿用于任何商业用途