Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

CSS aspect-ratio: When to replace the padding-bottom hack

Use CSS aspect-ratio for responsive frames instead of deriving height from percentage padding. See how sizing, intrinsic media ratios, object-fit, and legacy support affect the switch.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Replace the percentage padding-bottom ratio trick with CSS aspect-ratio for modern responsive layouts. For example, width: 100%; aspect-ratio: 16 / 9; gives an element a preferred 16:9 shape while its height remains automatic.

How to replace the padding-bottom ratio hack

  1. Set the desired ratio directly on the element. For a responsive 16:9 frame:

    As an Amazon Associate I earn from qualifying purchases.

    .media-frame {
      width: 100%;
      aspect-ratio: 16 / 9;
    }
  2. Keep at least one dimension automatic so the preferred ratio can affect sizing. If width is constrained by its container, the browser can calculate the height from the ratio.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  3. Remove the old percentage padding used solely to create height. Keep padding only if the element genuinely needs interior spacing.

    #1 Best Overall
    Sale
    HTML and CSS: Design and Build Websites
    • HTML CSS Design and Build Web Sites
    • Comes with secure packaging
    • It can be a gift option
  4. Check the result at the project’s target viewport sizes and against its required browser support.

What aspect-ratio changes—and what it does not

The aspect-ratio property expresses a preferred width-to-height relationship. It participates in sizing when at least one of width or height is automatic. If both dimensions are explicitly fixed, the ratio does not override them.

The ratio belongs to the box; it does not determine how an image or video is cropped or letterboxed inside it. For replaced elements such as images, intrinsic dimensions can also affect the result. MDN documents aspect-ratio: auto 16 / 9 as a way to use the supplied ratio until the content loads, then use the element’s intrinsic ratio.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make media fill the frame when needed

Setting a frame’s ratio does not by itself stretch or crop its contents. If media must fill a frame, size and position the media separately, then choose object-fit based on the desired result.

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
.media-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
}

.media-frame img,
.media-frame video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

object-fit: cover fills the frame by cropping excess content when necessary. Use object-fit: contain when preserving the full image or video matters more than filling every part of the frame; empty space may remain.

Why padding-bottom used to create a ratio

In horizontal writing modes, percentage padding is calculated from the containing block’s inline size—normally its width—not its height. So padding-bottom: 56.25% adds bottom padding equal to 56.25% of the available width. With no other content height, that produces a box approximately 16:9.

The technique was useful for responsive video and media wrappers before native ratio support was broadly available. It works, but its sizing logic is less obvious: a vertical padding value is being used to derive height from width. aspect-ratio: 16 / 9 states that relationship directly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When a fallback is still worth considering

MDN marks aspect-ratio as Baseline and widely available across browsers since September 2021. That is broad support, not a guarantee for every browser version a project may need to serve. Check MDN’s compatibility table against the oldest required browser before removing a fallback.

If legacy browser support is required, retain an appropriate fallback for those browsers; otherwise, the native property is the clearer default. Avoid keeping the padding trick automatically when the project’s support requirements no longer call for it.

References

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.