css中line-height设置无效怎么办

这篇文章给大家分享的是有关css中line-height设置无效怎么办的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

我们先写下这一串代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.head{
			height: 100px;
			text-align: center;
			line-height: 100px;/* 设置行高,让字体居中 */
			background: #333;/* 设置整个背景为黑色,便于观察字体 */
			color: red;
			font:700 18px simsun;/* 对字体进行设置 */
		}
	</style>
</head>
<body>
	<div class="head">
		你好,西南石油大学。
	</div>
</body>
</html>

然后在浏览器中打开看看效果:

css中line-height设置无效怎么办

我们发现在垂直方向,字体并没有在盒子的中间显示。

然后我们把设置行高那条语句放在设置字体(font)的下面:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.head{
			height: 100px;
			text-align: center;
			background: #333;/* 设置整个背景为黑色,便于观察字体 */
			color: red;
			font:700 18px simsun;/* 对字体进行设置 */
			line-height: 100px;/* 设置行高 */
		}
	</style>
</head>
<body>
	<div class="head">
		你好,西南石油大学。
	</div>
</body>
</html>

然后用浏览器打开:

css中line-height设置无效怎么办

字体就成功地跳到中间去啦~~~~~

总结:在用css对行高进行设置时,line-height的属性必须在font的下面,否则无效!

感谢各位的阅读!关于“css中line-height设置无效怎么办”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!