可以用“text-align”属性控制文字的位置,“center”属性值让文字居中显示。
1、新建html文档,在body标签中添加一个div标签,在div标签中添加p标签,这时文字将会在div标签中靠左显示:
2、为div标签设置“text-align”属性,属性值为“center”,这时文本将会居中显示:
3、如果想让文本靠右显示,可以为“text-align”属性设置“right”属性值,这时文本将会靠右显示:
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;