HTML/CSS 图片边距问题

Issue With HTML/CSS Margins With Images

我试图让 HTML/CSS 中的图像边距为 0px 但是当我使用

body {
margin: 0px;
}

它影响整个body(图像和文本)。这是预期的。所以我想,好吧,我只使用 class。我试过这个:

CSS:

.class-name {
margin: 0px;
}

HTML:

<img class="class-name" src="path/to/file.png">

现在它只是简单地忽略了我有保证金的事实......有什么解决办法吗?提前致谢!

在你的情况下设置margin:0px;在你的形象上根本没有帮助你。在第一种情况下你的图片有边距的原因是因为 body 元素的默认边距为(据我所知)8px.

要删除边距,您必须像这样删除 body 的边距:

body {
margin:0px;
(or margin-left:0px; margin-right:0px etc)
}

或者将图像的边距设置为负值。

.class-name {
margin: -8px;
}