Sub topic 7
Sizing video and showing a poster image
This chapter assumes you can already write a video element with controls and source children. It also assumes you know basic CSS sizing: width, max-width, and the box model. Here you will control how large a video appears before and after it loads. You will also choose what viewers see before playback starts.
How a video gets its size
A video element is a replaced element, like img. Its content is drawn by the browser, so its size comes from a chain of fallbacks:
- CSS
widthandheight, if you set them. - The
widthandheightattributes, if present. - The intrinsic size of the video, which the browser learns once metadata loads.
- The size of the poster image, if there is one and no video data has arrived yet.
- A default of 300 by 150 pixels, if none of the above is known.
The last step explains a common bug. Without a size and without a poster, the box is 300 by 150 before metadata arrives. It then jumps to the real size and pushes the rest of the page down. You can prevent this by reserving space in advance.
Attributes versus CSS
The width and height attributes take unitless numbers, which the browser reads as CSS pixels. Write width="640", not width="640px". They change the displayed size only. They do not re-encode or shrink the file, so a 4K video shown at 640 pixels wide still downloads at full size.
If you set both attributes and the ratio differs from the video's own ratio, the video does not stretch. It is letterboxed (black bars) inside the box. That behavior comes from object-fit, covered below.
For responsive layouts, CSS is the better tool. This example comes from the plan for this tutorial:
video {
width: 100%;
max-width: 800px;
height: auto;
aspect-ratio: 16 / 9;
object-fit: contain;
background: #000;
}Each declaration has a job:
width: 100%makes the video fill its container.max-width: 800pxstops it from growing past a readable size on wide screens.height: autolets the height follow from the width.aspect-ratio: 16 / 9reserves a 16:9 box before any data loads, which prevents the layout jump described above.object-fit: containdecides how the picture sits inside that box.background: #000fills any bars with black instead of the page color.
Fitting the picture with object-fit
object-fit works on video the same way it does on images. It matters whenever the box and the video have different ratios.
| Value | Behavior | Typical use |
|---|---|---|
| `contain` | Scales to fit entirely inside the box and keeps the ratio. Bars may appear. | Content that must not be cropped, such as lectures or screen recordings |
| `cover` | Scales to fill the box and keeps the ratio. Edges are cropped. | Decorative background video |
| `fill` | Stretches to the box and ignores the ratio. | Rarely wanted, since it distorts the picture |
| `none` | Keeps the native size and crops overflow. | Special cases |
object-position shifts the picture inside the box. For example, object-position: center top keeps the top of the frame when cover crops it.
Showing a poster image
The poster attribute names an image that the browser shows in the video box before playback starts:
<video controls width="640" poster="poster.jpg" preload="metadata">
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
<p>Your browser cannot play this video. <a href="clip.mp4">Download it instead</a>.</p>
</video>Here is how the pieces work together:
- Without a poster, the browser typically shows nothing, or the first frame once it has loaded. That first frame is often black or a fade-in.
- With a poster, the viewer sees a deliberate image. The poster is also the size source in step 4 of the chain above, so the box has a sensible shape before the video loads.
- With
preload="none", no video data is fetched until the user plays. A poster is then the only thing in the box, so it is close to required. - With
preload="metadata", the browser fetches only enough to learn the duration and dimensions. The poster still appears until playback begins.
The poster attribute applies only to video. The audio element has no visual area, so it ignores it.
Choosing a good poster
- Match the video's aspect ratio, so no bars appear around the poster.
- Match the display size. A 1280-pixel-wide poster is enough for a box up to 800 CSS pixels wide on most screens.
- Use JPEG or WebP for photographic frames. Keep the file small, because it loads before anything else in the player.
- Keep text in the poster readable at the smallest size you display.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Page jumps when the video loads | No size or ratio reserved | Add `aspect-ratio`, or `width` and `height` attributes |
| Video is stuck at 300 by 150 | No size, no poster, and metadata not loaded | Set a size, or use `preload="metadata"` plus a poster |
| Black bars on the sides | Box ratio differs from video ratio | Match `aspect-ratio` to the file, or accept `contain` |
| Picture looks stretched | `object-fit: fill`, or both dimensions forced without `contain` | Use `contain` or `cover` |
| Poster never appears | Wrong path, or the image failed to load | Check the Network panel for a 404 on the poster URL |
| Video overflows a small screen | Fixed `width="1280"` with no CSS limit | Add `max-width: 100%; height: auto` |
| Huge download for a small box | Attributes shrink display only | Encode a smaller file, or serve several resolutions |
Exercise
A page shows a 16:9 video inside a div that is 400 pixels wide and 400 pixels tall. The CSS is video { width: 100%; height: 100%; object-fit: cover; }. A reviewer complains that the left and right edges of the picture are cut off. What is happening, and how would you change the CSS to show the whole frame without distortion?
Show answer
With a square box and a 16:9 video, object-fit: cover scales the video until it fills the box in both directions. The excess width is cropped, so the sides are cut off. Change it to object-fit: contain, which fits the whole frame and adds black bars above and below. Better still, set height: auto and aspect-ratio: 16 / 9 so the box matches the video and there are no bars.