amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

<amp-fit-text> z-index issue

Open jyn15 opened this issue 6 years ago • 10 comments

What's the issue?

<amp-fit-text> adds z-index:2 CSS to the text container which breaks the rendering.

How do we reproduce the issue?

<!doctype html>
<html ⚡>
<head>
  <script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>
  <meta charset="utf-8">
  <title>amp-fit-text</title>
  <link rel="canonical" href="https://amp.dev/documentation/examples/components/amp-fit-text/index.html">
  <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
  <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <style amp-custom>
    .container {
      width: 150px;
      height: 150px;
    }
    .foreground {
      background: orange;
    }
    .foreground, .text {
      position: absolute;
      height: 100px;
      width:  100px;
    }
  </style>
  </head>
  <body>
    <div class="container">
      <div class="text"><div>Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.</div></div>
      <div class="foreground"></div>
    </div>
    <div class="container">
      <div class="text"><amp-fit-text width="1" height="1" layout="responsive">Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.</amp-fit-text></div>
      <div class="foreground"></div>
    </div>
  </body>
</html>

This is a simplified version of an actual AMPHTML ad. Without <amp-fit-text>, the text is hidden behind the foreground div, which is expected. With <amp-fit-text>, the z-index:2 CSS brings the text to the front.

Is there any reason for applying the z-index:2 style on the text container? https://github.com/ampproject/amphtml/blob/950ebeaa539a03775d7e1e56a5bb3fd61ea67e7a/extensions/amp-fit-text/0.1/amp-fit-text.js#L55

What browsers are affected?

All browsers

Which AMP version is affected?

2002200031230

jyn15 avatar Feb 26 '20 01:02 jyn15

@dvoytenko or @zhouyx do you have any context on this?

wassgha avatar Feb 27 '20 06:02 wassgha

I don't have any context. https://github.com/ampproject/amphtml/blob/master/css/Z_INDEX.md maybe helpful?

zhouyx avatar Feb 27 '20 18:02 zhouyx

I can see why that element needs a z-index for some of its contents. But is there a problem with that? Doesn't it define its own stacking context? If not - that should probably just needs to be fixed.

dvoytenko avatar Feb 27 '20 21:02 dvoytenko

Relatively/absolutely positioned element only creates stacking context when it has a z-index value other than 'auto': https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context so if other elements in the document do not define z-index and only amp-fit-text has z-index:2, the amp-fit-text's text container will always be the top-most element.

A more serious problem causing by the z-index:2 is that the amp-fit-text could be blocking a clickable element and make the AMPHTML ad unclickable. For more context: we are converting HTML5 ads to AMPHTML ads, we encountered this z-index issue while we were trying to recreate the same HTML structure in AMPHTML. Here is a simplified example showing the unclickable issue:

<!doctype html>
<html amp4ads>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <title>My AMP AD</title>
    <style amp4ads-boilerplate>body{visibility:hidden}</style>
    <script async src="https://cdn.ampproject.org/amp4ads-v0.js"></script>
    <script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>
    <script async custom-element="amp-ad-exit" src="https://cdn.ampproject.org/v0/amp-ad-exit-0.1.js"></script>
    <title>amp-fit-text</title>
    <style amp-custom>
      .container {
        width: 150px;
        height: 150px;
      }
      .taparea {
        background: rgba(0,0,0,0.05);
      }
      .taparea, .text {
        position: absolute;
        height: 140px;
        width:  140px;
      }
    </style>
  </head>
  <body>
    <amp-ad-exit id="exit-api">
      <script type="application/json">{
        "targets": {
          "redirectUrl": {
            "finalUrl": "https://www.google.com"
          }
        }
      }</script>
    </amp-ad-exit>
    <div class="container">
      <div class="text"><div>This gray area is clickable</div></div>
      <div class="taparea" on="tap:exit-api.exit(target=redirectUrl)" role=button tabindex=0></div>
    </div>
    <div class="container">
      <div class="text"><amp-fit-text width="1" height="1" layout="responsive">It becomes not clickable with amp-fit-text</amp-fit-text></div>
      <div class="taparea" on="tap:exit-api.exit(target=redirectUrl)" role=button tabindex=0></div>
    </div>
  </body>
</html>

jyn15 avatar Mar 20 '20 17:03 jyn15

Here is a link to the above example: https://amp-playground.appspot.com/amp/5664378560970752#runtime=amp4ads&mode=300x600

jyn15 avatar Mar 20 '20 17:03 jyn15

@jyn15 I agree that this is inconvenient. And I also believe the z-index for the display area is not really necessary there. Though it might be necessary for the measuring element.

/to @ampproject/wg-ui-and-a11y for triage.

dvoytenko avatar Mar 24 '20 01:03 dvoytenko

Wassim, can you take a look?

kristoferbaxter avatar Mar 24 '20 02:03 kristoferbaxter

@kristoferbaxter Taking over.

@jyn15

I agree that this may be inconvenient, will take a look.

For your particular case, since the boxes are stacked, I believe this would workaround this issue:

.text {
  pointer-events: none;
}

alanorozco avatar Mar 27 '20 16:03 alanorozco

Thanks Alan! I will use pointer-events:none to work around the unclickable issue for now.

jyn15 avatar Mar 27 '20 20:03 jyn15

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 06 '21 17:10 stale[bot]