为什么行框之间有空格,而不是由于半行距?

2024-05-11

在下面的代码示例中,您将看到垂直流动的之间有空白spans是。每个之间有空白line box.

我想首先声明这与之间的差距无关inline-block框,甚至是结果半领先 https://www.w3.org/TR/CSS21/visudet.html#leading,在计算最小行高时添加到内联水平框的顶部和底部。

来自CSS2.1规范 https://www.w3.org/TR/CSS21/visudet.html#inline-box-height:

内联框的高度包含所有字形及其 每边都是半行距,因此正好是“行高”。

And:

(线框的)最小高度由基线上方的最小高度和基线下方的最小深度组成。...

Notes:

  • background-color(如下例所示)覆盖了整行框
  • 尽管如此,每个行框之间仍然有空白
  • 我并不是在寻求消除差距的解决方案。如果我想这样做,我只需设置display: inline-block on the span

为什么会存在差距,有依据CSS2.1规范 https://www.w3.org/TR/CSS21/visudet.html#leading。规范的哪一部分解释了间距?

示例代码:

// From CSS spec:
// The height of the inline box encloses all glyphs and their half-leading on each side and is thus exactly 'line-height'. Boxes of child elements do not influence this height.
span {
  background-color: red;
  line-height: 1;
}
<span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span><br/><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span>

背景属性仅适用于内容区而不是线盒。在大多数情况下,内容区域是由height。正如我们可以读到的在规范中 https://www.w3.org/TR/CSS21/box.html#content-height:

盒子内容区域的尺寸 - 内容宽度和 内容高度——取决于几个因素:元素是否 生成该框已设置“宽度”或“高度”属性, 无论 该框包含文本或其他框,该框是否是表格等。

And here https://www.w3.org/TR/CSS21/visudet.html#the-height-property:

该属性指定框的内容高度。

该属性不适用于非替换的内联元素。请参阅 部分关于计算非替换内联的高度和边距 所使用规则的元素 https://www.w3.org/TR/CSS21/visudet.html#inline-non-replaced.

如果检查上面的链接我们可以读到:

“高度”属性不适用。内容区域的高度应该基于字体,但本规范没有明确规定 如何。例如,UA 可以使用 em-box 或字体的最大上升部分和下降部分。

Here is an illustration to better show youref https://stackoverflow.com/questions/5804256/image-inside-div-has-extra-space-below-the-image:

The content area is defined by the browser and in some case it can be the em1 that you see in above figure but not necessarely.

在所有情况下,无论如何line-height将是,内容区域将仅取决于字体属性。所以line-height定义行框的高度,内容区域的高度由字体属性定义。

所以真正的问题是:为什么默认情况下line-height不使行框等于内容区域?

如果检查我们检查文档 https://developer.mozilla.org/en-US/docs/Web/CSS/line-height我们可以看到默认值设置为normal and:

normal

取决于用户代理。桌面浏览器(包括 Firefox) 使用大约 1.2 的默认值,具体取决于元素的 字体系列。

Then

<number>(无单位)

使用值是这个无单位的<number>乘以元素本身的字体大小。

In some cases, we will have the line box a bit bigger than the content area which explain the gap.1


现在为什么要设置line-height to 1没有解决问题吗?

仅仅因为您设置了line-height跨度而不是line-height他们的容器还不够。这line-height容器的仍然是默认的1.2将被考虑,因为它大于1。换句话说,最大的line-height会赢。

下面是一些说明,以便更好地理解:

主体的线高是2并且只有更大的跨度线高才会产生效果:

body {
 line-height:2
}

span {
  background-color: red;
  line-height: 1;
  animation:change linear infinite 2s alternate;
}

@keyframes change {
  to {line-height:3}
}
<span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span><br/><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span>

将 line-height 设置为 body 就足够了,因为 span 将继承它:

body {
 line-height:1; /*line-height is equal to content area*/
}

span {
  background-color: red;
}
<span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span><br/><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span>

1 Worth to note that in some cases and for some particular font, you may not see any gap and you won't even need to set line-height to 1 because the content area may be bigger enough to cover the line box since the calculation of both value are independent.

这是一些例子

span {
  background-color: red;
}
div {
 margin:5px;
}
<div><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span><br/><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span>
</div>
<div style="font-family:cursive"><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span><br/><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span>
</div>

<div style="font-family:monospace"><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span><br/><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span>
</div>

<div style="font-family:sans-serif"><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span><br/><span>Some span. As seen, background covers font plus half leading on top/bottom. There is still a gap, which is due to something else.</span>
</div>

我们仅对第一个和最后一个示例有间隙。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么行框之间有空格,而不是由于半行距? 的相关文章

随机推荐

  • 即使在 Excel 2007 中插入行时也保持绝对引用

    我有一个电子表格 我希望单元格公式始终查看特定单元格 即使插入行或列并且特定单元格移动也是如此 实际上 我总是想查看表格的 顶部 单元格 即使在表格顶部插入了新行 例如 单元格 A2 的公式为 E 2 现在我突出显示第 1 行并执行 插入行
  • std::make_pair 与浮点数组(float2,无符号整数)

    我有一个用 float2 unsigned int 对模板化的向量 例如 std vector
  • 当我们第一次部署 WAR 文件时,某些代码可以运行吗?

    是否有任何方法或 API 可以使用 以便每当我部署新的 WAR 文件时 部分代码都应该执行 或者当 Tomcat 启动时 相应的 servlet 应该连续启动或运行一些代码 恢复一个老问题 因为唯一的答案没有显示任何例子 为了在部署 取消部
  • 返回即将推出的 YouTube API V3 视频安排日期?

    我想要返回 YouTube 中的直播的安排日期 Example of scheduled streams YT链接 https www youtube com channel UCP7jMXSY2xbc3KCAE0MHQ A https w
  • Pandas 使用什么规则来生成视图和副本?

    我对 Pandas 在决定数据帧中的选择是原始数据帧的副本或原始数据帧的视图时使用的规则感到困惑 例如 如果我有 df pd DataFrame np random randn 8 8 columns list ABCDEFGH index
  • Inno Setup安装先决条件[重复]

    这个问题在这里已经有答案了 我正在通过 Inno Setup 创建一个安装程序 我看到很多关于如何检测先决条件是否存在的代码示例 但没有任何关于当我找不到先决条件时如何实际安装先决条件的代码示例 我确信它非常简单 但是我该如何安装先决条件呢
  • 原子存储抛出错误

    我最近升级到了 C 11 兼容编译器 并且尝试将一些代码从 boost 更新到 c 11 标准 我在使用atomic store转换一些代码时遇到了问题 这是一些简单的测试代码 似乎会引发编译器错误 int main std shared
  • 在 grpc python 中处理异步流请求

    我试图了解如何使用双向流处理 grpc api 使用 Python API 假设我有以下简单的服务器定义 syntax proto3 package simple service TestService rpc Translate stre
  • 检测反射 DLL 注入

    在过去的几年中 恶意软件 以及一些渗透测试工具 如 Metasploit 的 meterpreter 负载 已经开始使用反射 DLL 注入 PDF http www harmonysecurity com files HS P005 Ref
  • 如何在 C++ 和 QML 应用程序中使用 qrc?

    我在 Windows7 上用 c qnd Qt Creator QML 编写了 Qt Quick Desktop 应用程序 现在 我必须部署它 并且我需要隐藏 qml 文件和图像 意味着 将它们放入资源等中 我读到有一个很好的方法可以使用
  • libgdx SpriteBatch 渲染到纹理

    是否可以使用 libGdx 适用于 Android 桌面的 Java 引擎 中的 SpriteBatch 渲染到纹理 如果是这样 怎么办 基本上我想将所有内容渲染到 512 x 256 纹理的 320 x 240 区域 然后缩放区域以适合屏
  • ES6 模板文字可以在运行时替换(或重用)吗?

    tl dr 是否可以制作可重用的模板文字 我一直在尝试使用模板文字 但我想我就是不明白 现在我感到沮丧 我的意思是 我想我明白了 但 它 不应该是它的运作方式 或者它应该如何实现 它应该变得不同 我看到的所有示例 甚至标记模板 都要求 替换
  • asp.net core http 如果没有内容类型标头,则删除 `FromBody` 忽略

    我在 http 中使用 bodyDELETE要求 我知道目前删除主体是非标准的 但是允许的 使用时出现问题HttpClient它不允许删除请求的正文 我知道我可以使用SendAsync 但我宁愿让我的 API 更加灵活 我希望这个机构是可选
  • 如何在 Objective-C 标头中引用 Swift 枚举

    有没有办法从 Objective C 标头引用 Swift 枚举 如果你想在 Objective C 头中查看 Swift 类 你可以使用 objc class Foo 我没有看到枚举有任何类似的东西 你想做的事情叫做forward dec
  • CPU寄存器和多任务处理

    我目前正在学习汇编 我很困惑 CPU 寄存器如何与多任务一起工作 所以在多任务系统中 CPU可以随时暂停某个程序的执行并运行另一个程序 那么在这一步中寄存器值是如何保存的呢 寄存器是压入堆栈还是以其他方式 CPU 寄存器如何与多任务一起工作
  • 如何使用ElasticSearch来实现社交搜索?

    我正在尝试使用 ElasticSearch 创建具有社交功能的商业搜索 我有一个企业目录 用户可以通过不同的方式与这些企业进行交互 通过查看它们 检查它们等 当用户搜索商家时 我希望能够在结果顶部向他们显示他们的朋友与之互动过的商家 或根据
  • AngularJS 中的嵌套模块

    我有 2 个不同的 AngularJs 模块 一个 widgetContainer 和一个 widget 小部件可以显示为独立的应用程序 也可以包含在小部件容器中 一个 widgetContainer 包含 0 N 个 widget 如果我
  • Google App Engine self.redirect() POST 方法

    在 GAE Python 中 使用 webApp 框架 调用 self redirect some url 通过 GET 方法将用户重定向到该 URL 是否也可以通过带有一些参数的 POST 方法进行 重定向 如果可以的话 怎样做 Than
  • jQuery .nothas?

    有没有办法做相当于 nothas jQuery 中的选择器 例如 如果我想添加一个TBODY to all TABLE如果元素还没有 那么最好做这样的事情 TABLE nothas TBODY append tbody tbody 如果这可
  • 为什么行框之间有空格,而不是由于半行距?

    在下面的代码示例中 您将看到垂直流动的之间有空白spans是 每个之间有空白line box 我想首先声明这与之间的差距无关inline block框 甚至是结果半领先 https www w3 org TR CSS21 visudet h