Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a block level element higher than the image it contains?

Tags:

html

css

Without height specifications, a <div> (or a <p> or any other similar element) that contains only a single image is a bit higher than it. It looks like its 4px higher in Firefox and 5px higher in Chrome (according to Firebug and its chrome equivalent). The extra space is added under the image.

Obviously I can fix this by assigning a height to the div, but I'd like to understand why that space is there and wether there is a way to eliminate it.

like image 362
Matteo Riva Avatar asked Feb 01 '11 15:02

Matteo Riva


1 Answers

Add line-height: 0px; to the containing element.

Setting the img to display:block also works but may break if someplace else you're hiding and showing images with display:none / display:inline.

like image 86
lilotop Avatar answered Sep 30 '22 19:09

lilotop