Skip to main content
Version: 11.13.0

Video fit

When the player and the video have different aspect ratios, you can choose how the video fits inside the player. These examples use a 21:9 player for a 16:9 video. Switch between the values of the CSS object-fit property to see the difference:

  • contain (default) shows the whole video, with black bars on the sides.
  • cover fills the player, cutting off the top and bottom of the video.
  • fill fills the player by stretching the video.

Open Video UI​

Tip: open your browser's developer console and use the player and THEOplayer variables to interact with this demo.

Set the --theoplayer-video-object-fit CSS custom property on the UI. --theoplayer-aspect-ratio sets the shape of the player itself.

theoplayer-default-ui {
--theoplayer-aspect-ratio: 21 / 9;
--theoplayer-video-object-fit: cover;
}
Code
demo.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Video fit</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet" />
<style>
html,
body {
margin: 0;
padding: 0;
font-family: 'Noto Sans', sans-serif;
}

theoplayer-default-ui:not(:defined) {
display: inline-block;
box-sizing: border-box;
}

theoplayer-default-ui {
width: 100%;
font-family: 'Noto Sans', sans-serif;
background: #000;
/* Make the player wider than the 16:9 video, so the effect of each fit is visible. */
--theoplayer-aspect-ratio: 21 / 9;
/* How the video fits inside the player: contain (default), cover or fill. */
--theoplayer-video-object-fit: contain;
}
</style>
<script src="https://cdn.theoplayer.com/dash/theoplayer/THEOplayer.js"></script>
<script nomodule src="https://unpkg.com/@theoplayer/web-ui@2/polyfills"></script>
<script async src="https://unpkg.com/@theoplayer/web-ui@2"></script>
</head>
<body>
<!--
libraryLocation: For demonstration purposes, we use the THEOplayer CDN.
For production use, host the library yourself and update this URL and the script above.

licenseUrl: Point this to your license file, or replace it with a license property.
-->
<theoplayer-default-ui
configuration='{"libraryLocation":"https://cdn.theoplayer.com/dash/theoplayer/","licenseUrl":"../../../../theoplayer-license.txt","mutedAutoplay":"all"}'
source='{"sources":{"src":"https://cdn.theoplayer.com/video/big_buck_bunny/big_buck_bunny.m3u8","type":"application/x-mpegurl"},"poster":"https://cdn.theoplayer.com/video/big_buck_bunny/poster.jpg","metadata":{"title":"Big Buck Bunny"}}'
></theoplayer-default-ui>
<script src="../utils.js"></script>
<script>
const ui = document.querySelector('theoplayer-default-ui');
onParentMessage('video-fit', ({ fit }) => {
ui.style.setProperty('--theoplayer-video-object-fit', fit);
});
</script>
</body>
</html>

Default UI​

In the Default UI, set object-fit on the <video> elements of the player.

.theoplayer-skin video {
object-fit: cover;
}
Code
default-ui.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Video fit (Default UI)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdn.theoplayer.com/dash/theoplayer/ui.css" />
<script src="https://cdn.theoplayer.com/dash/theoplayer/THEOplayer.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}

/* Make the player wider than the 16:9 video, so the effect of each fit is visible. */
.player-container {
position: relative;
width: 100%;
aspect-ratio: 21 / 9;
}

.player-container .video-js {
width: 100%;
height: 100%;
}

/* How the video fits inside the player: contain (default), cover or fill. */
.theoplayer-skin video {
object-fit: contain;
}
</style>
</head>
<body>
<div class="player-container">
<div id="player" class="video-js theoplayer-skin"></div>
</div>
<script src="../utils.js"></script>
<script>
var player = new THEOplayer.Player(document.querySelector('#player'), {
libraryLocation: 'https://cdn.theoplayer.com/dash/theoplayer/',
licenseUrl: '../../../../theoplayer-license.txt',
mutedAutoplay: 'all',
});
player.source = {
sources: [{ src: 'https://cdn.theoplayer.com/video/big_buck_bunny/big_buck_bunny.m3u8', type: 'application/x-mpegurl' }],
poster: 'https://cdn.theoplayer.com/video/big_buck_bunny/poster.jpg',
metadata: { title: 'Big Buck Bunny' },
};

onParentMessage('video-fit', function (message) {
document.querySelectorAll('.theoplayer-skin video').forEach(function (video) {
video.style.objectFit = message.fit;
});
});
</script>
</body>
</html>