简体   繁体   English

div和img之间的空间?

[英]Space between div and img?

I have code like this: 我有这样的代码:

        <div id="sc">  
            <h1>1. Orange</h1>
            <p>some text in here </p>                                          
        </div>
        <img class="separator" src="images/separator.png" /> 

There's ALWAYS 13px gap between the "sc" div and "separator" img. “sc”div和“separator”img之间总共有13px的差距。

Margins and paddings for both are set to 0, null, empty, nothing. 两者的边距和填充都设置为0,null,空,没有。 Argh. 哎呀。 I'm so mad ;d 我很生气; d

I was trying to figure out what's going on with firebug but the space between them just doesn't belong to anything, it's not a margin, not a padding, what the heck? 我试图弄清楚萤火虫发生了什么,但它们之间的空间不属于任何东西,它不是边缘,不是填充物,到底是什么?

No floats, no display settings, no inherited margins or paddings either. 没有浮点数,没有显示设置,没有继承的边距或填充。

What's wrong with my code? 我的代码出了什么问题? I've been trying to delete the whitespace in HTML but doesn't help (by the way if I put the separator above the "sc" div there's also some gap, but smaller). 我一直试图删除HTML中的空格但没有帮助(顺便说一下,如果我把分隔符放在“sc”div之上,那么也有一些差距,但是更小)。

Thanks. 谢谢。

[ADDED] [添加]

CSS STYLES: CSS样式:

.separator {
    margin: 0;
    padding: 0;
}

#sc {
    text-align: justify;
    padding: 0;
    margin: 0;
    background-image: url('images/bg.png');  
    background-repeat: repeat-y;
    width: 970px;
}

Add display: block; 添加显示:块; to the .separator image. 到.separator图像。

.separator {
    margin: 0;
    padding: 0;
    display: block;
}

The problem is that images can sometimes add a bit of magic space up/below them, I have this issue whenever I'm working with image elements as *block* elements. 问题是图像有时会在它们上方/下方添加一些魔法空间,每当我使用图像元素作为*block*元素时,我就会遇到这个问题。

I had a 3px gap between an image and div tag. 我在图像和div标签之间有3px的间隙。 Also all styles were set to 0. Really weird. 所有样式都设置为0.真的很奇怪。

The fix: 修复:

img {
   vertical-align: middle;
}

This worked beautifully for me. 这对我很有效。

With no screenshots to refer to I'm left in the dark on what you want, so this is all guessing. 没有截图可以参考我在黑暗中留下你想要的东西,所以这都是猜测。

I'm guessing from class="separator" that you are trying to break up your content with a horizontal line. 我猜测从class="separator"你试图用水平线分解你的内容。 Shouldn't you be using <hr /> with appropriate styling if that's the case? 如果是这样的话,您是否应该使用具有适当样式的<hr />

In any case, note that <p> elements have vertical margins set by default. 在任何情况下,请注意<p>元素默认设置了垂直边距。

I don't see why you want the separator right up snug against your text, because it visually doesn't make sense to me. 我不明白你为什么要让分隔符紧贴你的文字,因为它在视觉上对我没有意义。

If you really do, there's a bunch of options: 如果你真的这样做,有很多选择:

  1. Set margin-bottom: 0; 设置margin-bottom: 0; on the <p> <p>
  2. Set margin-top: -*px; 设置margin-top: -*px; on .separator where you're assuming you're always going to have an element right before the separator with bottom margin of *px on .separator你假设你总是在分隔符之前有一个元素,底部边距为*px
  3. #sc p:last-child { margin-bottom: 0; } #sc p:last-child { margin-bottom: 0; } and IE9.js for letting older Internet Explorer versions support it #sc p:last-child { margin-bottom: 0; }IE9.js为让较旧版本的Internet Explorer支持

But I reiterate; 但我重申一下; no margin between text and a separator doesn't sound right to me. 文本和分隔符之间没有边距对我来说听起来不对。

that's because there is whitespace between the tags 那是因为标签之间有空格

do: 做:

</div><img class="separator" src="images/separator.png" /> 

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

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