1. 程式人生 > >div 包裹img,div 高度大於img

div 包裹img,div 高度大於img

why?

this is a natural behavior of image because img is an inline element so user agents leave some space for descender characters.

How Inline Content is Constructed

In order to understand what just happened, let's take a look at the construction of a line box, the placement of images within a line box, and the placement of a line box within a table cell. First a basic line box containing text, shown in Figure 3.

Figure 3

The most crucial part of Figure 3 is the baseline (represented by the blue line), and its placement within the line box. The baseline's exact placement is dependent on the "default" font for the line box (represented by the red box), which is determined by the value of font-family for the element that contains the line box. It isn't possible for an author to change the baseline's position directly, so wherever it ends up is where it will be. The space below the baseline is referred to as "descender space" since that's where the descenders in lowercase letters like "j", "y", and "q" are drawn. Figure 4 shows what happens when we add an image to the mix.

Figure 4

Note where the image sits by default: its bottom edge is aligned with the baseline of the line box. This placement can be changed with vertical-align-- we'll talk about that in a bit-- but almost nobody ever changes the value from its default. Let's take away the text, and leave only the image, as was done in Figure 5.

Figure 5

So we have an image sitting on the baseline of a line box that contains only the image. Now consider what happens when we put that line in a table cell (Figure 6).

Figure 6

And there you have it-- spaces opening up where none have ever been seen before. It gets even worse with small images, like ones that are one pixel tall, as illustrated in Figure 7.


how?

you can remove it with css:

img { display:block;}or img { vertical-align:bottom;}

1.  基線、底線、頂線、中線[Css精通]深入理解CSS中的行高與基線 - 舊城 - 秋·水落石出

注意:基線(base line)並不是漢字文字的下端沿,而是英文字母“x”的下端沿。

參考:http://sojuker.blog.163.com/blog/static/1387908792012760243916/

           https://developer.mozilla.org/en-US/docs/Images,_Tables,_and_Mysterious_Gaps