An anchor(<a href=“”>) wrapping a iframe (<iframe>) does not work

The issue:

In a situation where we had an anchor wrapping an iframe but the anchor was not working properly. The iframe was showing nicely. The anchor did wrap the iframe but when we clicked on it nothing happened. The anchor did not redirect to the proper location.

The reason:

See the code segment below:

<a href="">
  <iframe src="" width="100%">
      <p>Your browser does not support iframes.</p>

It did not work because it is not a valid HTML. The element iframe must not appear as a descendant of the anchor (<a>) element.

The solution:

We can solve this issue by z-index property. Instead of wrapping the iframe with anchor, we should put the anchor and iframe as siblings. Both the siblings should have position absolute and the same width and height.

At the end the anchor should have a higher z-index value than the iframe. Please check the code segment below:

<a href="" class="element theanchor"></a>
<iframe class="element theiframe" src="">
    <p>Your browser does not support iframes.</p>

// CSS