AMP
  • 網站

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>` 標籤的第一個子元素。

<meta charset="utf-8">

AMP 執行階段必須載入為 `<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>

內文

大部分 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 上編輯範例