'How to place an image over iframe using proper css

I am trying to place an image(which is a close button) on iframe at the top-right corner, the iframe and image are loaded from js function in angular, I have placed it correctly by some CSS but the issue is when the screen is responsive or on the tab or mobile view it doesn't appear in the correct place

Below is the Html code:

<div style="
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    transition: all 0.3s ease-in-out 0s;
    z-index: 100;
    background: rgba(0, 0, 0, 0.7);
  ">
  <iframe style="
      display: inherit;
      z-index: 10001;
      position: absolute;
      transition: transform 0.5s ease-in-out 0s;
      transform: scale(1);
      opacity: 1;
      top: 40px;
      left: 50%;
      margin-left: -200px;
      background-color: rgb(255, 255, 255);
    " src="test.html" id="iframe-overlay" title="iframe">
  </iframe>
  <img style="
      transition: all 0.5s ease-in-out 0s;
      opacity: 1;
      float: left;
      position: absolute;
      top: 23% !important;
      left: 48% !important;
      transform: translate(-50%, -50%) !important;
      z-index: 99999999;
      margin-left: 192px;"
      id="close-overlay"
    " src="assets/images/pink_hair_sml.png" />
</div>

The image should be placed at the marked position even if the screen gets minimized or maximized the image should be placed at the same position.

enter image description here

here is how it should look

enter image description here

but when I reduce the screen to 75%

enter image description here

this is how it looks

I am able to fix it for each screen but still when the screen gets minimized or maximized position is not placed correctly

here is parent of the element

enter image description here



Solution 1:[1]

If you add a div that wraps the 2, you can position that using flex in relation to your to div like so:

<div style="
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    transition: all 0.3s ease-in-out 0s;
    z-index: 100;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
  ">
  <div style="position: relative; max-width: 50%">
    <iframe style="
      display: inherit;
      z-index: 10001;
      transition: transform 0.5s ease-in-out 0s;
      transform: scale(1);
      opacity: 1;
      background-color: rgb(255, 255, 255);
    " src="test.html" id="iframe-overlay" title="iframe">
    </iframe>
    <img style="
      transition: all 0.5s ease-in-out 0s;
      opacity: 1;
      float: left;
      position: absolute;
      top: 0px;
      right: 0px;
      transform: translate(-50%, -50%) !important;
      z-index: 99999999;
      id="close-overlay"
    " src="assets/images/pink_hair_sml.png" />
  </div>
</div>

Alternatively, positioning the wrapping element itself which means you don't have to add a new div, see below. The disadvantage of this is that there's then space not covered by the div itself and so whatever's underneath then shows through.

<div style="
    position: fixed;
    width: 50%;
    height: 100%;
    top: 0px;
    left: 25%;
    transition: all 0.3s ease-in-out 0s;
    z-index: 100;
    background: rgba(0, 0, 0, 0.7);
  ">
    <iframe style="
      display: inherit;
      z-index: 10001;
      transition: transform 0.5s ease-in-out 0s;
      transform: scale(1);
      opacity: 1;
      background-color: rgb(255, 255, 255);
      width: 100%;
    " src="test.html" id="iframe-overlay" title="iframe">
    </iframe>
    <img style="
      transition: all 0.5s ease-in-out 0s;
      opacity: 1;
      float: left;
      position: absolute;
      top: 10px;
      right: 10px;
      z-index: 99999999;
      id="close-overlay"
    " src="assets/images/pink_hair_sml.png" />
</div>

Solution 2:[2]

The positioning of the two elements (iframe and img) is pretty straightforward as we know the width of the iframe (it is picking up the default which is set at 300px by browsers see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe ).

However it is rendered somewhat complex by the rather strange set of styles applied inline to the iframe and (worse) to the img where a couple are set !important and therefore not overwritable by our own CSS.

This snippet just gets rid of all the inline styling on those two elements using Javascript and starts again.

It positions each element independently. The iframe is centered and set at 40px from the top of its positioned parent. The img is positioned to just after the right hand edge of the iframe, and then transitioned back by half its width and height to get the overlap.

<!doctype html>
<html>

<head>
</head>

<body>
  <div id="GlobalPayments-overlay-02d30efa" style="
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    transition: all 0.3s ease-in-out 0s;
    z-index: 100;
    background: rgba(0, 0, 0, 0.7);
  ">
    <iframe style="
      display: inherit;
      z-index: 10001;
      position: absolute;
      transition: transform 0.5s ease-in-out 0s;
      transform: scale(1);
      opacity: 1;
      top: 40px;
      left: 50%;
      margin-left: -200px;
      background-color: rgb(255, 255, 255);
    " src="test.html" id="iframe-overlay" title="iframe">
  </iframe>

    <img style="
      transition: all 0.5s ease-in-out 0s;
      opacity: 1;
      float: left;
      position: absolute;
      top: 23% !important;
      left: 48% !important;
      transform: translate(-50%, -50%) !important;
      z-index: 99999999;
      margin-left: 192px;" id="close-overlay" " src="assets/images/pink_hair_sml.png " />
</div>
<style>
body {
  width: 100vw;
}
#GlobalPayments-overlay-02d30efa {
    --iframeW: 300px; /*the defaults set by browsers https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe*/
    --iframeH: 150px;
    }
#GlobalPayments-overlay-02d30efa iframe {
      transition: transform 0.5s ease-in-out 0s;
      transform: scale(1);
      opacity: 1;
    background-color: rgb(255, 255, 255);
    top: 40px;
    margin: 0;
    padding: 0;
    left: calc(50% - (var(--iframeW) / 2));
    position: absolute;
}

#GlobalPayments-overlay-02d30efa img {
  position: absolute;
  top: 40px;
  left: calc(50% + (var(--iframeW) / 2));
  transform: translate(-50%, -50%);
  margin: 0;
  padding: 0;
  width: 16px;
  height: 16px;
}
</style>
<script>
const iframe = document.querySelector('iframe');
const img = document.querySelector('img');
  iframe.style = '';
  img.style = '';
</script>
</body>

Solution 3:[3]

After all your comments that finally explains that you cannot change the HTML code we start to understand what you need!

You have to override the inline CSS rules with the help of the !important operator. The big problem will be on the close image because it already has some !important rules in the inline CSS, which is bad news for us... But you can use JavaScript to correct the horrible HTML generated.

For the CSS itself, I prefer putting all items in position fixed and use % to position them.

The close image should not be set from the left but from the right. But as said before this will be tricky because the inline CSS already has some !important rules. But at least we can remove the float and left margin. Finally, after overriding during the battle, the simpliest was just to remove the inline style attribute with JS. You'll have to find if you can hook somewhere in the JS library. If you cannot then you could run the JS every half a second until it finds the close image, like I did (but it's not a nice solution).

By the way, it would be good to set the image size in the CSS so that it displays at the correct place and size before the image itself is downloaded.

/*
In your case, you'll have to run this once the
iframe is visible. See if you can hook somewhere.
Look at the JS library you are using or if you can
then replace it by one not generating all this
horrible HTML with inline styles.

Here, just for the demo, I finally find a solution
to run it once the document is loaded and also each
time a change is detected, typically the case of the
user clicking on a button or link to display the
iframe overlay. This avoids the solution of a timer
running all the time.
*/

/**
 * Correct the close button of the iframe displayed on overlay.
 */
function correctIframe() {
  let closeImg = document.querySelector('#iframe-overlay ~ img:not(.cleaned)');
  if (closeImg) {
    closeImg.setAttribute('style', '');
    closeImg.setAttribute('class', 'cleaned');
  }
}

// Once the document is loaded we can try to see if there's
// already an iframe overlay to correct it.
document.addEventListener('DOMContentLoaded', correctIframe);

// Each time the document changes (typically if some JS does
// something such as triggering an Ajax call, loading new HTML,
// or simply displaying the iframe overlay) then we also have
// to correct it.
let domInsertionObserver = new MutationObserver((mutation) => {
  correctIframe();
});
domInsertionObserver.observe(document, { childList: true });
#iframe-overlay {
  z-index: 1000 !important;
  position: fixed !important;
  top: 10% !important;
  left: 10% !important;
  width: 80% !important;
  height: 80% !important;
  margin: 0 !important;
}

/* An image tag just after the iframe. */
#iframe-overlay ~ img {
  visibility: hidden; /* To avoid seeing it at bad position. */
  z-index: 1001 !important;
  position: fixed !important;
  /*
    We cannot override the left attribute
    because it has !important in the inline
    rule! Bad! We cannot override the transform
    either! Even worth! So JavaScript can only
    save us by deleting the inline style attribute.
  */
  top: 10% !important;
  right: 10% !important;
  /* Adjust to the image size to avoid image
     being resized during the download step. */
  width: 30px;
  height: 30px;
  /* Move half of the image size to right and down. */
  transform: translate(50%, -50%) !important;
}

#iframe-overlay ~ img.cleaned {
  visibility: visible;
}
<div style="
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    transition: all 0.3s ease-in-out 0s;
    z-index: 100;
    background: rgba(0, 0, 0, 0.7);
  ">
  <iframe style="
      display: inherit;
      z-index: 10001;
      position: absolute;
      transition: transform 0.5s ease-in-out 0s;
      transform: scale(1);
      opacity: 1;
      top: 40px;
      left: 50%;
      margin-left: -200px;
      background-color: rgb(255, 255, 255);
    " src="test.html" id="iframe-overlay" title="iframe">
  </iframe>
  <img style="
      transition: all 0.5s ease-in-out 0s;
      opacity: 1;
      float: left;
      position: absolute;
      top: 23% !important;
      left: 48% !important;
      transform: translate(-50%, -50%) !important;
      z-index: 99999999;
      margin-left: 192px;"
      id="close-overlay"
    " src="assets/images/pink_hair_sml.png" />
</div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 A Haworth
Solution 3 marc_s