html如何让文字居中显示?

如题所述

可以用“text-align”属性控制文字的位置,“center”属性值让文字居中显示。

1、新建html文档,在body标签中添加一个div标签,在div标签中添加p标签,这时文字将会在div标签中靠左显示:

2、为div标签设置“text-align”属性,属性值为“center”,这时文本将会居中显示:

3、如果想让文本靠右显示,可以为“text-align”属性设置“right”属性值,这时文本将会靠右显示:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-07

html如何让文字居中显示

通过设置“左右margin”值为“auto”来实现居中的。我们来看个例子就是设置 div 这个块状元素水平居中:

html代码:

<style>div{    border:1px solid red;/*为了显示居中效果明显为 div 设置了边框*/

width:200px;
margin:20px auto;/* margin-left 与 margin-right 设置为 auto */}</style><body>
</div></body>也可以写成:

margin-left:auto;
margin-right:auto;

本回答被网友采纳
第2个回答  2017-12-25
文字外层,放个div,div加个样式,
例如1:
<div class="text">这里是想要居中的文字</div>,
样式表里这样写:
.text{text-align:center;}
例如2:
<div class="text" style=" text-align:center;">这里是想要居中的文字</div>
相似回答