此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

<center>:中央對齊文字元素

Deprecated

Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.

<center> HTML 元素是一個區塊級元素,它將其區塊級或內聯內容在其包含元素中水平居中顯示。通常,容器是 <body>,但不是必需的。

在 HTML 4(和 XHTML 1)中,此標籤已被棄用,取而代之的是 CSS 的 text-align 屬性,可以應用於 <div> 元素或個別的 <p>。要將區塊居中,請使用其他 CSS 屬性,如 margin-left 和 margin-right,並將它們設置為 auto(或將 margin 設置為 0 auto)。

DOM 介面

此元素實現了 HTMLElement 介面。

範例 1

html
<center>
  This text will be centered.
  <p>So will this paragraph.</p>
</center>

結果

範例 2(CSS 替代方案)

html
<div style="text-align:center">
  This text will be centered.
  <p>So will this paragraph.</p>
</div>

結果

範例 3(CSS 替代方案)

html
<p style="text-align:center">
  This line will be centered.<br />
  And so will this line.
</p>

結果

備註:將 text-align:center 應用於 <div> 或 <p> 元素會將這些元素的內容置中,同時保持它們的整體尺寸不變。

規範

Specification
HTML
# center

瀏覽器相容性

參見