如何在 HTML 中居中文本?

htmlweb developmentfront end technology更新于 2025/2/18 14:36:17

可以使用 <centre> HTML 标签将 HTML 文档中的文本水平居中。由于 HTML5 已取消此标签,建议您使用 CSS text-align 属性将文档文本水平对齐。<centre> 元素是此标签的另一个名称。

注意 − HTML5 不支持

语法

<p><center> ... </center></p>

以下是示例…

示例

在下面的示例中,我们使用 <center> 标签使文本居中对齐。

<!DOCTYPE html> <html> <body> <p><center>Welcome to TutorialsPoint</center></p> <p>The Best E-Way learning.</p> </body> </html>

输出

执行上述脚本后,"欢迎来到 tutorialspoint"文本将显示在网页的中心。

示例:使用 CSS

在下面的示例中,我们使用 CSS 样式 (text-align:center) 使文本居中显示。

<!DOCTYPE html> <html> <style> h1 {text-align: center;} p {text-align: center;} div {text-align: center;} </style> <body> <h1>TUTORIALSPOINT</h1> <p>The E-Way Learning.</p> <div>Welcome To Tutorials.</div> </body> </html>

输出

当我们运行上述脚本时,文本将显示在网页的中心,如 CSS 属性 text-align: center 所指定。


相关文章


有用资源