Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline-block element height issue

Tags:

html

css

I have a simple example in which an outer DIV contains an inner DIV which has
display: inline-block;.
Because I have set the height of the inner div, I expect the outer div to take on the same height as the inner div. Instead, the outer div is slightly taller, as you can see from the fiddle. Question: Why is this happening and how can I "fill up" the outer div without setting its height explicitly?
My goal is to have the outer div expand and shrink based on the height of the inner.

.outer {
  background-color: red;
}
.inner {
  display: inline-block;
  width: 480px;
  height: 140px;
  background-color: green;
}
<div class="outer">
  <div class="inner"></div>
</div>
like image 397
Lyn Headley Avatar asked Dec 17 '14 23:12

Lyn Headley


People also ask

How to define the line height of an inline-block?

We also define AD = A + D, the distance from the top to the bottom. 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.

What is the height of the content area of inline box?

In case a line box only contains non-replaced inline boxes with the same line-height and vertical-align, those rules say that the height of the line box will be given by line-height. So in your case, this is also 15px. Height of the content area of an inline box. However, the developer tools of your browser said 18px.

What is the default vertical-align for Inline Blocks?

The default vertical-align is to have the bottom edge of the inline-block box lined up with the baseline of the surrounding text. Even if there is no surrounding text, the layout engine still has to make room for an entire line of text.

How to align text to the bottom of an inline block?

The default vertical-align is to have the bottom edge of the inline-block box lined up with the baseline of the surrounding text. Even if there is no surrounding text, the layout engine still has to make room for an entire line of text. That's why these answers are suggesting that you play with the vertical-align property.


2 Answers

Your .inner div has display: inline-block. That means it needs an inline formatting context around it. Inline layout produces struts, which make room for descenders. You can see how it fits if you put a character next to the .inner element: http://jsfiddle.net/bs14zzeb/6/

The default vertical-align is to have the bottom edge of the inline-block box lined up with the baseline of the surrounding text. Even if there is no surrounding text, the layout engine still has to make room for an entire line of text.

That's why these answers are suggesting that you play with the vertical-align property. Setting it to vertical-align: top, as one answer suggests, tells the layout engine to align the top edge of the inline-block box with the top edge of the line box. Here, since the line height is less than 140px tall, it gets rid of the extra space on the bottom. But if the height of a line is taller than that, you'll still have extra space underneath: http://jsfiddle.net/bs14zzeb/9/

like image 66
guest Avatar answered Oct 01 '22 15:10

guest


When using inline-block don't forget to set a vertical-align property MDN

.outer {
    background-color: red;
}
.inner {
    display: inline-block;
    vertical-align: top;     /* tada!!!! */
    width: 480px;
    height: 140px;
    background-color: green;
}
<div class="outer">
    <div class="inner"></div>
</div>
like image 23
Roko C. Buljan Avatar answered Oct 01 '22 14:10

Roko C. Buljan