dominant-baseline

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流瀏覽器均已支援。

dominant-baseline CSS 屬性指定用於對齊框的文字和行內級內容的特定基線。它還指示在框的對齊上下文中參與基線對齊的任何框的預設對齊基線。如果存在,它將覆蓋形狀的 dominant-baseline 屬性。

基線選自字型基線表。如果名義字型中沒有基線表,或者基線表中缺少所需基線的條目,則瀏覽器可以使用啟發式方法來確定所需基線的位置。

dominant-baseline 屬性用於確定或重新確定一個縮放基線表(scaled-baseline-table)。縮放基線表是一個包含三個元件的複合值:

  1. 主導基線的基線識別符號,
  2. 基線表,以及
  3. 基線表字體大小。

dominant-baseline 的某些值會重新確定所有三個值。其他值僅重新建立基線表字體大小。當初始值 auto 會產生不希望的結果時,可以使用此屬性顯式設定所需的縮放基線表。

注意: dominant-baseline 屬性僅對 <text><textPath><tspan> SVG 元素起作用。

語法

css
/* Initial value */
dominant-baseline: auto;

/* Keyword values */
dominant-baseline: alphabetic;
dominant-baseline: central;
dominant-baseline: hanging;
dominant-baseline: ideographic;
dominant-baseline: mathematical;
dominant-baseline: middle;
dominant-baseline: text-bottom;
dominant-baseline: text-top;

/* Global values */
dominant-baseline: inherit;
dominant-baseline: initial;
dominant-baseline: revert;
dominant-baseline: revert-layer;
dominant-baseline: unset;

auto

如果此屬性應用於 <text> 元素,則計算值取決於 writing-mode 屬性的值。

如果 writing-mode 是水平的,則 dominant-baseline 元件的值為 alphabetic。否則,如果 writing-mode 是垂直的,則 dominant-baseline 元件的值為 central

如果此屬性應用於 <tspan><textPath> 元素,則 dominant-baseline 和 baseline-table 元件保持與父文字內容元素相同。

如果計算的 baseline-shift 值實際改變了基線,則 baseline-table 字型大小元件被設定為 dominant-baseline 屬性所在元素的 font-size 屬性的值;否則,baseline-table 字型大小保持與該元素相同。

如果沒有父文字內容元素,則縮放基線表值將按照 <text> 元素的構造方式進行構造。

alphabetic

主導基線的基線識別符號設定為 alphabetic,派生基線表使用字型中的 alphabetic 基線表構建,並且基線表字體大小更改為元素的 font-size SVG 屬性或 CSS font-size(如果已設定)的值。

central

主導基線的基線識別符號設定為 central。派生基線表根據字型基線表中定義的基線構建。該字型基線表按照以下基線表名稱的優先順序順序選擇:ideographicalphabetichangingmathematical。基線表字體大小更改為元素的 font-size SVG 屬性或 CSS font-size(如果已設定)的值。

hanging

主導基線的基線識別符號設定為 hanging,派生基線表使用字型中的 hanging 基線表構建,並且基線表字體大小更改為該元素的 font-size SVG 屬性或 font-size CSS 屬性的值。

ideographic

主導基線的基線識別符號設定為 ideographic,派生基線表使用字型中的 ideographic 基線表構建,並且基線表字體大小更改為元素的 font-size SVG 屬性或 CSS font-size(如果已設定)的值。

mathematical

主導基線的基線識別符號設定為 mathematical,派生基線表使用字型中的 mathematical 基線表構建,並且基線表字體大小更改為元素的 font-size SVG 屬性或 CSS font-size(如果已設定)的值。

middle

主導基線的基線識別符號設定為 middle。派生基線表根據字型中基線表中定義的基線構建。該字型基線表按照以下基線表名稱的優先順序順序選擇:ideographicalphabetichangingmathematical。基線表字體大小更改為元素的 font-size SVG 屬性或 CSS font-size(如果已設定)的值。

text-bottom

行下邊緣用作基線,這通常是字型 em 框的下邊緣。

text-top

行上邊緣用作基線,這通常是字型 em 框的上邊緣。

正式定義

初始值auto
應用於塊容器、彈性容器、網格容器、行內框、表格行和 SVG 文字內容元素
繼承性
計算值同指定值
動畫型別離散

正式語法

dominant-baseline = 
auto |
text-bottom |
alphabetic |
ideographic |
middle |
central |
mathematical |
hanging |
text-top

示例

html
<svg viewBox="0 0 450 160" width="700" height="200">
  <text x="50" y="20">alphabetic</text>
  <text x="50" y="60">central</text>
  <text x="50" y="100">hanging</text>
  <text x="50" y="140">ideographic</text>
  <text x="250" y="20">mathematical</text>
  <text x="250" y="60">middle</text>
  <text x="250" y="100">text-bottom</text>
  <text x="250" y="140">text-top</text>
  <path
    d="M   0,20 l 400,0
       m -400,40 l 400,0
       m -400,40 l 400,0
       m -400,40 l 400,0"
    stroke="grey" />
  <text x="0" y="20" fill="red">auto</text>
  <text x="0" y="60" fill="red">auto</text>
  <text x="0" y="100" fill="red">auto</text>
  <text x="0" y="140" fill="red">auto</text>
</svg>
css
text {
  font-size: 20px;
}
text:nth-of-type(1) {
  dominant-baseline: alphabetic;
}
text:nth-of-type(2) {
  dominant-baseline: central;
}
text:nth-of-type(3) {
  dominant-baseline: hanging;
}
text:nth-of-type(4) {
  dominant-baseline: ideographic;
}
text:nth-of-type(5) {
  dominant-baseline: mathematical;
}
text:nth-of-type(6) {
  dominant-baseline: middle;
}
text:nth-of-type(7) {
  dominant-baseline: text-bottom;
}
text:nth-of-type(8) {
  dominant-baseline: text-top;
}

規範

規範
CSS 內聯佈局模組級別 3
# dominant-baseline-property
Scalable Vector Graphics (SVG) 2
# DominantBaselineProperty

瀏覽器相容性

另見