Firefox not rendering responsive YouTube embed properly

This method of embedding seems to be functioning well on all browsers except for Firefox; I took advantage of a free trial at crossbrowsertesting.com to verify. I’m not using a direct iFrame embed, and all the solutions I’ve come across are related to that particular issue. The method I am utilizing is detailed in this article: A Better Method for Embedding YouTube Videos on your Website.

It displays the thumbnail image of a YouTube video, and only loads the actual video player when the user clicks on the thumbnail manually.

The nearest relevant topic I found on Stack Overflow was about YouTube embed not working in Firefox, but it didn't address my specific situation.

Here are screenshots showing correct display on Chrome:

https://i.stack.imgur.com/XVWyE.jpg

And incorrect display on Firefox:

https://i.stack.imgur.com/cOeEi.png

In the Firefox snapshot, you can observe the margin appearing as I hover over that <div>.

Setting an explicit height value does make the thumbnail visible in Firefox, but it compromises the responsiveness of the method.

document.addEventListener("DOMContentLoaded",
        function() {
          var div, n,
            v = document.getElementsByClassName("youtube-player");
          for (n = 0; n < v.length; n++) {
            div = document.createElement("div");
            div.setAttribute("data-id", v[n].dataset.id);
            div.innerHTML = labnolThumb(v[n].dataset.id);
            div.onclick = labnolIframe;
            v[n].appendChild(div);
          }
        });
      
      function labnolThumb(id) {
        var thumb = '<img src="https://i.ytimg.com/vi/ID/hqdefault.jpg">',
          play = '<div class="play"></div>';
        return thumb.replace("ID", id) + play;
      }
      
      function labnolIframe() {
        var iframe = document.createElement("iframe");
        var embed = "https://www.youtube.com/embed/ID?autoplay=1";
        iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
        iframe.setAttribute("frameborder", "0");
        iframe.setAttribute("allowfullscreen", "1");
        this.parentNode.replaceChild(iframe, this);
      }
.youtube-player {
        position: relative;
        padding-bottom: 56.23%;
        /* Use 75% for 4:3 videos */
        height: 0;
        overflow: hidden;
        max-width: 100%;
        background: #000;
        margin: 5px;
      }
      
      .youtube-player iframe {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 100;
        background: transparent;
      }
      
      .youtube-player img {
        bottom: 0;
        display: block;
        left: 0;
        margin: auto;
        max-width: 100%;
        width: 100%;
        position: absolute;
        right: 0;
        top: 0;
        border: none;
        height: auto;
        cursor: pointer;
        -webkit-transition: .4s all;
        -moz-transition: .4s all;
        transition: .4s all;
      }
      
      .youtube-player img:hover {
        -webkit-filter: brightness(75%);
      }
      
      .youtube-player .play {
        height: 72px;
        width: 72px;
        left: 50%;
        top: 50%;
        margin-left: -36px;
        margin-top: -36px;
        position: absolute;
        background: url("//i.imgur.com/TxzC70f.png") no-repeat;
        cursor: pointer;
      }
<div class="youtube-player" data-id="VIDEO_ID"></div>

Answer №1

To fix the issue, I decided to eliminate unnecessary flexbox classes from the parent div. Specifically:

frow direction-column

(these were inherited from the popular flexbox grid framework called FrowCSS)

Though I'm not entirely sure why, it appears that these classes were causing conflicts with the required styles in FireFox. After spending 2-3 hours troubleshooting, I finally identified the source of the problem. Hopefully, this solution will be beneficial to others facing similar challenges in the future.

Answer №2

After encapsulating the youtube-player within a div and adjusting the display to block with a width of 100%, I was able to successfully resolve the issue at hand. Interestingly, applying these styles directly to the youtube-player element did not yield the desired outcome.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best approach to tackle this design?

As a newcomer to React, I'm facing challenges in positioning components. The layout I envision is consistent across all pages, with certain elements remaining the same. Should I utilize a grid system for this design? How should I approach achieving th ...

Eliminate any repeated elements within the nested arrays found within an array of objects

Looking for a way to eliminate duplicates in this array structure. While mapping over arr, the values from each nested array within the objects are retrieved. However, I want to address and filter out any duplicate values. Current Result: bye hello hello ...

Getting to a nested key in a JSON object with JavaScript: a step-by-step guide

Currently, I'm working with a JSON file and need to extract the fileName tag for use. { "dataset": { "private": false, "stdyDscr": { "citation": { "titlStmt": { "titl": "Smoke test", "IDNo": ...

Getting started with html2canvas: A beginner's guide

So here's a seemingly simple question... I'm diving into new territory and stumbled upon http://html2canvas.hertzen.com with a straightforward tutorial. After successfully running the command npm install -g html2canvas, I hit a roadblock. Where e ...

Display issues with CSS Absolute Positioning in IE11

I have encountered an issue with my html and css code that uses absolute positioning. While it functions correctly on Chrome and Firefox, I am facing a problem with Internet Explorer. The absolute position property in the SVG class does not seem to work in ...

serving files using express.static

I have set up express.static to serve multiple static files: app.use("/assets", express.static(process.cwd() + "/build/assets")); Most of the time, it works as expected. However, in certain cases (especially when downloading many files at once), some fil ...

The THREE.js WebGLRenderer canvas captures the 'click' mouse event

After including the domElement (canvas) from THREE.js WebGLRenderer in my document, I noticed that the 'click' mouse event no longer triggers when clicking on the container element containing the canvas. Is there a method to prevent the canvas f ...

When a user clicks on JavaScript, it will return true if clicked and false if

Currently working with selenium Java code and incorporating JavaScript execution using the executeScript method, as seen below: WebElement menu = driver.findElement(By.cssSelector(string1)); ((JavascriptExecutor) driver).executeScript("arguments ...

Encountering an issue with JQuery when attempting to create a double dropdown SelectList. Upon submitting the POST request, the output received is always

Two dropdownlists have been created, where one acts as a filter for the other. When selecting a customer from the dropdown Customer, only a limited set of ClientUsers is displayed in the dropdown ClientUser. This functionality is achieved using a jQuery fu ...

Is it necessary to have col-xs-12 in Bootstrap 3?

In order to achieve the desired display for different screen sizes, we have implemented the following code: <div class="col-md-6">left</div> <div class="col-md-6">right</div> When viewed on smaller screens than md, this code succe ...

What is the most effective method for utilizing JSON as a database?

For my upcoming offline mobile web app project, I am considering using JSON to mirror some of my database tables and storing the data in localStorage. (Although Web SQL Database is an option, its future-proofing capabilities are questionable.) Initially, ...

What is the most effective approach to handling dependencies for an AngularJs 1.x web application?

Right now, I have a bunch of script elements pointing to cdn/local files, which isn't ideal. I'm considering declaring all necessary packages using npm/yarn and serving cdn files with self-hosted fallback. Is this a good idea? Would it be bette ...

Ensure that the top of a div stays in place as the user scrolls, and spans the entire width of the div

My goal is to create a sticky or fixed position header inside a div with a list, but I'm facing an issue where it only sticks to the width of the text. If I expand the width to 100%, it takes up the size of the entire page: import styled from 'st ...

Is it possible to add a personalized parameter to an unnamed JavaScript replace function?

I am looking to customize a date value in the following format: var d = new Date(); myobj.format(d, "dddd (ddd) S dd'd'.MM (MMM MMMM).yyyy HH:mm:ss.fff t tt T TT (o) {Z}"); I prefer not to utilize date.js because of its large size. The issue ...

Discovering a particular string within a jQuery array object: Tips and tricks

One thing that is confusing me is the jQuery array object. To explain further: I have two arrays, one called brandsLink and the other called floorLink. When a user clicks on a link, I am saving the brand name in a variable called brandName, and then checki ...

HTML does not occupy the entire width of the page

I'm struggling with an issue on my website where the <html> element doesn't span full width on mobile devices. Currently, I am using Bootstrap and Jquery for my website. If you have any insights on what may be causing this problem, please ...

React hook form submit not being triggered

import React, { useState } from "react"; import FileBase64 from "react-file-base64"; import { useDispatch } from "react-redux"; import { makeStyles } from "@material-ui/core/styles"; import { TextField, Select, Input ...

Storing the subscription value retrieved from an API in a global variable

I am trying to find a way to make the data retrieved from an API accessible as a global variable in Typescript. I know that using subscribe() prevents this, so I'm looking for a workaround. Here is the API code: getResultCount(category:any):Obs ...

Unable to transform socket.io event into Bacon EventStream

After successfully binding the event on socket.io, the following code was executed: io = require('socket.io')() io.on 'connection', (socket) -> console.log socket.id io.listen 3000 An attempt was made to convert the socket.io ...

The Vue v-bind:class feature does not always update immediately when the value of the binded object is changed

When utilizing Vue, it seems that developers often use v-bind:class to add dynamic classes to elements. However, in the example below, this method appears to not function as expected. //Html <div id="mainapp"> <span class="star" v-bind:class="{ ...