AMP

amp-fit-text

 
您現在可以在有效的 AMP 文件之外使用此元件,方法是使用此元件的 Bento 版本。請在 Bento 指南中瞭解更多資訊。

描述

展開或縮小字型大小,以將內容調整到給定的空間內。

 

必要指令碼

<script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>

用法

展開或縮小其字型大小,以將內容調整到給定的空間內。

`amp-fit-text` 元件可讓您管理指定區域內文字的大小和調整方式。對於 `amp-fit-text` 元素中包含的內容,`amp-fit-text` 元件會找出最適合的字型大小,以將所有內容調整到可用空間內。`amp-fit-text` 預期的內容是文字或其他行內內容,但也可能包含非行內內容。

在以下範例中,`<amp-fit-text>` 元素巢狀於 300x300 藍色 `div` 區塊中。對於 `<amp-fit-text>` 元素,我們指定了 `responsive` 版面配置。因此,文字會根據 `<amp-fit-text>` 元素的寬度和高度 (200x200) 提供的長寬比進行回應式縮放,但文字不會超過其父元素的大小。

Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
<div style="width: 300px; height: 300px; background:#005AF0; color: #FFF;">
  <amp-fit-text width="200" height="200" layout="responsive">
    Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
    inermis reprehendunt.
  </amp-fit-text>
</div>
在 playground 中開啟此程式碼片段

以下範例與上述範例類似,但在此範例中,我們指定 `max-font-size` 為 `22`,因此文字較小,但仍適合空間

Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
<div style="width: 300px; height: 300px; background:#005AF0; color: #FFF;">
  <amp-fit-text width="200" height="200" layout="responsive" max-font-size="22">
    Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
    inermis reprehendunt.
  </amp-fit-text>
</div>
在 playground 中開啟此程式碼片段

溢出的內容

如果 `amp-fit-text` 的內容溢出可用空間,即使指定了 `min-font-size`,溢出的內容仍會被截斷並隱藏。以 WebKit 和 Blink 為基礎的瀏覽器會針對溢出的內容顯示省略符號。

在以下範例中,我們指定了 `min-font-size` 為 `40`,並在 `amp-fit-text` 元素內新增了更多內容。這會導致內容超過其固定區塊父元素的大小,因此文字會被截斷以符合容器。

Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt. Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt
<div style="width: 300px; height: 300px; background:#005AF0; color: #FFF;">
  <amp-fit-text width="200" height="200" layout="responsive" min-font-size="40">
    Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque
    inermis reprehendunt. Lorem ipsum dolor sit amet, has nisl nihil convenire
    et, vim at aeque inermis reprehendunt
  </amp-fit-text>
</div>
在 playground 中開啟此程式碼片段

溢出內容的可及性考量

雖然溢出的內容在*視覺上*會被截斷以符合容器,但請注意,它仍然存在於文件中。請勿依賴溢出行為來簡單地將大量內容「塞入」您的網頁中 - 雖然在視覺上看起來可能很合適,但可能會導致網頁對輔助技術 (例如螢幕閱讀器) 的使用者來說過於冗長,因為對於這些使用者來說,所有截斷的內容仍然會被完整讀取/宣告。

屬性

min-font-size

以像素為單位指定 `amp-fit-text` 可以使用的最小字型大小 (整數)。

max-font-size

以像素為單位指定 `amp-fit-text` 可以使用的最大字型大小 (整數)。

通用屬性

此元素包含擴充至 AMP 元件的通用屬性。

樣式設定

您可以使用標準 CSS 設定 `amp-fit-text` 的樣式。特別是,您可以使用 `text-align`、`font-weight`、`color` 和許多其他 CSS 屬性,但主要例外是 `font-size`。

驗證

請參閱 AMP 驗證器規格中的 `amp-fit-text` 規則。

需要更多協助嗎?

您已閱讀此文件十幾次,但它仍然沒有涵蓋您的所有問題嗎?也許其他人也有同樣的感覺:請在 Stack Overflow 上與他們聯繫。

前往 Stack Overflow
發現錯誤或缺少功能嗎?

AMP 專案大力鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的持續參與者,但我們也歡迎針對您特別熱衷的問題提供一次性貢獻。

前往 GitHub