Why is there a pesky little space between <img> and other elements?

假装没事ソ 提交于 2019-11-28 00:04:18

The img is a "replaced element" in HTML, and as such, is it treated as a character. Now in the absence of any styles, the image is aligned with the baseline of the other characters on the line.

So in other words, there is room for the descender underneath the image.

Changing it to a block removes this feature, as you noticed.

Images by default are aligned with the base line of the text. So if you would put text next to the image, the image would align with the base of an x, but there is a little space (3px apparently) between that base line and the bottom of the line of text.

There you have it:

http://jsfiddle.net/xDCEX/

And you can solve it using vertical-align: bottom if you dont want to make the image a block. That way, the image is still part of the text, but instead of the baseline, it is now aligned to the bottom of the bounding box of the text.

http://jsfiddle.net/xDCEX/1/

Of course, changing it to a display: block also works, but it has other side effect. If everything is working now, changing vertical-align is the easy way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!