header tag-HTML Tutorial-php.cn
HTML has a lot of header content, including header information for SEO and header information for mobile devices. Moreover, each browser core and each domestic browser manufacturer have their own tag elements, which have many differences. Mobile work has increasingly become an important part of front-end work. In addition to ordinary project development, the HTML header tag function, especially the functional attributes of meta, link and other tags, is very important. Here is a list of parts, so that everyone can understand the meaning of each tag and corresponding attributes, and write
head tags that meet your needs, which can effectively enhance the page. Availability.
Note: Last year, I compiled the html5 head tags that mobile front-ends must know. With time and browser manufacturers’ upgrades, it seems a bit outdated now. So I reorganized it. Added new content, some outdated tips, and added some instructions for some desktop browsers.
The following are the basic header elements of 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>
Among them
<meta http-equiv="x-ua-compatible" content="ie=edge">
During desktop development, you can let IE browser render the page in the latest mode. For details, please see the Internet Explorer browser section of this article.
If your page is determined to only run in desktop browsers, then
<meta name="viewport" content="width=device-width, initial-scale=1">
Can also be omitted.
DOCTYPE (Document Type), this declaration is located at the forefront of the document, before the html
tag. This tag tells the browser which HTML or XHTML specification the document uses.
Use HTML5 doctype, case-insensitive.
DOCTYPE html>
Declare the character encoding used by the document,
<meta charset="utf-8">
Before html5, the web page would read like this:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
These two are equivalent, you can read more specifically: vs
, so it is recommended to use Short and easy to remember.
More standard way of writing lang attribute http://zhi.hu/XyIa
Simplified Chinese
<html lang="zh-cmn-Hans">
Traditional Chinese
<html lang="zh-cmn-Hant">
In rare cases, it is necessary to add area codes, usually to emphasize the differences in Chinese usage in different regions, for example:
<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>
Why lang="zh-cmn-Hans"
instead of what we usually write lang="zh-CN"
, please read further: Should the statement at the head of the page be lang="zh" or lang =”zh-cn”.
meta tag is an auxiliary tag in the head of HTML. It is located between the and
tags in the head of the HTML document. It provides information invisible to the user. Although this part of information is invisible to users, it is very powerful, especially in today's front-end development work. Setting appropriate meta tags can greatly improve the usability of website pages.
In desktop development, the meta tag is usually used to define page themes for search engine optimization (SEO) and robots, or to define cookies on the user's browser; it can be used to identify the author, set the page format, and mark the content summary and Keywords; you can also set the page to refresh itself based on intervals you define, set RASC content levels, and more.
In mobile development, in addition to the functional settings in the desktop, meta tags also include practical settings such as viewport settings, icons added to the home screen, tab colors, etc. For details, please see the detailed introduction later.
meta tags can be divided into two parts according to different attributes: http-equiv and name attributes.
http-equiv: It is equivalent to the file header function of http. It can return some useful information to the browser to help the browser display the web page content correctly.
name attribute: mainly used to describe web pages, and the corresponding attribute value is content. The content in content is mainly convenient for browsers, search engines and other robots to identify, etc.
<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">
For related detailed instructions, please check:
viewport
can make the layout display better on mobile browsers. I usually write
<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 参数:
<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。
标签(head 头部必须)
<title>your titletitle>
<meta name="keywords" content="your keywords">
<meta name="description" content="your description">
<meta name="author" content="author,email address">
<meta name="robots" content="index,follow">
相关链接:WEB1038 – 标记包含无效的值
通过百度手机打开网页时,百度可能会对你的网页进行转码,脱下你的衣服,往你的身上贴狗皮膏药的广告,为此可在 head 内添加
<meta http-equiv="Cache-Control" content="no-siteapp" />
相关链接:SiteApp 转码声明
下面是不推荐使用的 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 标签,估计大家的第一反应和我一样,就是引入外部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/
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" />
以下是不推荐使用的链接关系:
<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
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 介绍可参考:
QQ 浏览器(X5 内核)同样适用于微信,QQ等第三方应用页面开发。
<meta name="x5-orientation" content="portrait|landscape"> <meta name="x5-fullscreen" content="true"> <meta name="x5-page-mode" content="app">
设置 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 内核进行渲染。
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 浏览器文档
告诉浏览器这个网站对应的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">
<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” 时生效。
<meta name="apple-mobile-web-app-title" content="App Title">
图片自动处理成圆角和高光等效果。
<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中相应的说明。
<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">
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">
注:貌似没测试成功,如果你知道如何正确设置,欢迎留言斧正。
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/
有些时候感觉chrome浏览器下翻译插件很烦人,可以通过下面的代码禁用它。
<meta name="google" value="notranslate" />
有时候,你需要在你的页面上点击某个安卓,直接安卓你的chrome浏览器插件,而不是链接到Chrome webstore 的详细地址再安装,那么你可以使用:
<link rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/APP_ID">
具体使用,请查看:Using Inline Installation
从 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">
//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/
<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">
<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