您好,欢迎访问一九零五行业门户网

【转】你所不知道的HTML <head/> 头标签

html的头部内容特别多,有针对seo的头部信息,也有针对移动设备的头部信息。而且各个浏览器内核以及各个国内浏览器厂商都有些自己的标签元素,有很多差异性。移动端的工作已经越来越成为前端工作的重要内容,除了平常的项目开发,html 头部标签功能,特别是meta,link等标签的功能属性显得非常重要。这里整理了一份  部分的清单,让大家了解每个标签及相应属性的意义,写出满足自己需求的  头部标签,可以很有效的增强页面的可用性。
注:去年整理过移动前端不得不了解的html5 head 头标签,随着时间和浏览器厂商的升级,现在看起来似乎有些过时了。所以重新整理了一下。增加了新的内容,及过时的一些提示,同时增加了部分桌面端浏览器的一些说明。
html基本的头部标签下面是html基本的头部元素:
doctype html>html>head> meta charset=utf-8> meta http-equiv=x-ua-compatible content=ie=edge> meta name=viewport content=width=device-width, initial-scale=1> title>页面标题title> ...head>
其中
meta http-equiv=x-ua-compatible content=ie=edge>
在桌面开发的时候可以让ie浏览器以最新的模式渲染页面,具体可以查看本文internet explorer浏览器部分。
如果你的页面确定只在桌面浏览器中运行,那么
meta name=viewport content=width=device-width, initial-scale=1>
也可以省略。
doctypedoctype(document type),该声明位于文档中最前面的位置,处于 html 标签之前,此标签告知浏览器文档使用哪种 html 或者 xhtml 规范。
使用 html5 doctype,不区分大小写。
doctype html>
charset声明文档使用的字符编码,
meta charset=utf-8>
html5 之前网页中会这样写:
meta http-equiv=content-type content=text/html; charset=utf-8>
这两个是等效的,具体可移步阅读: vs ,所以建议使用较短的,易于记忆。
lang属性更加标准的 lang 属性写法 http://zhi.hu/xyia
简体中文
html lang=zh-cmn-hans>
繁体中文
html lang=zh-cmn-hant>
很少情况才需要加地区代码,通常是为了强调不同地区汉语使用差异,例如:
p lang=zh-cmn-hans>strong lang=zh-cmn-hans-cn>菠萝strong>和strong lang=zh-cmn-hant-tw>鳳梨strong>其实是同一种水果。只是大陆和台湾称谓不同,且新加坡、马来西亚一带的称谓也是不同的,称之为strong lang=zh-cmn-hans-sg>黄梨strong>。p>
为什么 lang=zh-cmn-hans 而不是我们通常写的 lang=zh-cn 呢,请移步阅读: 页头部的声明应该是用 lang=”zh” 还是 lang=”zh-cn”。
meta 标签meta标签是html中head头部的一个辅助性标签,它位于html文档头部的  和  标记之间,它提供用户不可见的信息。虽然这部分信息用户不可见,但是其作用非常强大,特别是当今的前端开发工作中,设置合适的meta标签可以大大提升网站页面的可用性。
桌面端开发中,meta标签通常用来为搜索引擎优化(seo)及 robots定义页面主题,或者是定义用户浏览器上的cookie;它可以用于鉴别作者,设定页面格式,标注内容提要和关键字;还可以设置页面使其可以根据你定义的时间间隔刷新自己,以及设置rasc内容等级,等等。
移动端开发中,meta标签除了桌面端中的功能设置外,还包括,比如viewport设置,添加到主屏幕图标,标签页颜色等等实用设置。具体可以看后面详细的介绍。
meta标签分类meta标签根据属性的不同,可分为两大部分:http-equiv 和 name 属性。
http-equiv:相当于http的文件头作用,它可以向浏览器传回一些有用的信息,以帮助浏览器正确地显示网页内容。
name属性:主要用于描述网页,与之对应的属性值为content,content中的内容主要是便于浏览器,搜索引擎等机器人识别,等等。
推荐使用的meta标签meta charset=utf-8>meta http-equiv=x-ua-compatible content=ie=edge>meta name=viewport content=width=device-width, initial-scale=1, shrink-to-fit=no> meta http-equiv=content-security-policy content=default-src 'self'> meta name=application-name content=应用名称> meta name=description content=一个页面描述> meta name=robots content=index,follow,noodp>meta name=googlebot content=index,follow> meta name=google content=nositelinkssearchbox> meta name=google content=notranslate> meta name=google-site-verification content=verification_token> meta name=generator content=program> meta name=subject content=你的网站主题> meta name=abstract content=> meta name=url content=https://example.com/> meta name=directory content=submission> meta name=rating content=general> meta name=referrer content=never> meta name=format-detection content=telephone=no> meta http-equiv=x-dns-prefetch-control content=off> meta http-equiv=set-cookie content=name=value; expires=date; path=url> meta http-equiv=window-target content=_value> meta name=icbm content=latitude, longitude>meta name=geo.position content=latitude;longitude>meta name=geo.region content=country[-state]>meta name=geo.placename content=city/town>
相关的详细说明请查看:
google 可以识别的 meta 标签whatwg wiki: meta 拓展icbm – 维基百科地理标记 – 维基百科为移动设备添加 viewportviewport 可以让布局在移动浏览器上显示的更好。 通常会写
meta name =viewport content =initial-scale=1, maximum-scale=3, minimum-scale=1, user-scalable=no>
width=device-width 会导致 iphone 5 添加到主屏后以 webapp 全屏模式打开页面时出现黑边(http://bigc.at/ios-webapp-viewport-meta.orz)
content 参数:
width viewport 宽度(数值/device-width)height viewport 高度(数值/device-height)initial-scale 初始缩放比例maximum-scale 最大缩放比例minimum-scale 最小缩放比例user-scalable 是否允许用户缩放(yes/no)minimal-ui ios 7.1 beta 2 中新增属性(注意:ios8 中已经删除),可以在页面加载时最小化上下状态栏。这是一个布尔值,可以直接这样写:meta name=viewport content=width=device-width, initial-scale=1, minimal-ui>
而如果你的网站不是响应式的,请不要使用 initial-scale 或者禁用缩放。
meta name=viewport content=width=device-width,user-scalable=yes>
相关链接:非响应式设计的viewport
适配 iphone 6 和 iphone 6plus 则需要写:
meta name=viewport content=width=375>meta name=viewport content=width=414>
大部分 4.7~5 寸的安卓设备的 viewport 宽设为 360px,iphone 6 上却是 375px,大部分 5.5 寸安卓机器(比如说三星 note)的 viewport 宽为 400,iphone 6 plus 上是 414px。
seo 优化部分页面标题标签(head 头部必须)title>your titletitle>
页面关键词 keywordsmeta name=keywords content=your keywords>
页面描述内容 descriptionmeta name=description content=your description>
定义网页作者 authormeta name=author content=author,email address>
定义网页搜索引擎索引方式,robotterms 是一组使用英文逗号「,」分割的值,通常有如下几种取值:none,noindex,nofollow,all,index和follow。meta name=robots content=index,follow>
相关链接:web1038 – 标记包含无效的值
百度禁止转码通过百度手机打开网页时,百度可能会对你的网页进行转码,脱下你的衣服,往你的身上贴狗皮膏药的广告,为此可在 head 内添加
meta http-equiv=cache-control content=no-siteapp />
相关链接:siteapp 转码声明
不推荐的 meta 属性下面是不推荐使用的 meta 属性,因为它们采用率低,或已弃用:
meta name=language content=en> meta name=keywords content=你,关键字,在这里,不使用空格,而用逗号进行分隔>meta name=revised content=sunday, july 18th, 2010, 5:15 pm> meta name=reply-to content=email@example.com> meta name=author content=name, email@example.com>meta name=designer content=>meta name=owner content=> meta name=revisit-after content=7 days> meta http-equiv=refresh content=300; url=https://example.com/> meta name=topic content=> meta name=summary content=> meta name=classification content=business> meta name=identifier-url content=https://example.com/> meta name=category content=> meta name=coverage content=worldwide> meta name=distribution content=global> meta http-equiv=pics-label content=value> meta http-equiv=expires content=0>meta http-equiv=pragma content=no-cache>meta http-equiv=cache-control content=no-cache>
link 标签说到 link 标签,估计大家的第一反应和我一样,就是引入外部css样式文件的,不错,这是 link 标签最最常用的功能。不过它还有很多别的用处,比如这是浏览器 favicon 图标,touch图标等等。
link rel=canonical href=https://example.com/2010/06/9-things-to-do-before-entering-social-media.html> link rel=shortlink href=https://example.com/?p=42> link rel=amphtml href=https://example.com/path/to/amp-version.html> link rel=stylesheet href=https://example.com/styles.css> link rel=manifest href=manifest.json> link rel=author href=humans.txt> link rel=copyright href=copyright.html> link rel=alternate href=https://es.example.com/ hreflang=es> link rel=me href=https://google.com/profiles/thenextweb type=text/html>link rel=me href=mailto:name@example.com>link rel=me href=sms:+15035550125> link rel=archives href=https://example.com/2003/05/ title=may 2003> link rel=index href=https://example.com/ title=dewitt clinton> link rel=start href=https://example.com/photos/pattern_recognition_1_about/ title=pattern recognition 1> link rel=prev href=https://example.com/opensearch/opensearch-and-openid-a-sure-way-to-get-my-attention/ title=opensearch and openid? a sure way to get my attention.> link rel=self type=application/atom+xml href=https://example.com/atomfeed.php?page=3> link rel=first href=https://example.com/atomfeed.php>link rel=next href=https://example.com/atomfeed.php?page=4>link rel=previous href=https://example.com/atomfeed.php?page=2>link rel=last href=https://example.com/atomfeed.php?page=147> link rel=edituri href=https://example.com/xmlrpc.php?rsd type=application/rsd+xml title=rsd> link rel=pingback href=https://example.com/xmlrpc.php> link rel=webmention href=https://example.com/webmention> link rel=import href=component.html> link rel=search href=/open-search.xml type=application/opensearchdescription+xml title=search title> link rel=alternate href=https://feeds.feedburner.com/example type=application/rss+xml title=rss>link rel=alternate href=https://example.com/feed.atom type=application/atom+xml title=atom 0.3> link rel=dns-prefetch href=//example.com/>link rel=preconnect href=https://www.example.com/>link rel=prefetch href=https://www.example.com/>link rel=prerender href=https://example.com/>link rel=preload href=image.png as=image>
具体说明查看:https://css-tricks.com/prefetching-preloading-prebrowsing/
rss订阅link rel=alternate type=application/rss+xml title=rss href=/rss.xml />
不推荐的link标签以下是不推荐使用的链接关系:
link rel=shortcut icon href=path/to/favicon.ico> link rel=subresource href=styles.css>
具体说明查看:https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/y_2efrh9bos/gulyaporbwaj
favicon 图标ie 11, chrome, firefox, safari, opera支持形式设置:
link rel=icon href=path/to/favicon-16.png sizes=16x16 type=image/png>link rel=icon href=path/to/favicon-32.png sizes=32x32 type=image/png>link rel=icon href=path/to/favicon-48.png sizes=48x48 type=image/png>link rel=icon href=path/to/favicon-62.png sizes=62x62 type=image/png>link rel=icon href=path/to/favicon-192.png sizes=192x192 type=image/png>
注意:对于ie 10及以下版本不支持形式设置,只通过将命名为favicon.ico的文件放置在网站根目录中实现。
比较详细的 favicon 介绍可参考:
所有关于网站图标(和触摸图标)favicon 对照表浏览器及平台详细说明qq 浏览器(x5 内核)qq 浏览器(x5 内核)同样适用于微信,qq等第三方应用页面开发。
meta name=x5-orientation content=portrait|landscape>meta name=x5-fullscreen content=true>meta name=x5-page-mode content=app>
360浏览器设置 360 浏览器渲染模式:webkit 为极速内核,ie-comp 为 ie 兼容内核,ie-stand 为 ie 标准内核。
meta name=renderer content=webkit|ie-comp|ie-stand>
详情文档链接:浏览器内核控制meta标签说明文档
360 浏览器就会在读取到这个标签后,立即切换对应的极速核。 另外为了保险起见再加入
meta http-equiv=x-ua-compatible content=ie=edge,chrome=1>
这样写可以达到的效果是如果安装了 google chrome frame,则使用 gcf 来渲染页面,如果没有安装 gcf,则使用最高版本的 ie 内核进行渲染。
uc 浏览器设置屏幕方向portrait 为横屏,landscape 为竖屏。
meta name=screen-orientation content=portrait|landscape>
设置全屏meta name=full-screen content=yes>
设置适应屏幕排版(缩放是否显示滚动条)uc 浏览器在标准排版效果实现的基础上,提供适应屏幕的排版方式,当设置为 uc-fitscreen=yes,页面进行缩放操作时,仅放大图片和文字等元素,但不放大屏幕宽度,保持不出现水平(横向)滚动条。
meta name=viewport content=uc-fitscreen=no|yes>
排版模式uc 浏览器提供两种排版模式,分别是适屏模式(fitscreen)及标准模式(standard),其中适屏模式简化了一些页面的处理,使得页面内容更适合进行页面阅读、节省流量及响应更快,而标准模式则能按照标准规范对页面进行排版及渲染。
meta name=layoutmode content=fitscreen|standard>
夜间模式可以帮助用户在低亮度或黑暗情况下更舒适的进行页面浏览。由于基于网页的应用愈加复杂,由浏览器实现的单一夜间模式不一定能够适应所有情况(例如游戏应用),因此 uc 浏览器允许网页设计者对其设计的页面禁用浏览器的夜间模式,自行设计更适合用户使用的夜间模式。
注意:页面内的 frame/iframe 中的夜间模式的 meta 不生效。
meta name=nightmode content=enable|disable>
整页图片强制显示为了节省流量及加快速度,uc 为用户提供了无图模式,在实际使用中存在页面中的图片是不可缺少的,例如验证码,地图等。通过强制图片显示的功能可以保证图片显示不受用户的设置影响。
**注意:整页图片强制显示仅对当前页面生效,对页面内的 frame/iframe 不生效,也不影响前进后退的页面
meta name=imagemode content=force>
开启应用模式meta name=browsermode content=application>
应用模式是为方便 web 应用及游戏开发者设置的综合开关,通过meta标签进行指示打开,当进入应用模式时,浏览器将自动调整以下参数:
参数状态说明
全屏 生效 可通过 meta 或 js api 调用退出全屏
长按菜单 失效 可通过 js api 调用重新生效
浏览器默认手势 失效 可通过 js api 调用重新生效
排版模式 标准模式 可通过 meta 或 js api 调用设置其他排版模式
强制图片显示 生效 /
夜间模式 失效 可通过 meta 或 js api 调用启用夜间模式
缩放字体例如:禁用的 uc 浏览器的字体缩放功能
meta name=wap-font-scale content=no>
具体uc 浏览器文档链接:uc 浏览器文档
apple ios原生浏览器添加智能 app 广告条告诉浏览器这个网站对应的app,并在页面上显示下载banner,需要注意的是smart app banners标签不能用在frame框架内部,否则不起作用。
其中app-id(必须), affiliate-data (可选), app-argument (可选)
meta name=apple-itunes-app content=app-id=app_id,affiliate-data=affiliate_id,app-argument=some_text>
例如digg的写法:
meta name=apple-itunes-app content=app-id=362872995, affiliate-data=bevboqlt02i, app-argument=digg://>
忽略数字自动识别为电话号码meta name=format-detection content=telephone=no>
启用 webapp 全屏模式meta name=apple-mobile-web-app-capable content=yes>
添加到主屏后设置状态栏的背景颜色meta name=apple-mobile-web-app-status-bar-style content=black>
只有在 “apple-mobile-web-app-capable” content=”yes” 时生效。
如果设置为 default 或 black ,网页内容从状态栏底部开始。如果设置为 black-translucent ,网页内容充满整个屏幕,顶部会被状态栏遮挡。添加到主屏后的标题(ios 6 新增)meta name=apple-mobile-web-app-title content=app title>
ios 图标图片自动处理成圆角和高光等效果。
link rel=apple-touch-icon href=path/to/apple-touch-icon.png>
禁止系统自动添加效果,直接显示设计原图。
link rel=apple-touch-icon-precomposed href=path/to/apple-touch-icon-precomposed.png>
ios 8+ 不再支持 precomposed, 只有 apple-touch-icon 是必须的
在大多数情况下,在head中一个180×180px的图标就足够了。如果您想要由设备确定的唯一图标,请使用不同大小的图标。
link rel=apple-touch-icon sizes=57x57 href=path/to/icon@57.png>link rel=apple-touch-icon sizes=72x72 href=path/to/icon@72.png>link rel=apple-touch-icon sizes=114x114 href=path/to/icon@114.png>link rel=apple-touch-icon sizes=144x144 href=path/to/icon@144.png>
启动画面 ( 不赞成使用 )ipad 的启动画面是不包括状态栏区域的,iphone 和 ipod touch 的启动画面是包含状态栏区域的
link rel=apple-touch-startup-image href=path/to/startup.png>
具体描述设置请查看http://www.css88.com/archives/5480中相应的说明。
ios 应用深度链接meta name=apple-itunes-app content=app-id=app-id, app-argument=http/url-sample.com>link rel=alternate href=ios-app://app-id/http/url-sample.com>
google android原生浏览器标签页选项卡颜色android lollipop 中的 chrome 39 增加 theme-color meta 标签,用来控制选项卡颜色。
http://updates.html5rocks.com/2014/11/support-for-theme-color-in-chrome-39-for-android
meta name=theme-color content=#db5945>
添加到主屏meta name=mobile-web-app-capable content=yes>
详细链接: https://developer.chrome.com/multidevice/android/installtohomescreen
安卓应用深度链接(网页上唤起应用)meta name=google-play-app content=app-id=package-name>link rel=alternate href=android-app://package-name/http/url-sample.com>
注:貌似没测试成功,如果你知道如何正确设置,欢迎留言斧正。
apple safari 浏览器safari 10开始支持固定书签页的svg favicons了,你可以这样使用:
link rel=mask-icon href=path/to/icon.svg color=red>
类似的效果
扩展阅读:https://yoast.com/dev-blog/safari-pinned-tab-icon-mask-icon/
google chrome浏览器关闭chrome浏览器下翻译插件有些时候感觉chrome浏览器下翻译插件很烦人,可以通过下面的代码禁用它。
meta name=google value=notranslate /> 
chrome浏览器插件安装有时候,你需要在你的页面上点击某个安卓,直接安卓你的chrome浏览器插件,而不是链接到chrome webstore 的详细地址再安装,那么你可以使用:
link rel=chrome-webstore-item href=https://chrome.google.com/webstore/detail/app_id>
具体使用,请查看:using inline installation
google chrome mobile (只针对 android)从 chrome 31 开始,你可以设置你的 web 应用为“app mode”,如 safari。
link rel=manifest href=manifest.json> meta name=mobile-web-app-capable content=yes> link rel=icon sizes=192x192 href=nice-highres.png>link rel=icon sizes=128x128 href=niceicon.png>link rel=apple-touch-icon sizes=128x128 href=niceicon.png>link rel=apple-touch-icon-precomposed sizes=128x128 href=niceicon.png>
internet explorer浏览器模式设置//ie8以下以ie7标准模式呈现网页,而ie9则以ie9的标准模式呈现网页:meta http-equiv=x-ua-compatible content=ie=emulateie7; ie=emulateie9>//如果安装了gcf,则使用gcf来渲染页面(chrome=1),//如果没有安装gcf,则使用最高版本的ie内核进行渲染(ie=edge)meta http-equiv=x-ua-compatible content=ie=edge>
gcf(google chrome frame )相关链接:https://www.chromium.org/developers/how-tos/chrome-frame-getting-started
x-ua-compatible相关链接:https://blogs.msdn.microsoft.com/ie/2010/06/16/ies-compatibility-features-for-site-developers/
win8,win10下的一些设置meta http-equiv=cleartype content=on>meta name=skype_toolbar content=skype_toolbar_parser_compatible> meta name=msapplication-tap-highlight content=no> meta name=application-name content=contoso pinned site caption>meta name=msapplication-tooltip content=example tooltip text>meta name=msapplication-starturl content=/> meta name=msapplication-config content=http://example.com/browserconfig.xml> meta name=msapplication-allowdomainapicalls content=true>meta name=msapplication-allowdomainmetatags content=true>meta name=msapplication-badge content=frequency=30; polling-uri=http://example.com/id45453245/polling.xml>meta name=msapplication-navbutton-color content=#ff3300>meta name=msapplication-notification content=frequency=60;polling-uri=http://example.com/livetile>meta name=msapplication-square150x150logo content=path/to/logo.png>meta name=msapplication-square310x310logo content=path/to/largelogo.png>meta name=msapplication-square70x70logo content=path/to/tinylogo.png>meta name=msapplication-wide310x150logo content=path/to/widelogo.png>meta name=msapplication-task content=name=check order status;action-uri=./orderstatus.aspx?src=ie9;icon-uri=./favicon.ico>meta name=msapplication-task-separator content=1>//windows 8 磁贴颜色meta name=msapplication-tilecolor content=#ff3300>//windows 8 磁贴图标meta name=msapplication-tileimage content=path/to/tileimage.jpg>meta name=msapplication-window content=width=1024;height=768>
app 链接meta property=al:ios:url content=applinks://docs>meta property=al:ios:app_store_id content=12345>meta property=al:ios:app_name content=app links>meta property=al:android:url content=applinks://docs>meta property=al:android:app_name content=app links>meta property=al:android:package content=org.applinks>meta property=al:web:url content=http://applinks.org/documentation>
具体请查看:app links docs
其它类似信息

推荐信息