在 HTML 中,使用 style 的 text-align 属性可以将文本居中对齐,可以通过在 style 属性中设置 text-align 为 "center" 来实现。

HTML 文字居中代码
在 HTML 中,使用 text-align 属性可以将文本设置为居中对齐。
代码:
<code class="html"><p style="text-align: center;">居中的文字</p></code>
详细说明:
立即学习“前端免费学习笔记(深入)”;
- 在
<p>标签中添加style属性。 - 在
style属性内,设置text-align为"center"。
示例:
<code class="html"><html>
<head>
<style>
p {
text-align: center;
}
</style>
</head>
<body>
<p>居中的文字</p>
</body>
</html></code>在这个示例中,整个 <p> 元素内的所有文本都将居中对齐。
提示:
-
text-align属性还可以设置为"left"(左对齐)或"right"(右对齐)。 - 也可以使用 CSS 类来设置文本对齐,例如:
<code class="html"><p class="center">居中的文字</p></code>
<code class="css">.center {
text-align: center;
}</code>











