AMP

重要事項:本文件不適用於您目前選取的格式 廣告

amp-fit-text

實驗性
Bento

說明

擴展或縮小字型大小,以將內容放入指定的空間。

 

必要指令碼

<script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-1.0.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-size22,因此文字較小,但仍符合空間大小

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 中開啟此程式碼片段

從 0.1 版遷移

0.1 不同的是,實驗性 1.0 版本的 amp-fit-text 不會將邊界和邊框大小視為有助於總 fit-text 涵蓋範圍區域。

在有效的 AMP 文件之外獨立使用

Bento 可讓您在非 AMP 頁面中使用 AMP 元件,而無需承諾完全有效的 AMP。您可以採用這些元件,並將其置於不支援 AMP 的架構和 CMS 的實作中。請參閱我們的指南 在非 AMP 頁面中使用 AMP 元件,以瞭解詳情。

若要尋找獨立版本的 amp-fit-text,請參閱 bento-fit-text

溢位內容

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

在以下範例中,我們指定了 min-font-size40,並在 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 中開啟此程式碼片段

溢位內容的可及性考量

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

屬性

媒體查詢

<amp-fit-text> 的屬性可以設定為根據 媒體查詢 使用不同的選項。

min-font-size

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

max-font-size

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

通用屬性

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

樣式設定

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

需要更多協助嗎?

您已閱讀本文件十幾次,但它並未真正涵蓋您的所有問題?也許其他人也有同樣的感覺:在 Stack Overflow 上與他們聯繫。

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

AMP 專案強烈鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎針對您特別感興趣的問題提供一次性貢獻。

前往 GitHub