22

<div style="background-color:red">
  <div style="display:inline-block;background-color:green;height:20px;width:20px;"></div>
</div>
<div style="background-color:yellow">
  <div style="display:inline-block;background-color:green;height:20px;width:20px;">hi</div>
</div>

When rendered in FF or Chrome the height of the red div is 26px, whereas the height of the yellow div is 20px. How can I make the red div render the same as the yellow div, but without it containing any text?

Hidden Hobbes
  • 13,893
  • 3
  • 38
  • 64
Chris Herring
  • 364
  • 3
  • 9

4 Answers4

47

just a thought:

as long as there's no text in the div, it's treated like a inline-image (or something else), and so it's vertical-align is set to 'baseline'(or text-bottom or whatever) instead of 'bottom'.

the solution:

to correct that, set vertical-align: bottom; on your inner divs. there's absolutely no need to put a space or invisible element into it, like others mentioned (but that would be an (ugly) solution, too)

oezi
  • 51,017
  • 10
  • 98
  • 115
  • My pleasure! if this was the answer you're looking for, it would be nice to accept it by klicking the tick-mark on the left (you should always accept an answer it answered your question) – oezi Oct 29 '10 at 16:08
  • 4
    Yeeha!! I hate CSS for problems like this, but I love SO for people like you – NaturalBornCamper May 31 '13 at 02:06
  • nasty little bug, doesn't even show up under computed properties in the inspector – danielmhanover May 25 '17 at 18:34
1

If you want to have a specific height, use

min-height: 1em;

Using a space of some sort is a different height in some circumstances.

prewett
  • 1,587
  • 14
  • 19
1

How about putting a zero-width space (&#8203;) in the "empty" node?

jhurshman
  • 5,861
  • 2
  • 26
  • 16
0

It's lame, but you could add a &nbsp; to the empty div.

inline-block is a funny display type and without content because other properties like font-size and line-height can actually growing the element taller than 20px.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174