--- url: "https://web.dev/articles/aspect-ratio" titulek: "The CSS aspect-ratio property | Articles | web.dev" h1: "The CSS aspect-ratio property Stay organized with collections Save and categorize content based on your preferences." popis: "Maintaining aspect ratio within images and elements is now easier to achieve with the new aspect-ratio CSS property." jazyk: "en" kanonicka: "https://web.dev/articles/aspect-ratio" drobecky: ["Articles", "The CSS aspect-ratio property"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:27:28" znaku: 9242 slov: 1433 kodovani: "iso-8859-2" sha256: "1abd36cb61aa273b646de102a2ac53ed783e6d899b4b06a15624b053b08217c7" --- The CSS aspect-ratio property Stay organized with collections Save and categorize content based on your preferences. The CSS property that helps maintain spacing in responsive layouts. Una Kravets Aspect ratio Browser Support 88 88 89 15 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/aspect-ratio) Aspect ratio is most commonly expressed as two integers and a colon in the dimensions of: width:height, or x:y. The most common aspect ratios for photography are 4:3 and 3:2, while video, and more recent consumer cameras, tend to have a 16:9 aspect ratio. Two images with the same aspect ratio. One is 634 x 951px while the other is 200 x 300px. Both have a 2:3 aspect ratio. With the advent of responsive design, maintaining aspect ratio has been increasingly important for web developers, especially as image dimensions differ and element sizes shift based on available space. Some examples of where maintaining aspect ratio become important are: Creating responsive iframes, where they are 100% of a parent's width, and the height should remain a specific viewport ratio Creating intrinsic placeholder containers for images, [videos](http://fitvidsjs.com/), and embeds to prevent re-layout when the items load and take up space Creating uniform, responsive space for interactive data visualizations or SVG animations Creating uniform, responsive space for multi-element components such as cards or calendar dates Creating uniform, responsive space for multiple images of varying dimension (can be used alongside `object-fit`) Object-fit Defining an aspect ratio helps us with sizing media in a responsive context. Another tool in this bucket is the `object-fit` property, which enables users to describe how an object (such an as image) within a block should fill that block: Showcasing various `object-fit` values. See [demo on Codepen](https://codepen.io/una/pen/mdrLGjR). The `initial` and `fill` values re-adjust the image to fill the space. In our example, this causes the image to be squished and blurry, as it re-adjusts pixels. Not ideal. `object-fit: cover` uses the image's smallest dimension to fill the space and crops the image to fit into it based on this dimension. It "zooms in" at its lowest boundary. `object-fit: contain` ensures that the entire image is always visible, and so the opposite of `cover`, where it takes the size of the largest boundary (in our example above this is width), and resizes the image to maintain its intrinsic aspect ratio while fitting into the space. The `object-fit: none` case shows the image cropped in its center (default object position) at its natural size. `object-fit: cover` tends to work in most situations to ensure a nice uniform interface when dealing with images of varying dimensions, however, you lose information this way (the image is cropped at its longest edges). If these details are important (for example, when working with a flat lay of beauty products), cropping important content is not acceptable. So the ideal scenario would be responsive images of varying sizes that fit the UI space without cropping. The old hack: maintaining aspect ratio with `padding-top` Using `padding-top` to set a 1:1 aspect ratio on post preview images within a carousel. In order to make these more responsive, we can use aspect ratio. This allows for us to set a specific ratio size and base the rest of the media on an individual axis (height or width). A currently well-accepted cross-browser solution for maintaining aspect ratio based on an image's width is known as the "Padding-Top Hack". This solution requires a parent container and an absolutely placed child container. One would then calculate the aspect ratio as a percentage to set as the `padding-top`. For example: 1:1 aspect ratio = 1 / 1 = 1 = `padding-top: 100%` 4:3 aspect ratio = 3 / 4 = 0.75 = `padding-top: 75%` 3:2 aspect ratio = 2 / 3 = 0.66666 = `padding-top: 66.67%` 16:9 aspect ratio = 9 / 16 = 0.5625 = `padding-top: 56.25%` Now that we have identified the aspect ratio value, we can apply that to our parent container. Consider the following example: `
`. If the image sent has the same aspect
ratio, but not necessarily those exact pixel values, we could still use image
attribute values to set the ratio, combined with a style of `width: 100%` so
that the image takes up the proper space. All together that would look like: `