建立第一个AMP页面【ytkah英译AMP-1】

想建立您的第一个AMP页面又不知道如何开始?在本教程中,您将学习如何建立一个基本的AMP HTML页面,如何对其进行设置并验证它是否与AMP兼容,以及如何为发布和分发作好准备。html

Create your first AMP page
Not sure how to get started? In this tutorial, you’ll learn how to create a basic AMP HTML page, how to stage it and validate that it’s AMP compliant, and finally how to get it ready for publication and distribution.web

下面的代码是一个不错的amp样板,能够作为学习的开始。复制并将其保存到扩展名为.html的文件中。json

<!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/documentation/guides-and-tutorials/start/create/basic_markup/">
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <script type="application/ld+json">
      {
        "@context": "http://schema.org",
        "@type": "NewsArticle",
        "headline": "Open-source framework for publishing content",
        "datePublished": "2015-10-07T12:02:41Z",
        "image": [
          "logo.jpg"
        ]
      }
    </script>
    <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>Welcome to the mobile web</h1>
  </body>
</html>

  

  body正文中的内容很是简单。可是在页面的头部有不少额外的代码,这些代码可能不会当即显示出来。让咱们来分析一下所需的标记。 app

  使用HTTPS:在建立AMP页面和内容时,应该强烈考虑使用HTTPS协议。虽然AMP文档自己或图像和字体不须要HTTPS,但有许多AMP特性须要HTTPS(例如,视频、iframes等)。要确保您的AMP页面充分利用全部AMP功能,请使用HTTPS协议。async

  必需的标记ide

  • 以<!doctype html>为开始标注html文档类型
  • 用<html ⚡>或<html amp>做为最外层标签,标识页面为AMP内容
  • 包含<head>和<body>标签,(在普通html是可选,但amp中必须包含head 和 body)
  • 包含一个<meta charset="utf-8">标签做为head标签的第一个子标签。表示标识页面的编码。
  • 包含一个<script async src="https://cdn.ampproject.org/v0.js"></script>标签在head标签中。做为一种最佳实践,您应该尽量早地将该脚本引入其中。做用是引入和加载AMP JS库。
  • 在head中包含一个<link rel="canonical" href="$SOME_URL">标签,指向AMP HTML文档的常规HTML版本,或指向自身(若是没有这样的HTML版本存在)。ytkah的理解:canonical是惟一页面url标识,防止由于url中带参数而引发的重复页面
  • 在head中包含<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">标签,咱们建议包含initial-scale=1。表示页面自适应。
  • 在head中包含AMP样板代码。CSS样板,最初隐藏内容,直到AMP JS加载。

  可选的标记布局

  除了基本的需求以外,咱们的示例还在头部包含一个Schema.org定义,这不是AMP的严格要求,但若是想要将内容分发到某些位置(例如,在谷歌搜索头部的花灯切换故事)则须要加这些标记。学习

 

  很好!这就是咱们建立第一个AMP页面所须要的一切,可是固然,在body中尚未进行不少工做。在下一节中,咱们将介绍如何添加基本的像图像,自定义AMP元素,如何自定义样式你的页面和做出一个响应式布局。引入图片amp-img【英译AMP】字体

  参考资料https://amp.dev/documentation/guides-and-tutorials/start/create/basic_markup/?format=websitesui

相关文章
相关标签/搜索