
- Google AMP 教程
- Google AMP - 首頁
- Google AMP - 概述
- Google AMP - 簡介
- Google AMP - 圖片
- Google AMP - 表單
- Google AMP - Iframes
- Google AMP - 影片
- Google AMP - 按鈕
- Google AMP - Timeago
- Google AMP - Mathml
- Google AMP - 自適應文字
- Google AMP - 日期倒計時
- Google AMP - 日期選擇器
- Google AMP - 故事
- Google AMP - 選擇器
- Google AMP - 連結
- Google AMP - 字型
- Google AMP - 列表
- Google AMP - 使用者通知
- Google AMP - 下一頁
- Google AMP - 屬性
- 樣式和自定義 CSS
- Google AMP - 動態 CSS 類
- Google AMP - 操作和事件
- Google AMP - 動畫
- Google AMP - 資料繫結
- Google AMP - 佈局
- Google AMP - 廣告
- Google AMP - 分析
- Google AMP - 社交小部件
- Google AMP - 媒體
- HTML 頁面到 AMP 頁面
- Google AMP - 基本語法
- Google AMP - 驗證
- Google AMP - 快取
- Google AMP - 自定義 JavaScript
- Google AMP - CORS
- Google AMP 有用資源
- Google AMP - 快速指南
- Google AMP - 有用資源
- Google AMP - 討論
Google AMP - 樣式和自定義 CSS
AMP 在螢幕上渲染頁面時會經過仔細的考慮。載入的頁面將包含影像、影片、iframe 等,這些都需要進行 HTTP 請求。因此,HTTP 請求會被延遲,以便先顯示頁面內容,併為影像、影片、iframe 的載入預留必要空間。
AMP 具有佔位符、回退、srcset 和佈局屬性等功能,可以使頁面具有響應性,並確保頁面內容不會被打亂。在本章中,我們將詳細討論所有這些內容。
AMP 樣式標籤
AMP 具有一個樣式標籤,上面帶有 **amp-custom** 屬性,如下所示:
<style amp-custom> button{ background-color: #ACAD5C; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; float: left; } amp-img { border: 1px solid black; border-radius: 4px; padding: 5px; } p { padding: 1rem; font-size:25px; } largeText { font-size:30px; background-color:red; } </style>
它主要用於編寫頁面所需的自定義 CSS。請不要忘記新增 **amp-custom** 屬性;否則,它將無法透過 AMP 驗證,如下所示:

AMP 還支援 HTML 元素的內聯 CSS,如下所示:
<div style = "color:green;margin-left:30px;"> Welcome to TutorialsPoint</p>
外部樣式表標籤
AMP 不支援外部樣式表,並在進行 AMP 驗證時會失敗。
示例
<!doctype html> <html amp lang = "en"> <head> <meta charset = "utf-8"> <script async src = "https://cdn.ampproject.org/v0.js"></script> <title>Google AMP - Dynamic Css Classes</title> <link rel = "canonical" href = " http://example.ampproject.org/article-metadata.html"> <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> <script async custom-element = "amp-bind" src = " https://cdn.ampproject.org/v0/amp-bind-0.1.js"> </script> <script async custom-element = "amp-dynamic-css-classes" src = "https://cdn.ampproject.org/v0/amp-dynamic-css-classes-0.1.js"> </script> <link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/materialize/0.9 8.0/css/materialize.min.css"> <style amp-custom> p { padding: 1rem; font-size:25px; } </style> </head> <body> <h3>Google AMP - Dynamic Css Classes</h3> <div style = "color:green;margin-left:30px;"> Welcome to TutorialsPoint</p> </body> </html>
使用 AMP 驗證器 驗證時,我們會收到以下錯誤。

為了使頁面中的元素能夠響應式地顯示,AMP 元素需要指定元素在頁面上佔據的寬度和高度。新增 layout = “responsive” 將使元素在頁面上保持縱橫比的同時具有響應性。
佈局屬性的詳細資訊將在 **Google AMP – 佈局** 章節中詳細討論。
廣告