本文摘自PHP中文网,作者青灯夜游,侵删。
文字居中的css代码是“text-align:center;”或“line-height:值;”。text-align属性规定元素文本的水平对齐方式,当值为center时,即可实现水平居中;而设置line-height可实现文字垂直居中。

本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。
css 文字居中
1、文字水平居中
text-align 属性规定元素中的文本的水平对齐方式,当值为center时可实现水平居中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" >
< style type = "text/css" >
div {
width: 300px;
height: 200px;
background: palegoldenrod;
text-align: center
}
</ style >
</ head >
< body >
< div >css 水平居中了--文本文字</ div >
</ body >
</ html >
|
效果图:

【推荐教程:CSS视频教程 】
2、文字垂直居中
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 >
|
效果图:

更多编程相关知识,请访问:编程视频!!
以上就是文字居中的css代码是什么的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
认识Html5的websocket
css有哪些属性不继承父级的
浅谈Html空链接的作用
前端开发者必须知道的http协议相关知识
已知和未知高度下进行垂直水平居中的方法
Html怎么实现密码隐藏显示
Html怎么设置button大小及颜色
深入理解Html中label的作用和使用方法(附代码)
你知道怎么利用css实现内凹圆角么
Html中引入外部页面的方法
更多相关阅读请进入《Html》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 文字居中的css代码是什么