html怎样使文字居中

lxf2023-03-20 12:08:01

html使文字居中的方法:1、通过“text-align:center;”属性设置文字水平居中;2、通过“line-height:height;”属性设置文字垂直居中。

html怎样使文字居中

本文操作环境:windows7系统、HTML5&&CSS3版、Dell G3电脑。

html文字居中代码具体示例如下:

<!DOCTYPE HTML>
<html>
<head>
 <title>html文字居中测试</title>
 <meta charset="UTF-8">
 <style type="text/css">
 body{background: #ddd;}
 div{width:300px;height:180px;margin:10px auto;color:#fff;font-size:24px;}
 .box1{background: #71a879;text-align: center;}
 .box2{background: #6a8bbc;line-height: 200px;}
 .box3{background: #dea46b;text-align: center;line-height: 200px;}
 </style>
</head>
<body>
<div >html文字水平居中</div>
<div >html文字垂直居中</div>
<div >html文字水平上下居中</div>
</body>
</html>

以上代码效果在本地测试如下图:

html怎样使文字居中

记住这些关键点,在html中,让文字居中的css代码:

1、水平居中:text-align:center;

text-align 属性规定元素中的文本的水平对齐方式

该属性通过指定行框与哪个点对齐,从而设置块级元素内文本的水平对齐方式。通过允许用户代理调整行内容中字母和字之间的间隔,可以支持值 justify;不同用户代理可能会得到不同的结果。

2、垂直居中:line-height:height;

line-height 属性设置行间的距离(行高)。

注释:不允许使用负值。

【推荐学习:html视频教程】

以上就是html怎样使文字居中的详细内容,更多请关注AdminJS其它相关文章!