CSS Div 和图像边距/填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2053291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:27:30  来源:igfitidea点击:

Div and Image Margin/Padding

cssxhtml

提问by mike

Can anyone tell me why the below CSS produces extra margin below the image? It should just add a 5px padding around the whole thing and it works fine with text. Any ideas?

谁能告诉我为什么下面的 CSS 会在图像下方产生额外的边距?它应该只是在整个事物周围添加一个 5px 的填充,它可以很好地处理文本。有任何想法吗?

div#somediv {
margin: 0;
padding: 5px;
}

div#somediv img {
margin: 0;
padding: 0;
}

<div id="somediv">
  <img src="someimage.jpg" />
</div>

回答by Will Vousden

Try making the image a block-level element:

尝试使图像成为块级元素:

div#somediv img
{
    display: block;
    margin: 0;
    padding: 0;
}