html - div 内的额外空间

标签 html css

我有点迷茫... 谁能告诉我,“T”上方和下方的额外空间从何而来?

“line-height”与“font-size”的值完全相同。我希望 div 应该与“T”本身具有相同的高度...

谢谢!

The T

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<style type="text/css">
#container {
    font-size: 50px;
    background-color: lime;
    line-height: 50px;
    margin: 0;
    padding: 0;
}
</style>

</head>
<body>
    <div id="container">T</div>
</body>
</html>

最佳答案

这就是 line-height 应该做的:使一行足够高以便可读并防止行相互重叠。 line-height 是根据 font-size 属性计算的。默认情况下它等于 1.2em

font-size 设置为 50px 并不意味着您的所有字符都将具有 50px 高度。作为spec says :

The font size corresponds to the em square, a concept used in typography.
Note that certain glyphs may bleed outside their em squares.

了解 em 方 block :wikipedia link .

所以你在这里得到的“边距”是在字体本身上,因为 'T' 字符的高度不等于 1em。

看看this documentation .你会更好地理解这个属性。

关于html - div 内的额外空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20334104/

相关文章:

javascript - javascript函数运行时将光标更改为等待符号

html - 如何将纯 css 工具提示添加到 svg 中的一类路径

php - 如何使用 php 从 html 中提取 img src、title 和 alt?

javascript - 如何使用javascript将iframe高度设置为100%

html - 针对非 IE 浏览器和特定 IE 版本的有效条件注释

javascript - 使用 css-modules 时后台 url 中的相对路径不起作用

image - 适合 div 的响应图像

javascript - 如何停止javascript中的函数?

javascript - 如何在 javascript 或 jquery 中重定向页面

javascript - react native : Can't style the Button