line-height之height

文并非介绍line-height的四条线,也不说它是如何继承的

Demo

在 PC 端,为了实现文字居中, 我们这样写 CSS

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        * {
            margin:0;
            padding: 0;
        }
        .box {
            width: 500px;
            height: 200px;
            line-height: 200px;
            border:1px solid orange;
            text-align: center;
            color:black;
        }
    </style>
</head>
<body>
    <div class="box">
        测试
    </div>
</body>
</html>

好,很好,非常好,简简单单就实现了文字居中,可喜可贺 咦! 我为啥要写border属性呢?无所谓啦,这种事随它去吧 真的是无所谓么 有时在移动端我们会加上这个属性

box-sizing:border-box;

看起来没有什么不一样啊! 让我修改一下border

border:50px solid black;

这他么什么鬼,画风完全不对好吧 这时候只需要将更改

line-height: 100px;

文字就再次居中了

让我恢复到原来的属性然后加上

padding:50px 0;

花还香香的 再来一条

box-sizing:border-box;

... 这时候也需要更改

line-height:98px;

哦!!! 不管你们懂不懂,反正我是懂了

我们都知道当 height 与 line-height 的高度相等的时候文字会垂直居中 ,这个 height 指的得是盒模型的 content 部分,不包括 padding 与 border

转载于:https://my.oschina.net/heartwalker/blog/704546

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值