HTML 電子郵件中的 CSS 支援

如要設定傳送至 Gmail 的 HTML 電子郵件樣式,您可以在 HTML 元素上使用內嵌 style 屬性,並在 <head> 元素內使用 <style> 區塊。除非另有說明,否則本文中的 CSS 清理規則適用於網頁版 Gmail、Android 版和 iOS 版 Gmail 行動應用程式中的 Google 帳戶。Gmail 支援部分 CSS 選取器、屬性和媒體查詢,並會在清理過程中,從電子郵件中移除或修改不支援的 CSS 規則和值。

如需支援的 CSS 屬性和媒體查詢完整清單,請參閱「支援的 CSS 屬性」和「支援的媒體查詢」。

本文適用於標準 HTML 電子郵件。如果您使用 AMP for Gmail 建立動態電子郵件,請參閱「AMP for Email 支援的 CSS」。

樣式區塊和內嵌樣式

在 HTML 電子郵件中使用 <style> 區塊時,請遵循下列規定,以免 Gmail 移除樣式:

  • 將 <style> 區塊放在 <head> 中:Gmail 只會從 <head> 元素中擷取 <style> 區塊,並移除放在 <body> 元素中的 <style> 區塊。
  • 不超過 16,384 個字元:所有 <style> 區塊的內容總長度不得超過 16,384 個字元。如果 <style> 區塊導致總數超過此上限,Gmail 會捨棄整個 <style> 區塊和任何後續的 <style> 區塊。
  • 使用有效的 CSS 語法:如果 <style> 區塊含有 CSS 語法錯誤,Gmail 會捨棄整個 <style> 區塊。
  • 請勿使用外部樣式表或匯入字型:Gmail 會移除 <link> 元素、@import 規則和 @font-face 規則。
  • 以原始 MIME HTML 郵件形式傳送:以原始 MIME text/html 郵件形式傳送使用 <style> 區塊的 HTML 電子郵件,例如透過 SMTP 或 Gmail API。詳情請參閱「建立及傳送電子郵件訊息」。將 HTML 貼上或插入 Gmail 撰寫視窗時,系統會移除 <head> 和 <style> 區塊。收件者回覆或轉寄郵件時,Gmail 也會從引用或轉寄的郵件中移除 <head> 和 <style> 區塊,並保留支援的內嵌 style 屬性。
  • 內嵌遭截斷郵件的重要樣式:收件者列印電子郵件或開啟整封郵件時,Gmail 會省略 <style> 區塊,只套用內嵌 style 屬性。HTML 電子郵件應保持精簡,避免內容遭到裁剪,並使用內嵌 style 屬性設定基本版面配置和文字樣式。

CSS 選取器

您可以使用元素、類別和 ID 選取器,以及標準組合器來套用樣式。Gmail 會強制執行下列選取器規則:

  • 虛擬類別和虛擬元素:Gmail 僅支援 :hover 虛擬類別 (在網頁和 Android 上;Gmail 會在 iOS 上移除),並會移除所有其他虛擬類別和虛擬元素。
  • 屬性選取器:Gmail 僅支援 [class~="value"] 屬性選取器,並會移除所有其他屬性選取器。如要在 HTML 元素上保留 value,您也必須在 <style> 中將 .value 參照為類別選取器。
  • 移除無效選取器:如果選取器使用不支援的 CSS3 虛擬類別、虛擬元素或屬性選取器 (例如 :focus、::before 或 [href]),Gmail 會移除該選取器。如果沒有有效的選取器,Gmail 會移除整項規則。:is()、:where()、:not(.a, .b) 或 [class~="value" i] 等較新的選取器語法無法剖析 CSS,並會捨棄整個 <style> 區塊。
  • 類別和 ID 重寫:Gmail 會在 class 和 id 值前面加上郵件專屬 ID,以便區隔電子郵件樣式。Gmail 會移除 HTML 元素上與 <style> 區塊中的 .class 選取器不符的類別名稱。

選取器範例

以下程式碼範例說明如何使用選取器:

<html>
  <head>
    <style>
      .colored {
        color: blue;
      }
      #body {
        font-size: 14px;
      }
    </style>
  </head>
  <body>
    <div id='body'>
      <p>Hi Pierce,</p>
      <p class='colored'>This text is blue.</p>
      <p>Jerry</p>
    </div>
  </body>
</html>

CSS 媒體查詢

您可以使用 CSS @media 查詢,根據使用者的裝置調整電子郵件樣式。Gmail 支援針對螢幕寬度、裝置寬度、螢幕方向和解析度的查詢,但有下列限制:

  • 支援的媒體類型:Gmail 僅支援小寫的 all 和 screen 媒體類型。
  • 支援的邏輯關鍵字:Gmail 僅支援小寫的 and 和 only 關鍵字,不支援 not。
  • 功能運算式語法:每個媒體功能運算式都必須包含小寫功能名稱、冒號和值,例如 (min-width: 500px)。範圍比較運算子 (例如 (width >= 500px)) 會導致 CSS 剖析失敗,並捨棄整個 <style> 區塊。
  • 移除不支援的查詢:Gmail 會移除任何使用不支援媒體類型、關鍵字、功能 (例如 prefers-color-scheme) 或不含值的布林值功能 (例如 (orientation)) 的媒體查詢。
  • Android 寬度重寫:在 Android 上,Gmail 會將媒體查詢中的 width、min-width 和 max-width 重寫為 device-width、min-device-width 和 max-device-width。

媒體查詢範例

以下程式碼範例示範如何使用媒體查詢:

<html>
  <head>
    <style>
      .colored {
        color: blue;
      }
      #body {
        font-size: 14px;
      }
      @media screen and (min-width: 500px) {
        .colored {
          color:red;
        }
      }
    </style>
  </head>
  <body>
    <div id='body'>
      <p>Hi Pierce,</p>
      <p class='colored'>
        This text is blue if the window width is
        below 500px and red otherwise.
      </p>
      <p>Jerry</p>
    </div>
  </body>
</html>

CSS 資源限制

Gmail 會清理 CSS 宣告並修改特定屬性值,確保電子郵件內容顯示在郵件檢視畫面中:

  • 不支援的屬性:Gmail 會移除「支援的 CSS 屬性」中未列出的屬性。常見的不支援屬性包括 position、top、right、bottom、left、z-index、flex、grid、justify-content、align-items、gap、background-attachment、box-shadow、color-scheme、list-style-image、transform、transition 和 CSS 自訂屬性 (--*)。
  • 背景圖片:Gmail 僅支援 background 和 background-image 中的 url() 和 image-set(),使用可公開存取的 http:// 或 https:// 網址 (透過 Google 的圖片 Proxy) 或 cid: 附件參照。Gmail 會將 data: 網址重寫為 url(''),並移除在其他屬性 (例如 list-style) 中使用 url() 的宣告。如果收件者封鎖外部圖片,或 Gmail 將郵件標示為垃圾郵件或可疑郵件,Gmail 會移除 url() 值,直到收件者顯示圖片為止。加入備用 background-color,確保文字清晰可辨。
  • 漸層和 CSS 函式:Gmail 支援 linear-gradient()、radial-gradient()、repeating-linear-gradient()、repeating-radial-gradient() 和數學函式 (calc()、min()、max()、clamp())。Gmail 會移除在 image-set() 內使用 conic-gradient()、repeating-conic-gradient()、light-dark()、image()、element() 或 type() 的宣告。顏色函式 rgb()、rgba()、hsl() 和 hsla() 需要以半形逗號分隔的引數 (例如 rgb(255, 0, 0))。以空格分隔的顏色或圖片函式 (包括 rgb(255 0 0 / 50%)、oklch()、oklab()、lab()、lch()、hwb()、color()、color-mix() 和 cross-fade()) 會導致 CSS 剖析失敗,並捨棄 <style> 區塊或 style 屬性。
  • 版面配置和簡寫值:雖然 Gmail 支援 display (例如 block、inline-block、table 和 none),但會移除隨附的彈性方塊和格線屬性 (flex、grid、justify-content、align-items 和 gap)。請使用 HTML <table> 元素建立多欄版面配置。對於 font-style,Gmail 只接受 normal、italic、oblique 和 inherit (可選用 !important),並會移除其他值。
  • 負邊界和文字縮排:Gmail 會移除 margin、margin-top、margin-right、margin-bottom、margin-left 和 text-indent 的負值 (或包含 - 的運算式,例如 calc(100% - 20px))。
  • 空白字元換行:Gmail 會將 white-space: pre 轉換為 white-space: pre-wrap,以便在郵件檢視畫面中將長行換行。

深色主題

Gmail 在不同用戶端處理深色主題 (深色模式) 的方式不同,且不支援自訂 CSS 深色模式規則:

  • 不支援 prefers-color-scheme 或 color-scheme CSS:Gmail 會在清理期間移除 @media (prefers-color-scheme: dark) (和 light) 查詢和 color-scheme CSS 屬性。
  • 網頁版和行動應用程式的差異:在 Android 和 iOS 版 Gmail 行動應用程式中,深色主題會自動調整 HTML 電子郵件的顏色,將淺色背景變暗,並將深色文字變亮。在網頁版 Gmail 中,深色主題會設定 Gmail 介面的樣式,但預設會在淺色背景上顯示 HTML 電子郵件內文。
  • 停用行動裝置色彩反轉:如要讓 Gmail 行動應用程式中的 Google 帳戶電子郵件保持淺色主題,請在 <head> 中新增 <meta name="color-scheme" content="light only">。您也可以使用 name="supported-color-schemes",且 name 和 content 值都必須是小寫 (例如 content="light only" 或 content="only light")。

支援的 CSS 屬性

下表列出所有通過 Gmail CSS 清理程序的 CSS 屬性,這些屬性須符合用戶端轉譯支援和「CSS 屬性限制」中的值限制。使用篩選器方塊,檢查 Gmail 是否支援特定屬性:

類別 支援的屬性
背景和顏色 background、background-blend-mode、 background-clip、background-color、 background-image、background-origin、 background-position、background-repeat、 background-size、color、 isolation、mix-blend-mode、 opacity
框線和外框 border、border-bottom、 border-bottom-color、 border-bottom-left-radius、 border-bottom-right-radius、 border-bottom-style、border-bottom-width、 border-color、border-left、 border-left-color、border-left-style、 border-left-width、border-radius、 border-right、border-right-color、 border-right-style、border-right-width、 border-style、border-top、 border-top-color、border-top-left-radius、 border-top-right-radius、border-top-style、 border-top-width、border-width、 outline、outline-color、 outline-style、outline-width
方塊模型、大小和間距 box-sizing、clear、display、 float、height、margin、 margin-bottom、margin-left、 margin-right、margin-top、 max-height、max-width、 min-height、min-width、 overflow、overflow-x、 overflow-y、padding、 padding-bottom、padding-left、 padding-right、padding-top、 width
字體排版和字型 font、font-family、font-feature-settings、font-kerning、font-size、font-size-adjust、font-stretch、font-style、font-synthesis、font-variant、font-variant-alternates、font-variant-caps、font-variant-east-asian、font-variant-ligatures、font-variant-numeric、font-weight、letter-spacing、line-height、quotes
文字版面配置和裝飾 direction、line-break、 text-align、text-align-last、 text-autospace、text-combine-upright、 text-decoration、text-decoration-color、 text-decoration-line、 text-decoration-skip、 text-decoration-style、text-emphasis、 text-emphasis-color、text-emphasis-style、 text-indent、text-justify、 text-orientation、text-overflow、 text-transform、text-underline-position、 unicode-bidi、vertical-align、 white-space、word-break、 word-spacing、word-wrap、 writing-mode
表格和清單 border-collapse、border-spacing、 caption-side、empty-cells、 list-style、list-style-position、 list-style-type、marker-offset、 table-layout
欄和分頁符號 break-after、break-before、 break-inside、column-count、 column-fill、column-gap、 column-rule、column-rule-color、 column-rule-style、column-rule-width、 column-span、column-width、 columns、page-break-after、 page-break-before、page-break-inside
圖片和替換元素 image-orientation、image-resolution、object-fit、object-position
舊版和語音 (聽覺) azimuth、elevation、ime-mode、layout-flow、layout-grid、layout-grid-char、layout-grid-char-spacing、layout-grid-line、layout-grid-mode、layout-grid-type、pause、pause-after、pause-before、pitch、pitch-range、richness、speak、speak-header、speak-numeral、speak-punctuation、speech-rate、stress、text-kashida-space、voice-family、zoom

支援的媒體查詢

下表列出 Gmail 支援的 @media 類型、功能和邏輯關鍵字,但須遵守 CSS 媒體查詢中的規則:

元件 支援的值 附註
媒體類型 all、screen 僅限小寫;每項查詢最多只能有一個媒體類型。
媒體功能 width、min-width、max-width、device-width、min-device-width、max-device-width、orientation、resolution、min-resolution、max-resolution 只能使用小寫 (feature: value) 語法。在 Android 上,Gmail 會將 width、min-width 和 max-width 重寫為 device-width 對等項目。
邏輯關鍵字 and、only 僅限小寫;Gmail 不支援 not。