简体   繁体   English

Safari上的布局差异(右边缘为负数)

[英]Difference of layout on Safari with margin-right negative

I stumbled upon a difference in layout rendering between Safari and Chrome/Firefox and I don't know which one is "right". 我偶然发现Safari和Chrome / Firefox之间在布局呈现方面存在差异,但我不知道哪个是“正确的”。

You can check the jsfiddle here 你可以在这里检查jsfiddle

On Firefox/Chrome the layout is as expected, the yellow div is right after the red ones. 在Firefox / Chrome上,布局符合预期,黄色div紧跟在红色div 之后 But on Safari, the yellow div is positioned under the red ones. 但是在Safari上,黄色的div位于红色的div 之下

After investigating what I did wrong I found out the bug comes from the CSS class E whose property margin-right (value: -11px) is bigger than the width property (value: 10px) for my div. 在调查了我做错了什么之后,我发现该错误来自CSS类E ,该类的属性margin-right (值:-11px)大于div的width属性(值:10px)。

I think I understand why Safari renders it this way. 我想我理解Safari为什么以这种方式呈现它。 The width of div of class B is computed as being the sum of the widths of its children as they have the property float: left; B类的div宽度被计算为其子级的宽度之和,因为它们具有float: left;属性float: left; .

Here it is widthB = widthB2*2 + widthE + marginRightE + widthC or marginRightE < -widthE so widthB is not large enough to contain each div next to each other. 在这里,它是widthB = widthB2*2 + widthE + marginRightE + widthCmarginRightE < -widthE因此marginRightE < -widthE不足以容纳彼此相邻的每个div。

So my questions are: 所以我的问题是:

  1. Am I right in my understanding of what Safari does? 我对Safari的功能理解正确吗?
  2. Why do Chrome and Firefox render differently? 为什么Chrome和Firefox呈现不同的外观? Are they just not decreasing the width of the parent div based on a negative margin-right ? 它们只是不基于负的margin-right减小父div的宽度吗?
  3. Would the proper correction to always have a margin-right lesser or equal to the width of a div in this case? 在这种情况下,是否进行适当的校正以始终使margin-right小于或等于div的宽度?

Thank you! 谢谢!

HTML: HTML:

<div class="A">
  <div class="C">
    <div class="B">
      <div class="B2"></div>
      <div class="B2"></div>
      <div class="E"></div>
      <div class="C">
        <div class="D"></div>
      </div>
    </div>
  </div>
</div>

CSS: CSS:

.A {
  background-color: blue;
  height: 200px;
}

.B {
  height:100px;
}
.B2 {
  background-color: red;
  height: 100px;
  width: 100px;
  float: left;
}
.C {
  float: left;
}
.D {
  height: 40px;
  width: 40px;
  float:left;
  background-color: yellow;
}
.E {
  height: 50px;
  width: 10px;
  position: relative;
  left: -10px;
  margin-right: -11px;
  background-color: black;
  float: left;
}

I'm not sure what you expect to happen with the CSS in the JS fiddle. 我不确定您对JS小提琴中的CSS会发生什么期望。 You are delving into undefined behaviour. 您正在研究不确定的行为。 I say this because: 我之所以这样说是因为:

  • 'C' is floated but does not have a defined width. “ C”是浮动的,但没有定义的宽度。 This leads to issues in various browsers depending on the complexity of the layout. 根据布局的复杂程度,这会导致各种浏览器出现问题。

  • None of the floated elements are ever cleared. 没有浮动元素被清除。 When floating it is imperative that a clearfix of some description is used, whether it is clear:both , etc. 浮动时,必须使用一些描述的clearfix,无论是否clear:both等。

If you tweak the mark-up and add a clear-fix, you see that the content is always 239px . 如果您对标记进行调整并添加一个明确的修正,您将看到内容始终为239px See http://jsfiddle.net/eaFn9/ 参见http://jsfiddle.net/eaFn9/

However , it seems like the relatively positioned item 'E' and margin is having a negative impact on the width calculation, as Chrome's web inspector seems to always report oddly for the negative margin on this element. 但是 ,似乎位置相对较高的项目“ E”和边距对宽度计算产生了负面影响,因为Chrome的网络检查器似乎总是奇怪地报告此元素的负边距。

If you play around with this in web inspector you can see it's almost as if the negative margin is the cause of the drop. 如果您在网络检查器中使用此功能,则可以看到负利润几乎是造成下降的原因。 I think it may be due to a container that does not have a width, and isn't position relative in itself. 我认为这可能是由于一个容器没有宽度,并且自身位置不相关。

How to fix? 怎么修?

Personally, I would want to re-write your layout to include fixed widths on all floats, reduce nesting of floats and clear where possible. 就个人而言,我想重新编写布局,以在所有浮标上包括固定宽度,减少浮标的嵌套并在可能的情况下清除。 It seems overly complex but without a real world use case it's hard to rewrite. 它看起来过于复杂,但是如果没有真实的用例,就很难重写。

However, It seemed to me that you can wrap 'B2' + 'E' elements in a wrapper that is floated and relatively positioned, then use absolute positioning on 'E' to give the same affect and remove the negative margin. 但是,在我看来,您可以将'B2'+'E'元素包装在一个浮动且相对定位的包装器中,然后对'E'使用绝对定位以产生相同的效果并消除负边距。

This is the JSFiddle I came up with: http://jsfiddle.net/jV3Ub/ 这是我想出的JSFiddle: http : //jsfiddle.net/jV3Ub/

Sorry, this is not really an answer but it's too long to make it a comment... Anyway, it took me a minute to figure this out. 抱歉,这并不是一个真正的答案,但是要发表评论太久了……无论如何,我花了一分钟时间才弄清楚这一点。

I used Firefox 19 on Mac OS X 10.8.2, Chrome 24.0 (Mac) and Safari 6.0.2 (Mac as well). 我在Mac OS X 10.8.2,Chrome 24.0(Mac)和Safari 6.0.2(以及Mac)上使用了Firefox 19。 Using the web inspector tools, I realized the divs are not computed the same way indeed. 通过使用Web检查器工具,我意识到div的计算方法确实不同。 I suck at calculations, but I took the time to sit down and look at this thoroughly, and I do understand Safari's calculations the same way you do. 我很喜欢计算,但是我花了一些时间坐下来仔细研究了一下,而且我也和您一样了解Safari的计算。

In Safari, it seems that div B isn't wide enough to contain the yellow div (C) so it seems to reject it to the bottom. 在Safari中,div B的宽度似乎不足以容纳黄色的div(C),因此似乎拒绝了它。 For the record, in my tests, I see the yellow div to the right of the red div in FF and Chrome, while Safari shows it right underneath the red, and to the upper left. 作为记录,在我的测试中,我在FF和Chrome中看到红色div右边的黄色div,而Safari在红色下面和左上方显示它。 I am not sure this will help, but I can only recommend you to use the web inspector tools now integrated to all modern browsers to debug this. 我不确定这是否有帮助,但是我只能建议您使用现已集成到所有现代浏览器中的Web检查器工具进行调试。

I'm not sure about why this happens, all I know is that by only changing the width of E by 1px, like so: 我不知道为什么会这样,我只知道通过仅将E的宽度更改1px,就像这样:

.E {
  height: 50px;
  width: 11px; /* added 1px to this property */
  position: relative;
  left: -10px;
  margin-right: -11px;
  background-color: black;
  float: left;
}

it displays correctly in Safari. 它可以在Safari中正确显示。

Make the following changes to classes .D and .E : 对类.D.E进行以下更改:

.D {
  float:left;
  height: 40px;
  width: 40px;
  background-color: yellow;
  margin-left: -11px;
}
.E{
  height: 50px;
  width: 10px;
  position: relative;
  left: -10px;
  background-color: black;
  float: left;
}

DEMO: http://jsfiddle.net/uryJJ/22/ 演示: http : //jsfiddle.net/uryJJ/22/

I hope this helps! 我希望这有帮助!

SECOND EDIT: 第二编辑:

I think we should link these two questions: https://stackoverflow.com/questions/4989930/css-negative-margin and why use negative margins? 我认为我们应该链接这两个问题: https : //stackoverflow.com/questions/4989930/css-negative-margin为什么要使用负边距? to this one. 到这个。

Also See the W3C spec on margin: http://www.w3.org/TR/CSS2/box.html#margin-properties . 另请参阅W3C规范,以了解保证金: http : //www.w3.org/TR/CSS2/box.html#margin-properties Section 8.3.1 Might actually explain what is going on with your sample. 实际上,第8.3.1节可能会解释样本的处理情况。 A collapsing margin issue not rendering correctly in Safari. Safari中无法正常呈现的页边距崩溃问题。

ORIGINAL POSTING: 原始帖子:

So my questions are: 所以我的问题是:

1) Am I right in my understanding of what Safari does. 1)我对Safari的功能理解正确吗? Why do Chrome and Firefox render differently? 为什么Chrome和Firefox呈现不同的外观? Sounds like that might be it, but, really, who cares? 听起来可能就是这样,但是,实际上,谁在乎? You are not getting the results you want. 您没有得到想要的结果。 You should change your code unless you don't care about Safari users. 除非您不关心Safari用户,否则应该更改代码。

2) Are they just not decreasing the width of the parent div based on a negative margin-right? 2)他们只是不减少负的margin-right的父div的宽度? Probably, but again, not really important. 可能,但又不是很重要。

3) Would the proper correction to always have a margin-right lesser or equal to the width of a div in this case? 3)在这种情况下,是否要进行适当的校正以始终使margin-right小于或等于div的宽度? I would say yes. 我会说是的。 To fix the issue and get the results you want I would move the div with class E inside the right most div with class B2. 要解决此问题并获得所需的结果,我将将E类的div移动到最右边的B2类的div中。 Then float E to the right and remove the position, left and margin-right attributes. 然后将E浮动到右侧,并删除position属性,left属性和margin-right属性。

.E {
height: 50px;
width: 10px;
background-color: black;
float: right;
}

http://jsfiddle.net/uryJJ/32/ http://jsfiddle.net/uryJJ/32/

FIRST EDIT 第一次编辑

.D {
height: 40px;
width: 40px;
float:left;
background-color: yellow;
position:relative;
left: -10px;
}
.E {
height: 50px;
width: 10px;
position: relative;
left: -10px;
background-color: black;
float: left;
}

http://jsfiddle.net/uryJJ/33/ http://jsfiddle.net/uryJJ/33/

Sorry, I might be beating this to death but this fixes it: 抱歉,我可能会把它打死,但这可以解决此问题:

.E {
height: 50px;
width: 10px;
margin-left: -10px;
background-color: black;
float: left;
}

http://jsfiddle.net/uryJJ/35/ http://jsfiddle.net/uryJJ/35/

I was not a fan of negative margin values until just now. 直到现在,我都不喜欢负保证金值。

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

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