这篇文章主要介绍了关于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(行高)设置无效的问题的解决方法(在css中行高属性line height是用来设置文本高度的)  css line-height 行高 第1张

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

然后我们把设置行高那条语句放在设置字体(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是用来设置文本高度的)  css line-height 行高 第2张

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

转载请说明出处
知优网 » 关于css中line-height(行高)设置无效的问题的解决方法(在css中行高属性line height是用来设置文本高度的)

发表评论

您需要后才能发表评论