css、line-height初探

浏览器默认行高1.0-1.2之间

line_height.jpg

行高主要有五种设置方法

1.line-height: normal; //默认

2.line-height: inherit; //继承

3.line-height: 140%; //百分比

4.line-height: 20px/20em //具体值

5.line-height: 1.2 //直接数值

font中设置line-height


1.line-height:

p{ font: 100%/normal arial; //字体大小/行高 字体

2.line-height:

p{ font: 100%/120% arial; //字体大小/行高 字体

3.line-height:

p{ font: 100%/20px arial; //字体大小/行高 字体

4.line-height:

p{ font: 100%/1.2 arial; //字体大小/行高 字体

实例

line-height

html结构

 <body>
   <h1> Hello World h1</h1>
   <p> P Hello World Hello WorldHello WorldHello WorldHello WorldHello W  orldHello WorldHello WorldHello     WorldHello WorldHello WorldHello World dHello WorldHello WorldHello WorldHello WorldHello       WorldHello WorldHello WorldHello WorldHello WorldHello World WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World p</p>

  <div id="footer">
    footer Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello     WorldHello WorldHello World  Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World
  </div>
</body>

Example1 The percentage value

CSS

    body{
    font-size: 16px;
    line-height: 120%;
    }
    h1 { font-size: 32px; }
    p { font-size: 16px; }
    #footer { font-size: 12px; }

最终line-height可在浏览器computed中查看

元素 font-size 最终line-height 显示效果
body 16px 16 * 120% = 19.2px
h1 32px inherits = 19.2px 太挤
p 16px inherits = 19.2px ok
#footer 12px inherits = 19.2px 太宽

Example2 the length value

CSS

body{
font-size: 16px;
line-height: 20px;
}
h1 { font-size: 32px; }
p { font-size: 16px; }
#footer { font-size: 12px; }

最终line-height可在浏览器computed中查看

元素 font-size 最终line-height 显示效果
body 16px 20px
h1 32px inherits = 20px 太挤
p 16px inherits = 20px ok
#footer 12px inherits = 20px 太宽

Example 3 the normal value

CSS

body{
font-size: 16px;
line-height: normal;
}
h1 { font-size: 32px; }
p { font-size: 16px; }
#footer { font-size: 12px; }

最终line-height可在浏览器computed中查看

元素 font-size 最终line-height 显示效果
body 16px normal * 1.2 => 16 * 1.2 = 19.2px
h1 32px normal * 1.2 => 32 * 1.2 = 38.4px ok
p 16px normal * 1.2 => 16 * 1.2 = 19.2px ok
#footer 12px normal * 1.2 => 12 * 1.2 = 14.4 ok

Example 4 the number value

CSS

body{
font-size: 16px;
line-height: 1.5;
}
h1 { font-size: 32px; }
p { font-size: 16px; }
#footer { font-size: 12px; }

最终line-height可在浏览器computed中查看

元素 font-size 最终line-height 显示效果
body 16px 16 * 1.5 = 24px
h1 32px 32px * 1.5 = 48px 太松
p 16px 16px * 1.5 == 24px ok
#footer 12px 12px * 1.5 = 18px ok

根据上方例子来看适应最好的方式是用下方进行line-height自适应设置。

body{
line-height: 1.5;
}
h1,h2,h3,h4,h5,h6 { line-height: 1.2;  }  

更多前端精彩
前端无缝滚动
*前端垂直居中 *

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,839评论 1 92
  • 早起留言。老公今天上班出发晚,阴雨绵绵,睡了个回笼觉。跟着去香港递交资料。下午回深圳在咖啡馆听完了上一期s的写作课...
    甘露yuer阅读 135评论 1 0
  • 成都冬意浓,奢阳破长空。 欣闻远客来,忘履邀宾朋。 廿年别钱塘,一朝聚城蓉。 问喧长短外,雅集宽窄中。 唏嘘叹年华...
    汤沅霖阅读 173评论 0 1
  • 第一猛将吕布与本多忠胜 外貌 吕布:五星,本多忠胜:三星 首先来说既然是一名武将,特别是一名勇猛的武将,必须要有震...
    荆棘阑珊阅读 450评论 0 0
  • 没有雾霾的大北京还是相当美丽的 银杏 枫叶 以上图片均为实拍,美景可遇而不可求!
    笑笑爱笑smile阅读 478评论 0 1