简体   繁体   English

如何在div包装img中删除不需要的空间?

[英]how to remove unwanted space in div wrapping an img?

jsfiddle jsfiddle

I have: 我有:

HTML: HTML:

<div>
    <img src='http://upload.wikimedia.org/wikipedia/commons/7/70/Peace_dove_icon.svg' width='50' height='50'>
</div>

CSS: CSS:

div{
    display:inline-block;
    position: relative;
    margin: 0;
    border: 0;
    padding: 0;
    overflow: visible;
}

but if you inspect the page you should hopefully see a seemingly arbitrary 5px extra on the bottom of the div tag. 但是如果您检查该页面,则希望在div标签的底部多看似任意的5px

how can I get rid of this? 我该如何摆脱呢?

It's the descender of the img. 它是img的后代。 Images behave like words and sit on the baseline in the container, which leaves space below them for descenders. 图片的行为就像单词,位于容器的基线上,在图片下方留出空间供后代使用。

Solution: give the img display:block or use properties like vertical-align , position or float , whichever suits the situation best. 解决方案:给img display:block或使用vertical-alignpositionfloat等属性,以最适合的情况为准。

I updated the fiddle - new one here - but I must say, there is no visible difference in this case. 我更新了小提琴-这里是新的 -但我必须说,在这种情况下没有明显的区别。 There is nothing on the screen except the image. 除了图像,屏幕上什么也没有。

You can use the vertical-align as by default, images are rendered inline, like a letter. 您可以使用垂直对齐方式 ,因为默认情况下,图像以线性方式呈现,就像字母一样。

or set the style="display: block;" 或设置style="display: block;"

Just give the div this css rule: 只需给div这个CSS规则:

height: 50px;

Here is a working fiddle: 这是一个工作的小提琴:

http://jsfiddle.net/Hive7/9JU3T/1/ http://jsfiddle.net/Hive7/9JU3T/1/

line-height:0; for the div solves the problem, too! 因为div也解决了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM