How do I align a video background to be centered without a cutoff?

Viewed 124

Basically, I coded this player for my personal video hosting site (some elements censored in this example) but when I embed a video, it aligns to the left side and gets cut off at the right side or has a black bar there if the ratio is off.

(I embedded a video from Archive.org for this example, it is by no means my own. It appears to belong to Scott Draves.)

    function vidplay() {
       var video = document.getElementById("SubjectVideo");
       var button = document.getElementById("play");
       if (video.paused) {
          video.play();
       } else {
          video.pause();
       }
    }

    function restart() {
        var video = document.getElementById("SubjectVideo");
        video.currentTime = 0;
    }

    function skip(value) {
        var video = document.getElementById("SubjectVideo");
        video.currentTime += value;
    }      
    function hideControls() {
  var x = document.getElementById("controls");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  font-family: Arial;
  font-size: 17px;
  background-color: black;
}
#SubjectVideo {
  position: fixed;
  height: 100%;
}
.content {
  position: fixed;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  color: #f1f1f1;
  width: 100%;
  padding: 20px;
}
#myBtn {
  width: 200px;
  font-size: 18px;
  padding: 10px;
  border: none;
  background: #000;
  color: #fff;
  cursor: pointer;
}
#myBtn:hover {
  background: #ddd;
  color: black;
}
#Data {
  display: flex;
  flex-wrap: wrap;
}
.EpisodeData {
    text-align: right;
    flex: 50%;
}
#hideButtons {
    text-align: left;
    flex: 50%;
}
<!DOCTYPE html>
<html>
<head>
<title>[Video title]</title>
<link rel="icon" href="../../../../favicon.ico" type="image/png" sizes="16x16">
<link rel="stylesheet" href="../../../player.css">
<script src="../../../RedirectScripts/redirect.js"></script>
<script src="../../../player.js"></script>
</head>
<body id="main" onLoad="hideControls()">
<video autoplay id="SubjectVideo" onended="NextVideo()">
  <source src="https://ia801507.us.archive.org/35/items/electricsheep-flock-247-52500-0/00247%3D52640%3D52374%3D52639.mp4" type="video/mp4">
  Your browser doesn't support < video >..
</video>
<div class="content">
  <div id="controls">
    <div id="buttonbar">
      <p><center><img src="../../logo.png" width="20%"></p>
      <button id="restart" onclick="restart();"><img src="../../../restart.png" alt="restart"></button> 
      <button id="rew" onclick="skip(-30)"><img src="../../../rewind.png" alt="rewind 30s"></button>
      <button id="play" onclick="vidplay()"><img src="../../../play.png" alt="play/pause"></button>
      <button id="fastFwd" onclick="skip(30)"><img src="../../../fastforward.png" alt="fast-forward 30s"></button>
    </div>
  </div>
  <div id="Data">
    <div class="hideButtons">
      <button id="hide" onclick="hideControls()">Show/Hide Controls</button>
    </div>
    <div class="EpisodeData">
      Video Title
    </div>
  </div>
</div>
</body>
</html>

When I insert a tag around the video, it puts the left edge of the video on the edge instead of properly centering it. does anyone know how I could effectively center it, and put borders on the bottom and top instead of having a cutoff on the sides?

The error at the end is for an autoplay script that isn't linked here, it can be disregarded.

2 Answers

Add the next styles to the video tag

    left: 0;
    right: 0;
    margin: auto;

Add this #SubjectVideo ruleset to center your <video> tag:

top: 0;
left: 0;
right: 0;
bottom: 0;
margin-right: auto;
margin-left: auto;

It should look like this:

#SubjectVideo {
    position: fixed;
    height: 100%;
    margin-right: auto;
    margin-left: auto;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
Related