line-height测量及使用
1、line-height定义
line-height表示行高,即两行文字基线间的距离。
以下是图示说明:

行高是2条红线之间的距离,即:1+2+3+4
在实际测量中,基线不好找,可测量顶线到顶线的距离来代替行高。
2、行间距
line-height 与 font-size 的计算值之差(在 CSS 中成为“行间距”)分为两半,分别加到一个文本行内容的顶部和底部。
示例代码:
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>line-height行高测量</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
font-size: 14px;
} .p {
width: 200px;
margin: 100px;
line-height: 100px;
border: 1px solid green;
}
</style>
</head> <body> <div class="p">
中文abc12345
</div>
</body> </html>
效果:

3、line-height取值
/*浏览器默认*/
line-height: normal;
/*设置数字,此数字会与当前的字体尺寸相乘来设置行间距*/
line-height:100px;
/*设置固定的行间距*/
line-height: 1.8;
/*基于当前字体尺寸的百分比行间距。*/
line-height: 180%;
说明:line-height可以继承,但是后代元素会继承这个缩放因子而不是计算值。
4、inline box
inline元素所产生的inline box,就是容器中每个行级元素都会产生一个inline box,而多个行级元素排成一行就会有多个inline box,即inline boxes。
<p>
<span>行级元素1</span><span>行级元素2</span><em>行级元素3</em>行级元素4
</p>
以上HTML有4个inline box,解释如下:
- p元素是一个容器,包裹了整个行级元素
- 不带标签的文字也是一个隐藏的行级元素
所有行级元素(行级元素1、行级元素2、行级元素3和行级元素4)具有四个inline box,而每一行都会有一个line box,其实就是每一行所有inline boxes,inline boxes高度取的是最高的inline box的高度。
即:每一行中,文字和图片都是inline box,它们共同组成了一个line box,line box的高度取决于inline box中最高的元素。
5、图片不受line-height影响
本示例图片尺寸为150*150px。
示例代码:
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>line-height行高属性</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
font-size: 14px;
} .p {
margin: 100px;
border: 1px solid red;
line-height: 1.8;
}
</style>
</head> <body> <div class="p">
<img src="dist/img/1_jslang.jpg" alt="尺寸为:150*150" /><span>az123</span>
</div>
</body> </html>
效果:

说明:上图的图片和文字下有一个间距,因为img的对齐方式默认为为基线对齐!
将img的基线对齐改为底部对齐可去除下面的空白
img{
vertical-align: bottom;
}
效果:

此时line-height应该设置为图片的高度,即150px。
文字和图片垂直居中的示例代码为:
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>line-height行高属性</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
font-size: 14px;
} .p {
margin: 100px;
border: 1px solid red;
/*设置为图片的高度了*/
line-height: 150px;
} img {
/*图片对齐方式改为底部对齐*/
vertical-align: bottom;
}
</style>
</head> <body> <div class="p">
<img src="dist/img/1_jslang.jpg" alt="尺寸为:150*150" /><span>az123</span>
</div>
</body> </html>
效果:
6、块级元素的高度和字体大小没有关系,是由行高决定。
示例代码:
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>line-height</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
} .test1 {
line-height: 14px;
font-size: 50px;
background-color: #f00;
} .test2 {
line-height: 50px;
font-size: 14px;
background-color: #ccc;
}
</style>
</head> <body>
<br />
<br />
<p class="test1">
我的行高是14px,字体大小是50px;
</p>
<br />
<br />
<p class="test2">
我的行高是50px,字体大小是14px;
</p>
</body> </html>
效果:

7、 行级元素元素的高度由字体大小决定,与行高无关。
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>line-height</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
} .test1 {
line-height: 14px;
font-size: 50px;
background-color: #f00;
} .test2 {
line-height: 50px;
font-size: 14px;
background-color: #ccc;
}
</style>
</head> <body>
<br />
<br />
<br />
<p>
<span class="test1">我的行高是14px,字体大小是50px;</span>
</p>
<br />
<br />
<p>
<span class="test2"> 我的行高是50px,字体大小是14px;</span>
</p>
</body> </html>
效果:

line-height测量及使用的更多相关文章
- How to Take Control of Your Line Height in Outlook.com
Reference to: http://www.emailonacid.com/blog/details/C13/line_height_and_outlook.com
- 深入了解css的行高Line Height属性
什么是行间距? 古时候我们使用印刷机来出来文字.印刷出来的每个字,都位于独立的一个块中. 行间距,即传说中控制两行文字垂直距离的东东.在CSS中,line-height被用来控制行与行之间垂直距离. ...
- height与line-height
1.网页的所有元素可以分为块元素和行元素.一行文字所在的一个逻辑区域是行元素,其他的元素就都是块元素line-height只针对行元素,height针对其他所有元素 2. width,height对于 ...
- View学习(二)-View的测量(measure)过程
在上一篇文章中,我们介绍了DecorView与MeasureSpec, 下面的文章就开始讨论View的三大流程. View的三大流程都是通过ViewRoot来完成的.ViewRoot对应于ViewRo ...
- Android自定义View(三、深入解析控件测量onMeasure)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51490283 本文出自:[openXu的博客] 目录: onMeasure什么时候会被调用 ...
- 真正的能理解CSS中的line-height,height与line-height
https://blog.csdn.net/a2013126370/article/details/82786681 在最近的项目中,常常用到line-height,只是简单的理解为行高但并没有深层次 ...
- Xamarin.Forms——尺寸大小(五 Dealing with sizes)
如之前所见的大量可视化元素均有自己的尺寸大小: iOS的状态栏高度为20,所以我们需要调整iOS的页面的Padding值,留出这个高度. BoxView设置它的默认宽度和高度为40. Frame的默认 ...
- Android 自绘TextView解决提前换行问题,支持图文混排
先看下效果图: 上面是MTextView,下面是默认的TextView. 一.原因 用最简单的全英文句子为例,如果有一个很长的单词,这一行剩余的空间显示不下了,那么规则就是不打断单词,而是把整个单词丢 ...
- Android Weekly Notes Issue #251
Android Weekly Issue #251 April 2nd, 2017 Android Weekly Issue #251. 本期内容: Android O新增的API: View的too ...
- Android-自定义View前传-View的三大流程-Layout
Android-自定义View前传-View的三大流程-Layout 参考 <Android开发艺术探索> https://github.com/hongyangAndroid/FlowL ...
随机推荐
- MySQL增强版命令行客户端连接工具(mycli)
效果: 安装: http://www.mycli.net/install 官网: http://www.mycli.net/install
- Disable File System Redirector For Windows x64 (Python recipe)(转)
This disables the Windows File System Redirector.When a 32 bit program runs on a 64 bit operating sy ...
- How to match between physical usb device and its drive letter?
struct tagDrives { WCHAR letter; WCHAR volume[ BUFFER_SIZE ]; } g_drives[ ]; // WCHAR GetUSBDrive( ) ...
- datagrid在MVC中的运用04-同时添加搜索和操作区域
本文介绍在datagrid上同时添加搜索和操作区域. 仅仅是增加操作区域 □ 方法1 $('#dg').datagrid({ toolbar: '#tb' }); <div id="t ...
- Oracle学习中,重点是sql语句的学习,而所有的sql语句都要在scott用户下完成。
熟悉这个用户下的四张表,是必要的. 查看所有表名: SELECT * FROM tab; 查看每张表的结构: DESC 表名称: 1.部门表:dept no 名称 类型 描述 1 DEPTNO NU ...
- VS2010属性
基于virtual studio 所有工程属性和 工程属性 这是VS2010的改变,不能够在“工具-选项”中看到“VC++目录”了. 但是呢,我们可以在另外一个地方找到它,请看下边的对比照片. VS ...
- python接口自动化6-重定向(Location)
前言 某屌丝男A鼓起勇气向女神B打电话表白,女神B是个心机婊觉得屌丝男A是好人,不想直接拒绝于是设置呼叫转移给闺蜜C了,最终屌丝男A和女神闺蜜C表白成功了,这种场景其实就是重定向了. 一.重定向 1. ...
- Ubuntu安装Oracle时出现乱码,及其他安装错误
只要在运行./runInstaller之前先运行下以下命令就ok了: export LANG=en_US #设置运行语言 编译错误 ln -s /usr/lib/i386-linux-gnu/libp ...
- 500 OOPS: vsftpd: cannot locate user specified in 'chown_username':whoever
错误:500 OOPS: vsftpd: cannot locate user specified in 'chown_username':whoever解决方案:在vsftpd.conf中修改如下两 ...
- iOS:制作一个简易的计算器
初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉. // // ViewController.m // 计算器 // // Created ...