brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: aspect-ratio keeps a box at 16:9 without padding tricks

Web-Development
~/tips/css-aspect-ratio

.video {

Gives an element a fixed width-to-height ratio with one CSS line.

For years I would have reached for the padding-top hack to get a box with a fixed shape, such as a video frame. CSS has a property for it now.

.video {
  width: 100%;
  aspect-ratio: 16 / 9;
}

The browser works out the height from the width, so the box keeps its shape when the window gets narrower. It also works well for placeholders that should reserve space before an image or iframe has loaded.

Caveat: the ratio is a preference. If the content inside needs more room, the box grows and the ratio is gone, and an explicit height overrules it. Add overflow: hidden or min-height: 0 if the box must stay exactly that shape.

Further reading: MDN: aspect-ratio

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link