简体   繁体   English

显示的神秘负边距:内联块元素

[英]Mystery negative margin on display:inline-block elements

I'm really floored as to why this is happening. 我真的很沮丧为什么会发生这种情况。 The posts on http://syndex.me have a margin of 2px. http://syndex.me上的帖子的边距为2px。 When the page initially loads, this is adhered to. 当页面最初加载时,这是坚持的。 When the second batch of posts loads (14 posts down it kicks in) You'll see that for some bizarre reason, the posts to the right are actually 2px shorter than they should be. 当第二批帖子加载时(14个帖子下载)你会看到由于一些奇怪的原因,右边的帖子实际上比它们应该短2px。

Weirder still, inspecting the posts shows that they are in fact set with a margin:2px And even more weirdness, this only happens on the left or right margins, but not the top and bottom (?!) Weirder仍然,检查帖子显示他们实际上设置了一个margin:2px甚至更奇怪,这只发生在左边缘或右边缘,但不是顶部和底部(?!)

Having done front end for quite a while, I'm pretty confident this is a strange case. 做了前端很长一段时间后,我非常有信心这是一个奇怪的案例。

I'm getting this rendering issue on firefox, safari and chrome. 我在firefox,safari和chrome上遇到了这个渲染问题。

If I roll over the posts using the inspector, I can see that each post does indeed have a 2px margin, it's just that the margin of the second post (to the right) starts as if the post alongside it had a zero margin, but it positively does have one too. 如果我使用检查员滚动帖子,我可以看到每个帖子确实有2px的保证金,只是第二个帖子(右边)的边距开始,就像它旁边的帖子有零边距一样,但是它也确实有一个。

It's as though the posts are ignoring their neighbours left and right margins? 好像帖子忽略了他们的邻居左右边缘?

One thing, the posts are using display:inline-block . 有一件事,帖子正在使用display:inline-block What i don't understand is: why does the behaviour only kick in after lazy-load? 我不明白的是:为什么这种行为只会在延迟加载后启动? and I know that inline elements have a natural space of 2 to 4 px, but this is ignoring 2px, seems weird. 我知道内联元素的自然空间为2到4像素,但这忽略了 2px,看起来很奇怪。

I don't know where to start looking on this one, any help would be appreciated. 我不知道从哪里开始看这个,任何帮助将不胜感激。

When the page first loads, there is whitespace in the HTML between each <div class="post"> . 首次加载页面时,每个<div class="post">之间的HTML中都有空格。 Each chunk of whitespace is rendered as one space character (usually 4px wide). 每个空白块都呈现为一个空格字符(通常为4px宽)。 <div class="post_margin_adjustment"> counteracts this with its margin-right: -4px , so the gap between the initial posts is 8px: <div class="post_margin_adjustment">用它的margin-right: -4px抵消这个margin-right: -4px ,所以初始帖子之间的差距是8px:

4px (margin-right from the left post)  
+ 4px (space character)  
- 4px (negative margin-right from left post_margin_adjustment)  
+ 4px (margin-left from the right post)  
= 8px

When the Infinite Scroll plugin loads the next page, it grabs the new div.post elements and inserts them into the page, without the whitespace. 当Infinite Scroll插件加载下一页时,它会抓取新的div.post元素并将它们插入到页面中, 而不包含空格。 So the gap between the new posts is 4px: 所以新帖子之间的差距是4px:

4px (margin-right from the left post)  
- 4px (negative margin-right from left post_margin_adjustment)  
+ 4px (margin-left from the right post)  
= 4px

This is why the gap between new posts is narrower than the gap between the initial posts. 这就是为什么新职位之间的差距比初始职位之间的差距要窄。


To fix this, I would: 要解决这个问题,我会:

  1. Remove the whitespace between initial posts: 删除初始帖子之间的空格:

     $('div.post').detach().appendTo('#posts'); 

    on page ready/load, which emulates what the Infinite Scroll plugin is doing. 在页面就绪/加载,它模拟无限滚动插件正在做什么。

  2. Remove margin-right: -4px; 删除margin-right: -4px; from .post_margin_adjustment . 来自.post_margin_adjustment

The gap between initial and new posts should now both be 8px wide. 现在,初始和新职位之间的差距应该是8px宽。

First, this SO Answer is a complement to the SO Answer provided by Zuul on this page. 首先,本SO答案是Zuul在此页面上提供的SO答案的补充。 I could not EDIT his answer or the PEER REVIEW process did not work for whatever reason. 我无法编辑他的答案,或者PEER REVIEW过程无论出于何种原因都无法正常工作。

Credible and/or official sources has been provided for the SO Question. 已为SO问题提供了可靠和/或官方来源。

Reference: " It can't be that computed style according to the inspector is saying one thing but visually it's doing another? " 参考:“ 根据检查员的说法,计算出的风格不是说一件事,而是视觉上它正在做另一件事?

In this case Firefox has a 3D Viewer for CSS built in. It shows revealing information that the images are loaded correctly but the margin-right is creating the issue. 在这种情况下, Firefox内置了一个用于CSS3D查看器 。它显示了图像正确加载的显示信息,但margin-right正在产生问题。 Let me illustrate this via captured images for your Gold Helmet photo. 让我通过拍摄的金盔照片图片来说明这一点。

This first image shows the loaded image in it's own container via the dotted-lines . 第一张图像通过虚线显示加载的图像在其自己的容器中。 Notice however that the image itself extends outside dotted-lines on the right side: 但请注意,图像本身延伸到右侧的虚线外: marginError.jpg


This second image is a close-up using Firefox 3D Viewer . 第二张图片是使用Firefox 3D Viewer的特写镜头。 Here you can see the original container is placed on the page correctly via the bottom level of blue shown. 在这里,您可以看到原始容器通过显示的底部蓝色正确放置在页面上。 This shows that the onload process has respected the correct CSS distance to the image on the right. 这表明onload进程已经尊重了右边图像的正确CSS距离。 Although the text at the upper right is blurry, there is a checkmark for margin-right in use of -4px : 虽然右上方的文字很模糊,但使用-4px margin-right有一个复选标记 cssRespectedOnload.jpg


This final photo has the checkmark for margin-right removed. 最后一张照片的margin-right已删除。 Notice that the photo is now presented correctly: 请注意,照片现在正确显示: removeMarginOffset.jpg


The above can be duplicated using Firefox when using the 3D Viewer for CSS . 使用3D Viewer for CSS时,可以使用Firefox复制上述内容。

NEW: You can right click each image and choose view image to see larger version. 新功能:您可以右键单击每个图像,然后选择view image以查看更大的版本。

If I've got the problem right, by analyzing the page with FF12.0 using Firebug, I see: 如果我的问题是正确的,通过使用Firebug用FF12.0分析页面,我看到:

.post_margin_adjustment {
    margin-right: -4px;
}

And that is pulling the left posts to the right side, thus causing the layout to be disrespectful towards your: 这就是将左侧柱子拉向右侧,从而导致布局对您的不适应:

.index .post {                                         /* syndex.me (line 469) */
  margin: 4px;
}

Removing the negative margin, seems to solve the issue. 消除负边际,似乎解决了这个问题。


Note: 注意:

You are talking about margin:2px , but what I see on the current css is margin:4px; 你在谈论margin:2px ,但我在当前的css上看到的是margin:4px; , so, the total between posts is 8px if fixed the issue of the negative margin . 因此,如果固定负margin的问题,帖子之间的总数是8px

Just used the "ruler" from the "web developer 1.1.9" and it gives me 8px when the negative margin is disabled! 刚刚使用了“web developer 1.1.9”中的“标尺”,当禁用负margin时它会给我8px


One last thing that is also messing up the margin declaration is: 最后一件事也是搞乱保证金声明:

.index .post {                                          /* syndex.css (line 1) */
  line-height: 0;
}

This declaration is causing the images to get pulled up, thus reducing the space between posts on the top/bottom. 此声明导致图像被拉起,从而减少了顶部/底部的帖子之间的空间。

Disabling this fixes the top/bottom spacing, allowing it to be properly presented by having the margin:4px . 禁用此功能可修复顶部/底部间距,允许通过margin:4px正确显示它。

Your AJAX-retrieved content is missing whitespace that the stylesheet seems to rely on. 您的AJAX检索内容缺少样式表似乎依赖的空白。 Proof: preload a few dynamic posts, inspect the page, Edit as HTML the body element. 证明:预加载一些动态帖子,检查页面, 编辑为HTML元素。 You'd see that static posts have enough whitespace between them (possibly excessively so) and dynamic ones are stuck together . 你会发现静态帖子之间有足够的空白 (可能过多),而动态 帖子 则粘在一起

Solution one: add some whitespace in the script that appends dynamic posts. 解决方案一:在附加动态帖子的脚本中添加一些空格。

Solution two: avoid relying on whitespace in css and use alternative means of removing whitespace from inline-block elements. 解决方案二:避免依赖css中的空格并使用替代方法从内联块元素中删除空格。

IMO the general direction of #2 is preferable. IMO#2的总体方向是优选的。

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

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