Why the computed height of inline nonreplaced elem

2020-07-22 19:03发布

Content area plus leading equals inline box

According to the figure above(come from "CSS The Definitive Guide 3rd Edition"), the computed height of an inline box is equals to content area plus leading, if we didn't specify line-height, than the height will only determine by the content area.

Again according to "CSS The Definitive Guide 3rd Edition", user agent may choose one of the two ways to caculate the content area height:

In nonreplaced elements, the content area can be one of two things, and the CSS2.1 specification allows user agents to choose which one. The content area can be the box described by the em boxes of every character in the element strung together, or it can be the box described by the character glyphs in the element.

I use a simple page to test the inline element height caculation between browsers:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style>
        span {
            font-size: 15px;
        }
    </style>
</head>
<body>
    <span>MERLIN</span>
</body>
</html>

And the result is listed below, the span height value is given by the browser developer tool.

  • Chrome(23.0.1271.101): 15px
  • Firefox(20.0): 16px
  • Safari(6.0.3(8536.28.10)): 17px

(Specify the line-height to 15px explicitly is still lead to the same result)

Why the computed heights are different between all three browsers? according to the specification, there are only two ways a user agent can choose form(em box or character glyph).

Does this means there is no way i can control the height of an inline nonreplaced element?

标签: html css
1条回答
太酷不给撩
2楼-- · 2020-07-22 19:55

according to the specification, there are only two ways a user agent can choose form(em box or character glyph).

But it also says:

The height of the content area should be based on the font, but this specification does not specify how.

So it's still not 100% clearly defined how the height should be calculated. Note that different browsers have different font rendering algorithms, which may also affect these calculations.

查看更多
登录 后发表回答