简体   繁体   English

奇怪的差距 <div> IE中的元素,而不是FF或Opera

[英]Strange gap between <div> elements in IE, not in FF or Opera

I know this kind of question must get asked all the time but I haven't found a solution for my problem yet. 我知道这种问题必须一直被问到,但我还没有找到解决问题的方法。

Using FF, Opera and the IE that is on Windows 7 (can't remember what it is), the page looks exactly as it should, but using IE7 on Windows Vista, there is a gap between my navigation bar and the rest of the page which frankly makes it look stupid, and the illusion of tabbed pages is lost. 使用FF,Opera和Windows 7上的IE(不记得它是什么),页面看起来应该是应该的,但在Windows Vista上使用IE7,我的导航栏与其余部分之间存在差距。坦率地让它看起来很愚蠢的页面,标签页的幻觉丢失了。

I have a reset stylesheet to reset all the elements to have no padding, margins etc and FF, Opera and the IE on Windows 7 produce the page as they should, it's only IE7 (and I'm guessing earlier versions of IE) that don't. 我有一个重置样式表来重置所有元素没有填充,边距等和FF,Opera和Windows 7上的IE生成他们应该的页面,它只是IE7(我猜的早期版本的IE) “T。

Here are 2 screenshots showing the problem, the first from FF/Opera/IE on Windows 7: 以下是显示问题的2个屏幕截图,第一个来自Windows 7上的FF / Opera / IE:

替代文字

This one is from IE7 on Windows Vista: 这个来自Windows Vista上的IE7:

alt text http://img43.imageshack.us/img43/7558/figarosiegap.jpg alt text http://img43.imageshack.us/img43/7558/figarosiegap.jpg

And here is a link to the actual website in question: Figaro's Ristorante 这里有一个指向实际网站的链接: Figaro's Ristorante

Any ideas anyone? 任何人的想法?

Thanks for your time. 谢谢你的时间。

I've run into this problem a bazillion times. 我已经遇到过这个问题了无数次。 Add this to your CSS: 将其添加到您的CSS:

#header img { vertical-align: bottom }

There's a funny bug in IE up to and including version 7 where it will treat some whitespace (an empty text node, really) as a real text node, and align the image as if there was text in the element as well. IE中有一个有趣的错误,包括版本7,它会将一些空格(真正的空文本节点)视为真正的文本节点,并将图像对齐,就好像元素中也有文本一样。

Another option would be to declare the image as a block level element: 另一种选择是将图像声明为块级元素:

   #header img { display: block }

This CSS is safe to add to your global file, it will not interfere with how other browsers render the page. 这个CSS可以安全地添加到您的全局文件中,它不会干扰其他浏览器呈现页面的方式。

The IE on windows 7 is IE8 Windows 7上的IE是IE8

I've taken a look at it using IE7, and the gap appears to be because of the image in the 'header' div. 我用IE7看过它,差距似乎是因为'header'div中的图像。 If you look at it with a tool like IE Developer toolbar you can see the boundaries around the objects on the page. 如果您使用IE Developer工具栏等工具查看它,您可以看到页面上对象周围的边界。

Sorry i cant paste an image but i'll try to describe it: there is a #text element after the image which is being forced onto a new line by IE7. 对不起,我无法粘贴图像,但我会尝试描述它:图像后面有一个#text元素被IE7强行换行。 if you change the style on the img to include float: left; 如果你改变img的样式,包括float:left; This fixes the problem for me. 这解决了我的问题。

Hope this helps! 希望这可以帮助! (Let me know if you need more clarity) (如果您需要更清晰,请告诉我)

The gap is part of the text line where the menu image is, because the image is an inline element so it's placed on the baseline of the text line. 间隙是菜单图像所在文本行的一部分,因为图像是内联元素,因此它位于文本行的基线上。 The gap is the distance from the baseline of the text to the bottom edge of the line, ie the space used by hanging characters like 'g' and 'j'. 间隙是从文本基线到线条底部边缘的距离,即悬挂字符(如“g”和“j”)所使用的空间。

Simply adding display:block; 只需添加display:block; to the style of the image solves the problem. 图像的风格解决了问题。 It turns the image element from an inline element to a block element so that it's not placed on a base line of the text but as a separate element. 它将图像元素从内联元素转换为块元素,这样它就不会放在文本的基线上,而是作为一个单独的元素放置。

I've run into this problem a thousand times, and finally, after using overly complicated fix after fix, the answer is simple! 我遇到了这个问题一千次,最后,在修复后使用过于复杂的修复后,答案很简单! (At least when <img> 's are involved.) In the div that is producing a gap under it, add 'overflow: hidden;' (至少在涉及<img>时)。在正在产生间隙的div中,添加'overflow:hidden;' to its css; 它的CSS; you will need to set its height, of course. 当然,你需要设定它的高度。 So, if your div is 39px high, this will keep it at 39px high, ignoring the extra whitespace IE loves to put under <img> s 所以,如果你的div是39px高,这将保持在39px高,忽略了IE喜欢放在<img> s下的额外空白

Hope it helps. 希望能帮助到你。

There's not much useful information (html or pictures that work) in this question. 在这个问题中没有太多有用的信息(html或图片有效)。 So, here's a random guess. 所以,这是随机猜测。

I've had situations where a line-break or spaces between elements can cause vertical space between elements. 我有过这样的情况:元素之间的换行符或空格可能会导致元素之间的垂直空间。 Try placing the closing and opening tags immediately next to each other and see if this corrects the issue. 尝试将关闭和打开标签紧挨着放置,看看是否能解决问题。

Different browsers all have different default margins and padding. 不同的浏览器都有不同的默认边距和填充。 In this case, I'm guessing IE7s defaults are throwing you off. 在这种情况下,我猜测IE7的默认设置会让你失望。 There are two general solutions to the problem. 这个问题有两种通用的解决方案。 You can set your own margin and padding at the html, body level: 您可以在html,body级别设置自己的边距和填充:

html, body {
    margin: 0;
    padding: 0;
}

or you can use IE conditional comments to load sepearte stylesheets for different versions of IE. 或者您可以使用IE条件注释来为不同版本的IE加载sepearte样式表。 Last I checked, the conditional comments were considered a better solution because browser defaults do provide some usefulness. 最后我检查过,条件注释被认为是一个更好的解决方案,因为浏览器默认值确实提供了一些有用性。

Jason is correct that it's a bug in how IE handles whitespace in the html... treating it as a text node. Jason是正确的,它是IE如何处理html中的空格的错误...将其视为文本节点。 Though I don't think it's unique to images. 虽然我不认为它是独特的图像。 I believe I've seen this behavior with divs as well. 我相信我也看过div的这种行为。 As a global change you may try applying vertical-align:bottom to both images and divs. 作为全局变化,您可以尝试将vertical-align:bottom应用于图像和div。 Though I don't know what mayhem that may produce. 虽然我不知道可能会产生什么样的混乱。

But the quick and dirty fix is to just remove the whitespace. 但快速而肮脏的解决方法是删除空白。 Kinda sucks, but change stuff like this: 有点糟透了,但改变这样的东西:

<img src="blah" alt="" width="5" height="5" />

<div>blorg</div>

To this: 对此:

<img src="blah" alt="" width="5" height="5"

/><div>blorg</div>

I warned that this is quick and dirty. 我警告说这很快又脏。 But it works. 但它的确有效。

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

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