本文摘自PHP中文网,作者青灯夜游,侵删。
html文字居中的样式:1、水平居中样式为“text-align: center;”;2、文字垂直居中样式“line-height:px数值;”;3、文字垂直居中样式“display: flex;align-items: center;”。

本教程操作环境:windows7系统、HTML5版、Dell G3电脑。
html文字居中
1、文字水平居中--text-align: center;
text-align 属性规定元素中的文本的水平对齐方式。
该属性通过指定行框与哪个点对齐,从而设置块级元素内文本的水平对齐方式。通过允许用户代理调整行内容中字母和字之间的间隔,可以支持值 justify;不同用户代理可能会得到不同的结果。
值 | 描述 |
---|
left | 把文本排列到左边。默认值:由浏览器决定。 |
right | 把文本排列到右边。 |
center | 把文本排列到中间。 |
justify | 实现两端对齐文本效果。 |
inherit | 规定应该从父元素继承 text-align 属性的值。 |
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" >
< style type = "text/css" >
h1 {
text-align: center
}
h2 {
text-align: left
}
h3 {
text-align: right
}
</ style >
</ head >
< body >
< h1 >这是标题 1</ h1 >
< h2 >这是标题 2</ h2 >
< h3 >这是标题 3</ h3 >
</ body >
</ html >
|
效果图:

【推荐教程:CSS视频教程 、《html视频教程》】
2、文字垂直居中
1)、line-height 使单行文字垂直居中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
height: 300px;
background: palegoldenrod;
line-height:300px;
}
</ style >
</ head >
< body >
< div class = "box" >css 垂直居中了--文本文字</ div >
</ body >
</ html >
|
效果图:

2)、CSS3的flex布局 使文字垂直居中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
height: 300px;
background: paleturquoise;
line-height:300px;
/*设置为伸缩容器*/
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
/*垂直居中*/
-webkit-box-align: center;/*旧版本*/
-moz-box-align: center;/*旧版本*/
-ms-flex-align: center;/*混合版本*/
-webkit-align-items: center;/*新版本*/
align-items: center;/*新版本*/
}
</ style >
</ head >
< body >
< div class = "box" >css 垂直居中--文本文字(弹性布局)</ div >
</ body >
</ html >
|

更多编程相关知识,请访问:编程视频!!
以上就是html文字居中的样式是什么的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
Html <b>加粗与<strong>加粗标签区别
实现在Html页面加载完毕后运行js方法
Html实现像百度的首页效果一样的背景图(代码)
Html canvas标签怎么用
详解Html实现在线预览word、excel、pdf等文件的功能(附代码)
Html optgroup标签怎么用
Html怎么给文字加粗
Html<input>标签怎么用
图文详解Html中有序列表、无序列表和自定义列表的区别
Html link标签怎么用
更多相关阅读请进入《Html》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » html文字居中的样式是什么