1

Context: Electron desktop app using Timeline.js which is embedding Video.js through an iFrame. The iFrame source is below.

I've been struggling with this for a few hours now: I need to vertically center the Video.js instance in the iFrame it is within. I can't hardcode values because the app (not only videojs) can go full screen.

Using the "brute force" CSS below, it "works" but obviously the black bars are not acceptable. I removed that an added a ref to the vjs vjs-16-9 CSS and it works great: sets up size based on content, resizes in full screen mode – all good except that I haven't been able to figure out how to center the vjs instance vertically in the iFrame.

Probably something simple but I'm new at all this.

.video-js {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%!important;
    height: 100%!important;
}

enter image description here

Using vjs-16-9 CSS

enter image description here

iFrame source

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Video.JS Example</title>
    <link href="../node_modules/video.js/dist/video-js.min.css" rel="stylesheet">
    <script src="../node_modules/video.js/dist/video.min.js"></script>

<style>
   html, body {
        height:100%;
        width:100%;
        padding: 0px;
        margin: 0px;
      }

    </style>
</head>

<body>
    <div>
        <video id="videoPlayer" class="video-js vjs-default-skin vjs-16-9" controls preload="auto">
        </video>
    </div>

    <script>
        function getParamValue(paramName) {
            var url = window.location.search.substring(1);
            var qArray = url.split('&');
            for (var i = 0; i < qArray.length; i++) {
                var pArr = qArray[i].split('=');
                if (pArr[0] == paramName)
                    return pArr[1];
            }
        }

        // grap the video & poster frame refs from url
        var videoSrc = getParamValue('videoSrc');
        videoSrc = "assets/videos/" + videoSrc;

        var poster = getParamValue('poster');
        poster = "assets/images/" + poster;

        videojs("videoPlayer", {}, function () {
            this.src(videoSrc);
            this.poster(poster);
            this.load();
        });

    </script>
</body>
</html>
spring
  • 18,009
  • 15
  • 80
  • 160

2 Answers2

3

Finally found a solution which doesn't seem fragile: Vertical align anything with just 3 lines of CSS.

Assigned to the div containing the videojs instance

.centerVertically {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

   <div class="centerVertically">
        <video id="videoPlayer" class="video-js vjs-default-skin vjs-16-9" controls preload="auto">
        </video>
    </div>
spring
  • 18,009
  • 15
  • 80
  • 160
0

Most likely , iframe has its own styling by its own. Hence , brute force Css is needed. But to answer your question regarding aligning center for your iframe. You might want to explore more on using flex , justify content, justify center.

There are also other alternative such as using float but float is not a good practice to use unless you really have no choice of using it. The reason is. Float might react differently and mess up the alignment at certain occasions ..

I hope my information helps. Cheers.