Margin-collapsing can cause reflow with amp-auto-ads
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.
/cc @zhouyx
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.)
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
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.
Got it. Then I agree with you: we can apply a class that set overflow: auto to created #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.
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 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?
display: block; is also necessary.
@tlong2 any progress here? Thank you.
The fix was submitted on 3rd April by https://github.com/ampproject/amphtml/pull/8310. Should have updated the issue here sorry.