Scorchsoft
YouTubeiframez-indexCSSjQueryWeb Development

Solution: Embedded YouTube z-index is ignored and is appearing above a fixed DIV.

Fix embedded YouTube iframe videos ignoring their z-index and the z-index of other div elements using both html and jquery.

1 min read
Screenshot of JavaScript source code open in a syntax-highlighted code editor, with line numbers down the left-hand gutter

In summary

An embedded YouTube video can sit on top of your fixed-position elements no matter what z-index you set, because the problem lives inside the iframe rather than in your own CSS. Adding ?wmode=transparent to the embed URL fixes it — either by hand on each embed, or site-wide with a short jQuery snippet.

We've all been there, you embed a Youtube video onto a website that has fixed position DIV elements within the page, scroll, and are greeted with a nasty effect of the video seemingly appearing above these fixed position components. You've probably tried changing the "Position" CSS property from relative to absolute, as well as changing the z-index value on the various HTML elements with no success.

The issue is actually down to the styling and handling of the video within the iFrame itself, so the above actions won't help. Fortunately there is a simple solution to this widespread problem:

HTML solution

Here is what the embed code may typically look like as provided by YouTube:

1<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/lzQgAR_J1PI" frameborder="0" wmode="Opaque">

Try taking the above code and add in ?wmode=transparent to the embedded link like this:

1<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/lzQgAR_J1PI?wmode=transparent" frameborder="0" wmode="Opaque">

Or, if you would like a more automated site-wide solution then you may want to try this following JQuery snippet to do the leg-work for you.

JavaScript Solution

1$(document).ready(function(){
2
3 $('iframe').each(function(){
4 var url = $(this).attr("src");
5 var char = "?";
6 if(url.indexOf("?") != -1){
7 var char = "&";
8 }
9
10 $(this).attr("src",url+char+"wmode=transparent");
11 });
12
13});

Voilà, your youtube video will now acknowledge its z-index and will appear below your other div elements that have a higher value.

Do you need a responsive website?

Read more

Share

Want to talk about your project?

Tell us what you’re trying to achieve and we’ll map the fastest credible path.