<meta>標簽可提供頁面資料的元資訊(meta information),
<meta>標簽位于檔案頭部,不包含內容,其屬性定義了與檔案關聯的鍵值對,
簡單來說,<meta>就是檔案資訊描述的標簽,我們從以下幾個方面來看看<meta>的實際用途,比如:
- 搜索引擎優化(SEO)
- 定義頁面使用語言
- 自動重繪并指向新的頁面
- 實作網頁轉換時的動態效果
- 控制頁面緩沖
- 網頁定級評價
- 控制網頁顯示的視窗
一、meta標簽的屬性
1、charset屬性
該特性申明檔案的字符編碼但,該宣告可以被任何一個元素的 lang 特性的值覆寫,推薦使用utf-8國際通用編碼,如:
<meta charset="utf-8">
2、content屬性
此屬性包含http-equiv 或name 屬性的值,具體取決于所使用的值,
3、http-equiv屬性
這個列舉屬性定義了能改變服務器和用戶引擎行為的編譯,這個編譯值使用content 來定義,如下:
-
content-language(過時)
指定頁面的默認語言,推薦使用
<html>設定全域lang屬性來代替, -
content-security-policy:內容安全策略
它允許頁面作者定義當前頁的 內容策略, 內容策略主要指定允許的服務器源和腳本端點,這有助于防止跨站點腳本攻擊,
-
content-type(過時)
設定檔案字符編碼,推薦使用meat的
charset屬性代替, -
default-style:默認樣式(首選樣式)
該屬性指定了檔案首選的默認樣式,content的值必須包含
link元素的標題、href屬性鏈接到CSS樣式表或包含CSS樣式表的``元素的標題, -
refresh:頁面重繪時間
如果
content值為正整數,則設定為指定時間間隔重繪當前頁面,值為正整數后跟字串的形式則指定時間間隔內跳轉到指定頁面,以上時間單位為秒,<!-- Redirect page after 3 seconds --> <meta http-equiv="refresh" content="3;url=http://www.cnblogs.com/fzz9/"> <meta http-equiv="refresh" content="3 <http://www.cnblogs.com/fzz9/>"> -
set-cookie(過時)
為頁面設定cookie,請改用HTTP的Set-Cookie頭部,
4、name屬性
該屬性定義檔案級元資料的名稱,如果以下其中一個屬性設定了itemprop, http-equiv or charset ,就不能再設定這個屬性了,
name屬性可包含以下值(不完整):
-
application-name
定義正運行在該網頁上的網路應用名稱,
-
author
檔案的作者名稱,自由格式,
-
description
其中包含頁面內容的簡短和精確的描述, 一些瀏覽器,如Firefox和Opera,將其用作書簽頁面的默認描述,
-
generator
包含生成頁面的軟體的標示符,
-
keywords
包含與逗號分隔的頁面內容相關的單詞,
-
referrer(實驗中的屬性)
控制所有從該檔案發出的 HTTP 請求中HTTP Referer首部的內容,
-
creator
定義檔案創建者的名稱,與author類似,
-
googlebot
屬性
robots的同義詞,該屬性只有googleBot(谷歌索引抓取工具)才會使用, -
publisher
自由格式定義檔案發布者的名稱,
-
robots
定義搜索引擎爬蟲與頁面應具有的行為, 它是以逗號分隔的值串列,可選值:
Untitled
-
viewport(供移動設備使用)
該屬性提供有關視口初始大小的提示,僅供移動設備使用,
可選值為:
Untitled
我們常使用下面這個設定來適配移動端頁面:
<meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0,user-scalable=no">
二、meta標簽的實際使用
1、各大網站首頁的meta
我們可以從各大網站的頁面查看他們對meta的使用,比如:
-
淘寶首頁
<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="renderer" content="webkit"> <meta name="spm-id" content="a21bo"> <meta name="description" content="淘寶網 - 亞洲較大的網上交易平臺,提供各類服飾、美容、家居、數碼、話費/點卡充值… 數億優質商品,同時提供擔保交易(先識訓后付款)等安全交易保障服務,并由商家提供退貨承諾、破損補寄等消費者保障服務,讓你安心享受網上購物樂趣!"> <meta name="aplus-xplug" content="NONE"> <meta name="keyword" content="淘寶,掏寶,網上購物,C2C,在線交易,交易市場,網上交易,交易市場,網上買,網上賣,購物網站,團購,網上貿易,安全購物,電子商務,放心買,供應,買賣資訊,網店,一口價,拍賣,網上開店,網路購物,打折,免費開店,網購,頻道,店鋪"> -
微博首頁
<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="initial-scale=1,minimum-scale=1"> <meta content="隨時隨地發現新鮮事!微博帶你欣賞世界上每一個精彩瞬間,了解每一個幕后故事,分享你想表達的,讓全世界都能聽到你的心聲!" name="description"> -
github首頁
<meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <meta name="description" content="GitHub is where people build software. More than 36 million people use GitHub to discover, fork, and contribute to over 100 million projects."> <meta property="fb:app_id" content="1401488693436528"> <meta property="og:url" content="<https://github.com>"> <meta property="og:site_name" content="GitHub"> <meta property="og:title" content="Build software better, together"> <meta property="og:description" content="GitHub is where people build software. More than 36 million people use GitHub to discover, fork, and contribute to over 100 million projects."> <meta property="og:image" content="<https://github.githubassets.com/images/modules/open_graph/github-logo.png>"> <meta property="og:image:type" content="image/png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="1200"> <meta property="og:image" content="<https://github.githubassets.com/images/modules/open_graph/github-mark.png>"> <meta property="og:image:type" content="image/png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="620"> <meta property="og:image" content="<https://github.githubassets.com/images/modules/open_graph/github-octocat.png>"> <meta property="og:image:type" content="image/png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="620"> <meta property="twitter:site" content="github"> <meta property="twitter:site:id" content="13334762"> <meta property="twitter:creator" content="github"> <meta property="twitter:creator:id" content="13334762"> <meta property="twitter:card" content="summary_large_image"> <meta property="twitter:title" content="GitHub"> <meta property="twitter:description" content="GitHub is where people build software. More than 36 million people use GitHub to discover, fork, and contribute to over 100 million projects."> <meta property="twitter:image:src" content="<https://github.githubassets.com/images/modules/open_graph/github-logo.png>"> <meta property="twitter:image:width" content="1200"> <meta property="twitter:image:height" content="1200"> <meta name="pjax-timeout" content="1000"> <meta name="request-id" content="2ED9:0DCF:16C7FB:216F9D:5CC52252" data-pjax-transient=""> <meta name="selected-link" value="/" data-pjax-transient=""> <meta name="google-site-verification" content="KT5gs8h0wvaagLKAVWq8bbeNwnZZK1r1XQysX3xurLU"> <meta name="google-site-verification" content="ZzhVyEFwb7w3e0-uOTltm8Jsck2F5StVihD0exw2fsA"> <meta name="google-site-verification" content="GXs5KoUUkNCoaAZn7wPN-t01Pywp9M3sEjnt_3_ZWPc"> <meta name="octolytics-host" content="collector.githubapp.com"> <meta name="octolytics-app-id" content="github"> <meta name="octolytics-event-url" content="<https://collector.githubapp.com/github-external/browser_event>"> <meta name="octolytics-dimension-request_id" content="2ED9:0DCF:16C7FB:216F9D:5CC52252"> <meta name="octolytics-dimension-region_edge" content="ap-southeast-1"> <meta name="octolytics-dimension-region_render" content="iad"> <meta name="octolytics-actor-id" content="30432876"> <meta name="octolytics-actor-login" content="fongzhizhi"> <meta name="octolytics-actor-hash" content="7746069481520fac3723891a8bbfada265f7541285b3ba8dce727e3dd67e5af2"> <meta name="analytics-location" content="/dashboard" data-pjax-transient="true"> <meta name="google-analytics" content="UA-3769691-2"> <meta class="js-ga-set" name="userId" content="d47384dc9e738b855285fea4b0c3b639"> <meta class="js-ga-set" name="dimension1" content="Logged In"> <meta name="hostname" content="github.com"> <meta name="user-login" content="fongzhizhi"> <meta name="expected-hostname" content="github.com"> <meta name="js-proxy-site-detection-payload" content="NDE0ZmM0YmRlYTcyMmYxMmM3OTcxN2NkMmJlMDFkZmM2ODEwNjA5YjI2YzIxNjlhNTVjODQ0NjJlY2Q4NWI0Ynx7InJlbW90ZV9hZGRyZXNzIjoiMTE2LjI0Ljk5LjEwMSIsInJlcXVlc3RfaWQiOiIyRUQ5OjBEQ0Y6MTZDN0ZCOjIxNkY5RDo1Q0M1MjI1MiIsInRpbWVzdGFtcCI6MTU1NjQyMzI1NCwiaG9zdCI6ImdpdGh1Yi5jb20ifQ=="> <meta name="enabled-features" content="SPONSORS_TIERS,UNIVERSE_BANNER,MARKETPLACE_INVOICED_BILLING,MARKETPLACE_SOCIAL_PROOF_CUSTOMERS,MARKETPLACE_TRENDING_SOCIAL_PROOF,MARKETPLACE_RECOMMENDATIONS,NOTIFY_ON_BLOCK,RELATED_ISSUES"> <meta name="html-safe-nonce" content="df3a1b9bcd5d110071c16fe060650098953e13ef"> <meta http-equiv="x-pjax-version" content="5b8034c4a9fa0ce9faea05e0eaa63e01"> <meta name="browser-stats-url" content="<https://api.github.com/_private/browser/stats>"> <meta name="browser-errors-url" content="<https://api.github.com/_private/browser/errors>"> <meta name="theme-color" content="#1e2327"> -
stackoverflow首頁
<meta name="description" content="S tack Overflow is the largest, most trusted online community for developers to learn, share their programming knowledge, and build their careers."> <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0"> <meta property="og:type" content="website"> <meta property="og:url" content="<https://stackoverflow.com/>"> <meta property="og:site_name" content="Stack Overflow"> <meta property="og:image" itemprop="image primaryImageOfPage" content="<https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded>"> <meta name="twitter:card" content="summary"> <meta name="twitter:domain" content="stackoverflow.com"> <meta name="twitter:title" property="og:title" itemprop="name" content="Stack Overflow - Where Developers Learn, Share, & Build Careers"> <meta name="twitter:description" property="og:description" itemprop="description" content="Stack Overflow | The World’s Largest Online Community for Developers">
2、meta一些的具體作用
-
瀏覽器兼容性設定
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />當指定的content值為IE=edge,chrome=1時,優先使用 IE 最新版本和 Chrome,
假定客戶端安裝了Google Chrome Frame,則在IE中使用chrome的渲染引擎來渲染頁面,否則,將會使用客戶端IE最高的標準模式對頁面進行渲染,
還能這樣設定:
<!-- 使用IE6 --> <meta http-equiv="X-UA-Compatible" content="IE=6" > -
瀏覽器內核控制
很多瀏覽器是雙核的,比如
webkit內核高速瀏覽,IE內核兼容網頁和舊版網站,除了瀏覽器的默認設定外,我們還可通過設定meta標簽的屬性來設定當前頁面的內核渲染模式,<meta name="renderer" content="webkit|ie-comp|ie-stand"> -
站點適配
主要是移動端手機協議選擇,主要用于PC-手機頁的對應關系:
<!-- [wml|xhtml|html5] 根據手機頁的協議語言,選擇其中一種; url="url" 后者代表當前PC頁所對應的手機頁URL,兩者必須是一一對應關系, --> <meta name="mobile-agent" content="format=[wml|xhtml|html5]; url=url"> -
SEO(搜索引擎優化)
根據搜索引擎爬取網頁內容的特點,我們可以對頁面資訊進行定義,以便更好地讓搜索引擎識別到,以此來提高網站或頁面的識別度,增加搜索時的排名等,
-
頁面關鍵字:
keyword這是name屬性的屬性值,我們可以使用該屬性值對頁面進行關鍵字描述,以便于搜索引擎的準確抓取,
-
頁面描述:
description同
keyword類似,該屬性值可對頁面進行總結性描述, -
搜索引擎的搜素方式相關設定:
robots
-
3、總結
meta標簽規定了檔案的屬性資訊,瀏覽器在處理該頁面時就會先讀取meta中的資訊再進行處理,不同的瀏覽器或設備有不同的屬性鍵值對,根據具體使用查詢檔案或使用案例進行設定,
參考地址
https://www.cnblogs.com/fzz9/p/10783692.html
https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/meta
https://huanghui8030.github.io/html/meta-sydq.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/257402.html
標籤:其他
