amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Allow page authors to propagate some of the styles to the iframe

Open dvoytenko opened this issue 10 years ago • 16 comments

This mainly concerns font family, style and size, but other style customizations are possible as well. Ideally, we will be able to reconcile this styling with Shadow DOM rules (see http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/).

Currently, iframes only inherit background color from the parent's context. However, other styles could be useful to ensure that the iframe looks similar to the parent page. We can consider two different types of style propagation:

  1. Inheritance. In this case, inheritable CSS properties (background color, font, etc) can be sent to the child iframe and child iframe would apply these properties to preserve the look and feel of the parent context. Our guide here is a set of CSS properties that are inherited by default and we can consult Shadow DOM spec for details.
  2. Child style customization. This is similar to Shadow DOM's ::shadow and /deep/ selectors that allow the parent context to change styles inside the child context.

A good place to start is the first category and a limited set of properties such as:

  • background - the background color is inherited automatically in a way when child's background is transparent. But other parts of background may or may not be preserved. However, this is most likely a "done" part.
  • color - color of text.
  • font-family. Obviously, one can't inherit font-family without @font-face, which could be a challenging part.
  • font-style, font-size, font-weight, font-variant.
  • line-height, letter-spacing
  • direction - a bit of a dangerous property, but could also be a driver in proper rtl/ltr support.

How this could look like in format:

<amp-iframe inherit-parent-styles></amp-iframe>

Other considerations:

  • How do we propagate the properties the first time? Simply via #fragment? E.g. #font-family=Arial&font-size=12px. Or via messaging?
  • Do we send updates, e.g. due to media queries? Ideally no - just use styles at initialization. Most of properties above do not normally change based on media queries.
  • How do we actually apply them on the parent change without breaking it? Do we just leave it up to the parent page completely?
  • How to avoid FOUC
  • Often fonts are protected by referrer

dvoytenko avatar Mar 28 '16 21:03 dvoytenko

/cc @mkhatib @sriramkrish85 @cramforce @ericlindley-g

dvoytenko avatar Mar 28 '16 21:03 dvoytenko

This is interesting—have you come across many cases where this would be directly useful, or is this more a dependency for other functionality we'd like to offer?

I'm assuming this would only work for same-origin iframes (?)

ericlindley-g avatar Mar 28 '16 21:03 ericlindley-g

Eric, no, the idea is to try to see if we can get this closer for cross-origin iframes.

dvoytenko avatar Mar 28 '16 21:03 dvoytenko

This is for native ads and similar stuff, right?

I think posting down the contents of the custom style sheet is reasonable (definitely via postMessage) if the container opted in. One thing to note is that this would become harder should we ever decide to de-inline the stylesheet.

cramforce avatar Mar 28 '16 22:03 cramforce

Curious if we have examples already in the wild of how this can be useful - or where we're seeing discrepancies in styles because of iframes. I am not sure how much ads providers allow customization - can people customize the font for adwords loaded for example?

mkhatib avatar Mar 29 '16 04:03 mkhatib

@mkhatib Based on the Sharethrough discussion happening elsewhere, I think this capability could be a really nice enhancement to things like native ads serving: http://www.sharethrough.com/resources/mobile-native-ad-examples/. Matching the style of the surrounding content is really important to achieving the desired effect.

@cramforce Regarding de-inlining the stylesheet, would we support both non-inline and inline? In that case, could we just say that if you want to take advantage of this feature, it's required that those styles are expressed inline?

rudygalfi avatar Mar 29 '16 17:03 rudygalfi

@cramforce - correct, it will be harder. But if we can get any events, we can always forward the message once we have the necessary info. Opt-in with messages for us will mean that the AMP page has to opt-in into this by adding an attribute, e.g. "propagate-styles". The child iframe will opt-in by sending "request-propagated-styles" message.

@mkhatib This is mostly for creatives that need to match the parent page's look and feel closer. Whether or not this is feasible or even possible is not yet known - we will need to prototype.

dvoytenko avatar Mar 29 '16 17:03 dvoytenko

@mkhatib Hi I'm an engineer from Sharethrough. One major advantage for publishers is that they can make changes to the design of their site and those styles will be reflected in their native ads.

Often times the teams doing the site design and the ads team are different and might not be talking directly, and in order to keep the native ads looking like the rest of the site, it's important that the styles are shared and inherited.

nuclearghost avatar Mar 30 '16 15:03 nuclearghost

@dvoytenko is this still valid since we moved away from using shadow DOM to friendly iframes? I know @szach would also love to have this feature.

jasti avatar Feb 10 '17 03:02 jasti

Yeah. This is exactly why it's relevant - iframes don't do it automatically. The problem with this we found is that it's rather hard to propagate fonts "as is" since the font definition itself (link) may need to be copied into iframes. It's often the case that such referrals denied by the font providers.

/cc @keithwrightbos @tdrl

dvoytenko avatar Feb 11 '17 01:02 dvoytenko

Assuming the AMP validated creative returned is 1st party to the publisher (meaning the publisher setup or approves the creative) where it is explicitly trusted by the publisher, then I see no reason why we couldn't allow the creative to be directly embedded within the page without any CSS isolation container (e.g. just placed directly within amp-ad element). This would allow as much inheritance of the publisher page style as possibly. The publisher would need to understand the risks of such an operation (e.g. CSS effecting their page).

The question would be what effect that has on integration points (e.g. amp-analytics variable isolation) and whether again these are a concern given the explicit trust between the publisher and the advertiser.

/cc @michaelkleber

keithwrightbos avatar Feb 12 '17 14:02 keithwrightbos

Rendering the ad directly inside the amp-ad element means no CSS isolation, which is usually not what advertisers want. Responsive creatives also use @media queries to detect their size; if we move them out of iframes then they accidentally detect the parent page's size instead.

I think @dvoytenko 's notion of forwarding style properties is better suited to ads' needs.

michaelkleber avatar Feb 13 '17 15:02 michaelkleber

We can go back to the idea of using shadow DOM in some cases. However, it's important to stress why we walked away from SD for ads originally: @media, vw, vh and many others are near-impossible to properly reconstruct with SD.

dvoytenko avatar Feb 14 '17 17:02 dvoytenko

I think this is mainly an advertising use case, so moving it over to @jasti's plate.

rudygalfi avatar Feb 18 '17 00:02 rudygalfi

Closing it for now. So far hasn't seen this requirements pop up.

lannka avatar Jul 02 '19 17:07 lannka

How about embedding videos from video-sharing platforms? It will be really useful to be able to change the player CSS so the colors and fonts match the whole page.

ShahriarKh avatar Aug 07 '21 22:08 ShahriarKh