每个标准的 HTML 文档都包含一个 <head>
头部分,除了声明文档类型,编码方式和文档标题,引入外部资源这些基本功能外, 头还能作不少很是有用的事情,这篇文章整理了做者承认的一些最佳实践,写在这里与各位分享。css
在 html
标签中经过 lang
属性进行明确的语言声明,将会有助于翻译,英文、简体中文和繁体中文网页所属性值以下:html
<html lang="en"> <html lang="zh-Hans"> <html lang="zh-Hant">
请在 <head>
中第一行统一使用 utf-8
编码声明。html5
<meta charset="utf-8">
尽可能不使用 <base>
元素,绝对地址和 URL 对于开发者和用户来讲都更好。git
<!-- Bad --> <base href="/blog/"> <link href="hello-world" rel="canonical"> <!-- Good --> <link href="/blog/hello-world" rel="canonical">
设置 viewport
的宽度为设备宽度,默认缩放比为 1(容许用户缩放),设置 viewport-fit=cover
以兼容 iPhone X 全面屏“刘海”的显示。github
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
DNS 预读取是一项使浏览器主动去执行域名解析的功能,其范围包括当前文档内的全部连接,这可以减小用户点击连接时的延迟。web
<meta http-equiv="x-dns-prefetch-control" content="on"> <link rel="dns-prefetch" href="//www.google-analytics.com"> <link rel="dns-prefetch" href="//fonts.googleapis.com">
指示浏览器预先请求当前页面所须要的关键性资源。api
<link rel="preload" href="/css/main.css" as="style"> <link rel="preload" href="/js/main.js" as="script">
指示浏览器预先请求用户即将浏览页面所须要的关键性资源。跨域
<link rel="prefetch" href="/img/css-sprite.png"> <link rel="prefetch" href="/fonts/icons.woff2">
对于国内各类双核浏览器,经过设置 renderer
头要求它们使用 webkit 内核;对于 IE 浏览器,经过设置 IE=edge
要求它使用最新的版本;对于百度搜索强制转码的流氓作法,经过 no-transform
禁止其自动转码;指示浏览器对相似电话、邮箱或地址的内容不要自做聪明的瞎识别(移动端)。浏览器
<meta name="renderer" content="webkit"> <!-- 用在360中 --> <meta name="force-rendering" content="webkit"> <!-- 用在其它 --> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- 针对 IE 浏览器 --> <meta http-equiv="Cache-Control" content="no-transform"> <!-- 针对百度搜索 --> <meta name="format-detection" content="telephone=no,email=no,adress=no">
HTML 文档的一些元数据,包括:做者、描述、关键词和生成工具;设置 robots
为 index,follow
指示搜索引擎爬虫该页面须要进入索引,而且页面内的全部连接都要继续跟踪;设置 referrer
为 origin-when-cross-origin
指示浏览器在进行跨域跳转时,其 referrer
值将统一为域名,而非具体的 URL 地址。markdown
<meta name="author" content="米老朱"> <meta name="description" content="米老朱的我的博客"> <meta name="keywords" content="米老朱,极客,博客,WEB,开发,产品设计"> <meta name="generator" content="Hugo 0.32"> <meta name="robots" content="index,follow"> <meta name="referrer" content="origin-when-cross-origin">
虽然说全部浏览器都还支持过期的 favicon.ico 格式,但在 HTML5 时代,咱们应该使用更好的 PNG icon with sizes 方案。同时为了兼容老旧浏览器,咱们能够将生成好的 favicon.ico 文件放在网站的根目录下面,一般浏览器会自动请求并加载它,并不须要额外经过 link 标签引入。
<meta name="theme-color" content="#db5945"> <!-- 主题颜色 --> <meta name="application-name" content="米老朱的博客"> <!-- 应用名称 --> <meta name="apple-mobile-web-app-capable" content="yes"> <!-- 隐藏状态栏 --> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <!-- 状态栏颜色 --> <meta name="apple-mobile-web-app-title" content="米老朱的博客"> <!-- iOS 下的应用名称 --> <meta name="msapplication-tooltip" content="米老朱的博客"> <!-- Hover 的提示信息 --> <meta name="msapplication-TileColor" content="#db5945"> <!-- 磁贴背景颜色 --> <meta name="msapplication-TileImage" content="/img/logo-144.png"> <!-- 磁贴图标 --> <meta name="msapplication-config" content="/browserconfig.xml"> <!-- 磁贴配置文件 --> <link rel="icon" type="image/png" href="/img/logo-16.png" sizes="16x16"> <!-- Browser Favicon --> <link rel="icon" type="image/png" href="/img/logo-32.png" sizes="32x32"> <!-- Taskbar Shortcut --> <link rel="icon" type="image/png" href="/img/logo-96.png" sizes="96x96"> <!-- Desktop Shortcut --> <link rel="icon" type="image/png" href="/img/logo-128.png" sizes="128x128"> <!-- Chrome Web Store --> <link rel="icon" type="image/png" href="/img/logo-196.png" sizes="196x196"> <!-- Chrome for Android Home Screen --> <link rel="icon" type="image/png" href="/img/logo-228.png" sizes="228x228"> <!-- Opera Coast Icon --> <link rel