Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline-block element with no text renders differently

Tags:

<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?

like image 247
Chris Herring Avatar asked Oct 29 '10 15:10

Chris Herring


People also ask

How are inline and block elements different from each other?

Inline elements don't start in a new line. Block elements always start in a line. Inline elements allow other inline elements to sit behind. Block elements have top and bottom margin.

Does text align work on inline-block?

Even though the property says “text” align, it affects all elements inside the block-level element that are either inline or inline-block elements. The property only affects the content inside the element to which it is applied, and not the element itself.

Can inline elements be used for one line text only?

Yeah it works but someone already provided the same solution.


1 Answers

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)

like image 196
oezi Avatar answered Sep 28 '22 00:09

oezi