Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

html - Why does inline-block cause this div to have height?

jsFiddle Demo

I cannot seem to figure out why using display:inline-block would cause this <div> element to somehow gain height when its containing element was hidden. This does not happen with display:block.

html:

<div style="display:inline-block;"><input type="hidden" /></div>
<div>Gap above created by inline-block</div>
<div style="display:block;"><input type="hidden" /></div>
<div>No gap above if using block</div>

screenshot of jsfiddle

Why does display:inline-block cause the gap depicted here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One thing that happens when you create a display:inline-block is that the line-height calculations will change:

In an inline formatting context, boxes are laid out horizontally, one after the other, beginning at the top of a containing block. Horizontal margins, borders, and padding are respected between these boxes. The boxes may be aligned vertically in different ways: their bottoms or tops may be aligned, or the baselines of text within them may be aligned.

source: http://www.w3.org/TR/CSS2/visuren.html#block-formatting

The height of each inline-level box in the line box is calculated. For replaced elements, inline-block elements, and inline-table elements, this is the height of their margin box; for inline boxes, this is their 'line-height'.

source: http://www.w3.org/TR/CSS2/visudet.html#line-height

CSS assumes that every font has font metrics that specify a characteristic height above the baseline and a depth below it. In this section we use A to mean that height (for a given font at a given size) and D the depth. We also define AD = A + D, the distance from the top to the bottom.

source: http://www.w3.org/TR/CSS2/visudet.html#inline-box-height

So the line height will be defined on their font type. However when the inline-block is empty it will have its basic line-height. It however still tries to generate his line-height with a font.

To quick fix this you can use a wrapper which defines exclusive that there is no font, so no line-height which leads into no height:

.wrapper
{
    font-size: 0;
}

Where you can reset this property in your inline-block:

.wrapper div
{
    font-size: medium;
}

Where the default value of font-size is medium.

jsFiddle

This way you can still use content in the inline-block without there being a gap.


Update

This update is because of Kevin Wheelers comment

... I'm confused, it still never says what the height of an empty inline-block element is. ...

I want to note that I have not found any official documentation about this, though through testing I have found common patterns.


Short version:

Just think of it as inline-block expects content and reserves a minimum line space based of the known line-height.


Some more insight:

JsFiddle as a more clear example

As you can see the gab of the inline-block height is based on a line-height, which we have determed in the first post.

Now where does this line-height come from?

It is inherited from the first that determs the line-height: the <body> element.

You can test this my changing font-size, font-family or the line-height of the <body> element.

So it reserves a line-box for it's content. Which is strange that it is visible at all, as you can see according to the W3 specs of inline-formatting:

Line boxes that contain no text, no preserved white space, no inline elements with non-zero margins, padding, or borders, and no other in-flow content (such as images, inline blocks or inline tables), and do not end with a preserved newline must be treated as zero-height line boxes for the purposes of determining the positions of any elements inside of them, and must be treated as not existing for any other purpose.

It does this for every other element inside of the inline-block, but it always seems to reserve a minimum line space.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...