微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

文字居中的css代码是什么

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

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

css 文字居中

1、文字水平居中

text-align 属性规定元素中的文本的水平对齐方式,当值为center时可实现水平居中。

<!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>

效果图:

1.png

【推荐教程:CSS视频教程

2、文字垂直居中

line-height 使单行文字垂直居中

<!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.png

更多编程相关知识,请访问:编程视频!!

原文地址:https://www.jb51.cc/css/1206276.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。