amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Margin-collapsing can cause reflow with amp-auto-ads

Open tlong2 opened this issue 9 years ago • 10 comments

Adjacent <P> elements get their margins collapsed, if we insert a 0x0 <amp-ad> between two <P> elements with margins then we prevent this margin collapsing, which causes a reflow.

Info on margin collapsing: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing

We need to deal with this. The insert 0x0 <amp-ad> and then attemptChangeSize approach may not be the best way, as there are probably many edge cases where this can cause a reflow. I'll explore potential alternatives.

tlong2 avatar Mar 17 '17 17:03 tlong2

/cc @zhouyx

lannka avatar Mar 17 '17 17:03 lannka

This seems to be caused by the fact that even for a 0x0 <amp-ad> the 'i-amphtml-layout-size-defined' class name gets added. This sets overflow: hidden, which means that the <amp-ad> interferes with margin collapsing. Without this overflow: hidden it should all work fine.

@lannka / @zhouyx, is there a way we could make the 0x0 <amp-ad> be in a lame-state until after the attemptChangeSize request is successful? (i.e. prevent AMP trying to upgrade it or apply any special class names until <amp-auto-ads> is happy it's outside the viewport.)

tlong2 avatar Mar 20 '17 09:03 tlong2

What about only support layout.NODISPLAY? Then custom-element will not apply i-amphtml-layout-size-defined class. Then you can set display in auto-ads #buildCallback

zhouyx avatar Mar 20 '17 17:03 zhouyx

Just to clarify <amp-auto-ads> does not have a layout. We're talking about the <amp-ad>s that it injects.

They cannot have layout='nodisplay' when initially injected with 0 size as the <amp-ad> will end up with display: none and then the attemptChanegSize logic won't be able to determine where it is and make a decision about whether it can be resized.

tlong2 avatar Mar 20 '17 17:03 tlong2

Got it. Then I agree with you: we can apply a class that set overflow: auto to created element and remove it after #attemptChangeSize. as long as #layoutCallback is called after attemptChangeSize has resolved. BTW, what happen if attempt resize fail? I fail to find the logic, please help.

zhouyx avatar Mar 20 '17 18:03 zhouyx

Cool. What's the best way to achieve this? It would be best to block/delay the i-amphtml-layout-size-defined class getting applied, rather than having a priority war between two classes each defining "overflow: X !important". So something like an i-amphtml-layout-pending-sizing state?

If the attemptChangeSize fails, then we just leave the 0-size amp-ad in the DOM: https://github.com/ampproject/amphtml/blob/master/extensions/amp-auto-ads/0.1/placement.js#L185 We could clean-up and remove it from the DOM, but not sure what is better, as removing it incurs a cost.

tlong2 avatar Mar 20 '17 18:03 tlong2

@tlong2 I played with the JS bin a little bit. setting overflow: visible doesn't seem to work. http://jsbin.com/mewozoqegu/edit?html,css,output Am I setting anything wrong?

zhouyx avatar Mar 22 '17 21:03 zhouyx

display: block; is also necessary.

tlong2 avatar Mar 22 '17 22:03 tlong2

@tlong2 any progress here? Thank you.

adelinamart avatar Aug 23 '17 22:08 adelinamart

The fix was submitted on 3rd April by https://github.com/ampproject/amphtml/pull/8310. Should have updated the issue here sorry.

tlong2 avatar Aug 24 '17 13:08 tlong2