Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div gets extra height for no reason

I have some markup like so:

<div class="account-picture">
    <img src="http://i.imgur.com/Mcr3l.png">
</div>

The div needs to be floated left. The image is 128px x 128px.

And some css:

.account-picture{
   float: left;
   background: #FFFFFF;
   padding: 10px;
   border: 1px solid red;
   font-size: 1px;
   overflow: hidden;
}

img{
   border: 1px solid #F8F8F8;
   overflow: hidden;
}

But the problem is there seems to be some extra height assigned to the div. The layout diagrams from firebug are as follows: enter image description here

Why is the height of the div getting 2 extra pixels? Why does it vary across browsers?

  • Firefox 12: 2px extra
  • IE9: 0.26px extra
  • Chrome: 0px extra.

And here's a fiddle: http://jsfiddle.net/mWe5Y/

Why is this happening, and how do I get rid of that extra "height"?

like image 273
F21 Avatar asked May 03 '12 07:05

F21


People also ask

How do I stop div from resizing?

It is mostly used with textarea and div elements. To disable resizable property of an element use resize to none.

How do you get rid of extra height in CSS?

You can adjust the vertical-align of the image to position it elsewhere. However, adjusting the vertical-align: top did not completely solve the problem since there was still extra height. In my case, it was necessary to set line-height:0 to completely remove all extra height.

Why is div overflowing?

Overflow happens when there is too much content to fit in a box. CSS provides various tools to manage overflow. As you go further with CSS layout and writing CSS, you will encounter more overflow situations.


1 Answers

Because img is an inline element.

To get rid of that extra height:

.account-picture img {
    display: block;
}
like image 97
clyfish Avatar answered Nov 16 '22 01:11

clyfish