2

I am currently in learning CSS and it seems I have a hard time understanding the box model. I have a very simple page layout:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="css/simpleimagebrowser.css">
</head>
<body>
<menu type="toolbar">
    <ul>
        <li>prev</li>
        <li>next</li>
    </ul>
</menu>
<div class="imagecontainer">
    <img src="images/awsome.jpg">
</div>

</body>
</html>

and a very simple css:

body
{
    margin: 0;
    padding: 0;
}
menu
{
    padding: 0;
    margin: 0;
    background: green;
}
ul
{
    padding: 0;
    margin: 0;
}
li
{
    display: inline;
}
img
{
    padding: 0;
    margin: 0;
}
.imagecontainer
{
    background: yellow;
    padding: 0;
    margin: 0;
}

Why is does my yellow <div> have this little margin or gap at the bottom?

I noticed that when I set the font-size to 0 that margin goes away. Can someone explain conceptually what's going on from a css/boxmodel perspective? It seems as if the browser is adding a blank text line below the image or something ...

enter image description here

bitbonk
  • 48,890
  • 37
  • 186
  • 278
  • Not sure if this has anything to do with it but you didn't close your img tag. It should be self closing with /> at the end of it. – HK1 Apr 25 '13 at 20:45
  • I think in html5 land this is not necessary: http://stackoverflow.com/questions/10598501/closing-tags-in-html5 – bitbonk Apr 25 '13 at 20:46
  • Have you tried setting you body or that image container ( whatever contains that yellow ) height to 100% ? – InspiredBy Apr 25 '13 at 20:48
  • This is not a duplicate of http://stackoverflow.com/questions/10612380/get-rid-of-space-underneath-inline-block-image I am not asking how to get rid of it (as I mentioned, I already now a solution), I am asking for an explanation why this happens: – bitbonk Apr 25 '13 at 20:51

3 Answers3

2

Add display: block; to your image

img {
    display: block;
}

The white space is due to the image being an inline element. I suppose it's the equivalent of line-heightwhich adds white space around text.

Turnip
  • 35,836
  • 15
  • 89
  • 111
1

This is because all inline elements are expected to fit the 'contains text' model of a span tag, where space is reserved for the tails on letters like g, q, j, etc.

If non, this extra-space is used for 'link underlining' by default browser settings on inline level elements. Meaning, it reserves space for a link hover underline.

tail (n: tel)

Of a letter, the part that extends below the baseline and to the right, as gjqy. Of the capital letters, Q and R have tails, though they need not extend below the baseline.

Milche Patern
  • 19,632
  • 6
  • 35
  • 52
0

Indeed, adding "display: block;" to your img rule will make the problem go away. This is most likely due to images being treated as "inline" or "inline-block" by default. Thus the browser is very likely attempting to work your image into the line-height of the parent element.