【关于line-height为什么会让文字居中对齐的思考】

潘多拉魔盒,一旦开启一点,就会发现怎么要学习有那么多!

必须要知道的一些概念
1. 基线、底线、顶线、中线

基线、底线、顶线、中线
注:基线(base line)不是汉字的下端沿,而是英文字母的下沿端【回忆下英文字帖的三线】

2. 内容区

内容区

  • 内容区 是顶线和底线包裹的区域
  • 行内元素可以通过background-color属性来显示出来
  • 大小由font-size 和 文字数量决定
3. 行距、行高

行高、行距

  • 行高:一行文字的高度,也可以理解为相邻文本基线间的距离
  • 行距:相邻文本,上文本的基线和下文本的顶线的距离
详解vertical-align这属性
有两种使用场景
  • 行内元素(inline)
  • 表格单元格(table-cell)元素
作用

设置元素的垂直方向的对齐方式【行内元素专属,必须给行内元素设置的属性】

继承性

不继承
属性的继承性十分重要!例如:box-sizing 这个属性就是不可继承的

属性值
  1. baseline:基线对齐(默认)
  <!--
 	img和span一样是个行内样式
 	我们看这个图片和文字在一块展示时,它的对齐方式
	-->
  <div>
  An <img class="top" src="./img/panda.png" alt="link" width="32"height="32" />image with a text-top alignment.
 </div> 

baseline
2. sub : 下标对齐 和super:上标对齐

什么是下标和下标
元素的基线会相对于原基线降低或上升,具体降低或上升多少由浏览器来决定
vertical-align:sub 降低
vertical-align:super 升高

    <div>An <img style="vertical-align:sub"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a sub alignment.</div>
    <br>  
    <div>An <img style="vertical-align:super"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a super alignment.</div>
    <br>  

sub和super
3. top:顶端对齐和bottom:底端对齐

外部容器的的顶端和底端

<style>
    div{
      width:auto;
      height:50px;
      line-height: 50px;
      border: 1px solid red;
    }
</style>
    <div>An <img style="vertical-align:top"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a top alignment.</div>
    <br>  
    <div>An <img style="vertical-align:bottom"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a bottom alignment.</div>
    <br>  

top和bottom
4. text-top:文字顶线 和 text-bottom:文字底线

<style>
    div{
      width:auto;
      height:50px;
      line-height: 50px;
      border: 1px solid red;
    }
</style>
    <div>An <img style="vertical-align:text-top"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a text-top alignment.</div>
    <br>  
    <div>An <img style="vertical-align:text-bottom"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a text-bottom alignment.</div>
    <br> 

text-top和text-bottom
5. middle: 中部对齐

个人理解:该行内元素的中线和外部容器的中线重合

<style>
    div{
      width:auto;
      height:50px;
      line-height: 50px;
      border: 1px solid red;
    }
</style>
    <div>An <img style="vertical-align:middle"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a middle alignment.</div>
    <br>  

middle
6. 百分比和数值

和上标下标一样,不同的是,上标下标是浏览器来决定调整基线的降低或上升高度
这里我们可以控制基线的上下移动的幅度
百分比和基于line-height来计算的

<style>
    div{
      width:auto;
      height:50px;
      line-height: 50px;
      border: 1px solid red;
    }
</style>
    <div>An <img style="vertical-align:5px"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a 5px alignment.</div>
    <br>
    <div>An <img style="vertical-align:-5px"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a -5px alignment.</div>
    <br>
    <div>An <img style="vertical-align:10%"  src="./img/panda.png" alt="link" width="32" height="32" /> image
      with a 10% alignment.</div>
    <br> 

具体数值
百分比

那为啥line-height可以让元素居中对齐呢
  • 这个属性是可以继承的
  • 给块级元素设置line-height=高度,那么就相当于给行内元素(内容区的显示文本或图片)设置了行高
  • 如果是单行文字,那么文字的中线就会自动和外部容器的中线重合,这样就实现了居中

转载于:https://m.php.cn/article/479817.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值