文本行高——line-height属性

line-height CSS 属性用于设置多行元素的空间量,如多行文本的间距。对于块级元素,它指定元素行盒(line boxes)的最小高度。对于非代替的 inline 元素,它用于计算行盒(line box)的高度。

        一般情况下,浏览器默认的line-height为1.2。可以自定义 line-height 覆盖这个初始值,那么该怎样设置line-height呢?有以下5种方式:

  1. normal 默认。
  2. 设置合理的行间距。 number 设置数字,此数字会与当前的字体尺寸相乘来设置行间距,即number为当前font-size的倍数。
  3. length 设置固定的行间距。
  4. % 基于当前字体尺寸的百分比行间距。
  5. inherit 规定应该从父元素继承 line-height 属性的值。

1、normal

取决于用户端。桌面浏览器(包括 Firefox)使用默认值,约为1.2,这取决于元素的 font-family

2、<数字>

该属性的应用值是这个无单位数字<数字>乘以该元素的字体大小。计算值与指定值相同。大多数情况下,这是设置line-height推荐方法,不会在继承时产生不确定的结果。

3、<长度>

指定<长度>用于计算 line box 的高度。参考<长度>了解可使用的单位。以 em 为单位的值可能会产生不确定的结果(见下面的例子)。

4、<百分比>

与元素自身的字体大小有关。计算值是给定的百分比值乘以元素计算出的字体大小。百分比值可能会带来不确定的结果(见下面第二个例子)。

5、-moz-block-height Non-Standard

将行高设置为当前块的内容区域高度。

 

模拟题: 修改诗歌行高

描述

您需要在 style.css 文件中实现如下功能

  1. .para1 的行高为 40px
  2. .para2 的行高为 2em,文字大小为 20px
  3. .para3 的行高是文字大小的 2 倍
  4. .para4 的行高为 12px

 HTML:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./style.css">
    <link rel="stylesheet" href="./common.css">
    <title>Sonnet</title>
</head>

<body>
    <p class="para1">thy eternal summer shall not fade</p>
    <p class="para2">Nor lose possession of that fair thou owest;</p>
    <p class="para3">Nor shall death brag thou wander'st in his shade,</p>
    <p class="para4">When in eternal lines to time thou growest:<br>So long as men can breathe or eyes can see,<br>So long lives this and this gives life to thee.</p>
</body>

</html>

样式css:

公共部分——common.css:

p {
    background-color: antiquewhite;
    margin: 10px;
    font-size: 16px;
}

作答部分——style.css:

.para1{
    line-height: 40px;
}
.para2{
    line-height: 2em;
    font-size: 20px;
}.para3{
    line-height: 200%;/* 或者写成line-height:1; */
}
.para4{
    line-height: 12px;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

面相进程,面相对象

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值