zhongxia245 / blog

这是一个Blog, 如果喜欢可以订阅,是Watch, 不是 Star 哈。。。

Home Page:https://zhongxia245.github.io/blog/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

【20161208】CSS line-height 百分比和数值的区别

zhongxia245 opened this issue · comments

时间:2016-12-08 11:44:54

一、结论

line-height: 150% 不等于 line-height: 1.5
line-height的值为百分比: 子集元素继承父级元素的距离
line-height的值为数值 : 子元素计算各自的行距离

二、例子讲解

1. line-height 为百分比

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            font-size:14px;
            line-height:150%;
            background: black;
        }
        p{
            font-size:26px;
            background: gray;
            color: white;
        }
    </style>
</head>
<body>
    <p>你好!</p>
</body>
</html>

效果:

例子1结果

body{font-size:14px;line-height:150%;}
p{font-size:26px;}

//结果就是:
body{line-height:21px;}//14*150%=21
p{line-heigt:21px;}//继承父元素 

2. line-height 为数值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            font-size:14px;
            line-height:1.5;
            background: black;
        }
        p{
            font-size:26px;
            background: gray;
            color: white;
        }
    </style>
</head>
<body>
    <p>你好!</p>
</body>
</html>

效果:

说明:

body{font-size:14px;line-height:1.5;}
p{font-size:26px;}
//结果就是:
body{line-height:21px;} //14*1.5=21 
p{line-height:39px;} //26*1.5=39