Sub topic 7
Sizing video and showing a poster image
This chapter assumes you know basic HTML attributes, CSS sizing, and how the video element loads a file. It covers how a video gets its size, how to reserve space before it loads, and how to show a poster image.
How a video gets its size
A video element is an inline element with a default size. Until the browser reads the file's metadata, it uses 300×150 pixels. After metadata loads, it uses the video's intrinsic size, which is the width and height stored in the file. If the page did not reserve space, the layout shifts at that moment.
You can set the size in two ways:
- The
widthandheightattributes, in pixels with no unit. - CSS properties such as
width,max-width,height, andaspect-ratio.
Setting both attributes lets the browser compute an aspect ratio before the file loads. That prevents the layout jump.
Making a video responsive
Fixed pixel sizes overflow small screens. This CSS makes the video follow its container, up to a limit, and keeps a 16:9 box:
video {
width: 100%;
max-width: 800px;
height: auto;
aspect-ratio: 16 / 9;
object-fit: contain;
background: #000;
}width: 100% fills the container. max-width stops it from growing too large. aspect-ratio reserves the box. background fills any empty area.
Fitting the picture with object-fit
If the box and the video have different ratios, object-fit decides what happens to the picture.
| Value | Effect |
|---|---|
| `contain` | Shows the whole video and adds bars (letterboxing) where needed. |
| `cover` | Fills the box and crops the edges. |
| `fill` | Stretches the video to the box and distorts it. This is the default. |
Use contain for content people need to see in full. Use cover for decorative backgrounds.
Showing a poster image
The poster attribute names an image that the browser shows before playback starts. Without it, the browser may show a blank box, or the first frame once it has loaded.
<video controls width="640" height="360" poster="poster.jpg" preload="metadata">
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
</video>Notes on poster:
- It is loaded separately from the video, so it appears quickly.
- It is useful with
preload="metadata"orpreload="none", because no frame has been downloaded yet. - Choose an image with the same aspect ratio as the video. Otherwise
object-fitapplies to it as well. - If the poster fails to load, the browser falls back to the first frame or an empty box.
- A poster is not a text alternative. Meaning that is only in the image must also be available in the surrounding text or a caption track.
Common mistakes
- Omitting both size attributes and
aspect-ratio, so the page jumps when metadata arrives. - Setting
heightin CSS to a fixed value whilewidthis fluid, which distorts the box. - Using a poster with a different ratio and getting cropped or letterboxed results.
- Writing
width="640px". The attributes take plain numbers.
Exercise
A video's file is 1920×1080. You set video { width: 100%; height: 300px; } in a container 600 pixels wide. The picture looks stretched. What property fixes this without cropping, and what value do you use?
Show answer
Set object-fit: contain. The default fill stretches the video to the 600×300 box. contain keeps the 16:9 ratio and adds bars at the sides. A better fix is to replace the fixed height with height: auto and aspect-ratio: 16 / 9.