文字居中的css程式碼是什麼

2021-03-09 13:01:13

文字居中的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

【推薦教學: 】

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

更多程式設計相關知識,請存取:!!

以上就是文字居中的css程式碼是什麼的詳細內容,更多請關注TW511.COM其它相關文章!