AMP

入門程式碼

以下標記為基本的 AMP 頁面。

<!doctype html>
<html amp lang="en">
  <head>
    <meta charset="utf-8">
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <title>Hello, AMPs</title>
    <link rel="canonical" href="https://amp.dev.org.tw/documentation/guides-and-tutorials/start/create/basic_markup/">
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <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>
  </head>
  <body>
    <h1 id="hello">Hello AMPHTML World!</h1>
  </body>
</html>
在 playground (遊樂場) 中開啟這段程式碼

內文內容相當簡單明瞭,但在 head (頁首) 中有一些額外程式碼。下表將詳細說明必要的標記。每個 AMP HTML 文件都必須:

規則 說明
<!doctype html> doctype 開始。 HTML 的標準。
包含頂層 <html ⚡><html amp> 標記。 將頁面識別為 AMP 內容。
包含 <head><body> 標記。 雖然在 HTML 中為選用項目,但在 AMP 中為必要項目。
<head> 標記之後立即包含 <meta charset="utf-8"> 標記。 識別頁面的編碼。
<head> 標記內包含 <script async src="https://cdn.ampproject.org/v0.js"></script> 標記。最佳做法是盡可能提早加入指令碼。 包含並載入 AMP JS 程式庫。
<head> 內包含 <link rel="canonical" href="$SOME_URL"> 標記。 href 屬性應指向頁面本身。此區段基於舊版原因而存在。
<head> 標記內包含 <meta name="viewport" content="width=device-width" /> 標記。 指定回應式視viewport。如要瞭解詳情,請參閱建立回應式 AMP 頁面
<head> 標記中包含 AMP 樣板程式碼 CSS 樣板,用於在載入 AMP JS 之前先隱藏內容。

按一下上方範例下方的「在 playground (遊樂場) 中開啟這段程式碼」,開始建構您的頁面!

如果您想快速跳過,可以按一下這裡查看完成的教學課程程式碼!