Hello World
簡介
AMP HTML 教學課程 - 學習 AMP HTML 檔案的不同建構區塊。AMP HTML 完全建立在現有的網路技術之上。它透過限制 HTML、CSS 和 JavaScript 的某些部分來實現可靠的效能。為了彌補這些限制,AMP HTML 定義了一組自訂元素,用於呈現超出基本 HTML 的豐富內容。此範例展示了建立有效的 AMP HTML 檔案所需的內容。
必須進行 Doctype 宣告。
<!doctype html>
這會告訴所有人這是一個 AMP 檔案。<html amp>
也適用。
<html ⚡ lang="en">
Head
字元集定義必須是 <head>
標籤的第一個子元素。
<meta charset="utf-8">
AMP runtime 必須作為 <head>
標籤的第二個子元素載入。
<script async src="https://cdn.ampproject.org/v0.js"></script>
AMP HTML 檔案需要一個指向常規 HTML 的標準連結。如果沒有 HTML 版本,它應該指向自身。
<link rel="canonical" href="https://amp.dev.org.tw/documentation/examples/introduction/hello_world/index.html">
AMP HTML 檔案需要一個 viewport 宣告。建議包含 initial-scale=1。
<meta name="viewport" content="width=device-width">
CSS 必須內嵌。
<style amp-custom>
h1 {
color: red;
}
</style>
AMP 樣板。
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
Body
大多數 HTML 標籤可以直接在 AMP HTML 中使用。
Hello World!
<h1>Hello World!</h1>
某些標籤(例如 <img>
標籤)會被等效或稍微增強的自訂 AMP HTML 標籤取代(請參閱規格中的 HTML 標籤)。您可以使用 AMP 驗證器來檢查您的 AMP HTML 檔案是否為有效的 AMP HTML。只需將 #development=1
新增至 AMP URL 即可。驗證錯誤將會列印在 Javascript 主控台中。您可以試試看這個以 AMP 建置的網站。
查看其他範例以瞭解更多關於 AMP 的資訊。
<amp-img src="/static/samples/img/amp.jpg" width="1080" height="610" layout="responsive"></amp-img>
如果此頁面上的說明未能涵蓋您的所有問題,請隨時與其他 AMP 使用者聯繫,討論您的確切使用案例。
前往 Stack Overflow 無法解釋的功能?AMP 專案強烈鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎您針對您特別關注的問題做出一次性的貢獻。
在 GitHub 上編輯範例-
由 @sebastianbenz 撰寫