CSS Aspect-Ratio Box Sizing Visualizer

Set container dimensions, pick an aspect-ratio and object-fit mode, then watch the live pixel math update instantly. No guessing - exact crop, letterbox, and distortion numbers in real time.

Container Settings
Width
Aspect Ratio
/
Media Settings (Intrinsic Size)

The native pixel dimensions of your image or video - what the browser knows before any CSS is applied.

Common Presets
Object-Fit Controls
object-fit
object-position
Generated CSS
/* waiting for input */
Pixel Math Breakdown
Container (rendered)
-
width x height in px
Media aspect ratio
-
intrinsic W/H ratio
Crop / Loss
-
-
Extra Space / Distortion
-
-
Live Interactive Preview
Drag the corner handle to resize the container
cover center center 400 x 225 px
↕ Dashed teal border = container bounds. Orange dashes = cropped (invisible) media region.
Key Terms Explained
aspect-ratio
A CSS property that locks the width-to-height proportion of a box. Writing aspect-ratio: 16 / 9 tells the browser to compute the height automatically from whatever the width resolves to.
object-fit
Controls how a replaced element (img, video, canvas) fills its layout box. Values include cover, contain, fill, scale-down, and none - each with different cropping and scaling behavior.
object-position
Sets the anchor point for the rendered image inside its container. Accepts keywords (top, center, bottom, left, right) or percentages. Only has a visible effect when object-fit is cover or none.
Intrinsic Size
The natural pixel dimensions of a resource, defined by its content rather than CSS. A 1920x1080 JPEG has an intrinsic size of 1920 wide by 1080 tall regardless of how the containing box is sized.
Extrinsic Sizing
Dimensions that come from outside the element itself - from CSS, parent layout constraints, or the viewport - rather than from the element's own content. A div with width: 100% is extrinsically sized.
Letterboxing
The empty bars that appear above/below (or left/right of) an image when object-fit: contain is used and the container and image have different aspect ratios. The background color of the container shows through these gaps.
Responsive Design
An approach to building layouts that adapt fluidly to different viewport sizes. The aspect-ratio property is a key tool because it maintains proportional boxes without hard-coded pixel heights.
Replaced Element
An element whose content is defined outside CSS, such as an img, video, iframe, or canvas. These elements have an intrinsic size and respond to object-fit and object-position, which generic divs do not.

The Complete Guide to CSS Aspect Ratio and Object-Fit

If you have ever wrestled with a layout that looks perfect on a 1440px monitor and breaks completely on a 375px phone, you have felt the pain of hardcoded pixel heights. The CSS aspect-ratio property and object-fit family of values solve this at the browser level - no JavaScript, no padding hacks, no extra wrappers.

How to Use This Tool

  1. Set the container width in the Container Settings panel. Choose px for an absolute size or % for a fluid column width (the tool uses 800px as the reference container).
  2. Pick or type an aspect ratio. The container height is computed automatically from width divided by ratio.
  3. Enter the intrinsic dimensions of your media - the actual pixel size of the image or video file.
  4. Click an object-fit toggle to see how the browser would scale or crop the media. The pixel math cards update instantly.
  5. Adjust object-position to shift the crop anchor point - useful for keeping a face or logo in frame when using cover.
  6. Grab the resize handle (bottom-right corner of the teal dashed box in the preview) to manually resize the container and watch all numbers recalculate live.

Understanding the Pixel Math

When you use cover, the browser scales the image so its shorter side fills the container, then hides the overflow. This tool calculates exactly how many pixels are cropped from each axis so you know how much of your image is invisible.

With contain, the image shrinks until its longer side fits inside the container. The tool calculates the letterbox gap - the empty pixel height or width of the bars that appear on either side.

fill stretches the image to match the container exactly. Since no cropping or empty space occurs, the distortion is purely a ratio stretch. The tool expresses this as a percentage change in either axis.

scale-down behaves like contain if the image would need to be enlarged, or like none if the image is already smaller than the container. No scaling larger than the intrinsic size ever occurs.

none renders the image at its intrinsic dimensions with no scaling. If it is larger than the container, the overflow is hidden; if smaller, it floats at the object-position anchor surrounded by the container background.

The aspect-ratio Property vs. the Padding-Bottom Hack

Before aspect-ratio landed in browsers (broadly supported from mid-2021), developers faked responsive height using percentage padding. The trick relied on the fact that padding-top and padding-bottom percentages are calculated relative to the element's width, not its height. A 16:9 ratio required padding-bottom: 56.25% (9/16 = 0.5625) plus an absolutely-positioned inner element to hold actual content - awkward, fragile, and hard to read.

With aspect-ratio: 16 / 9 you write one property and remove the wrapper div entirely. The browser computes the height automatically whenever the width changes - whether from a CSS grid, flexbox, or viewport resizing.

FAQ

cover scales the image up until it fully fills the container, cropping whatever hangs outside the edges. No letterbox gap appears, but pixels outside the box are hidden. contain scales the image down (or up) until the entire image fits inside the container without cropping, which leaves empty space (letterboxing) on two sides whenever the container and image aspect ratios differ.
When you set explicit width and height on an img element without also setting object-fit, the browser defaults to object-fit: fill, which stretches the image to match the container exactly regardless of the image's intrinsic (native) proportions. The fix is to add object-fit: cover or object-fit: contain to preserve the original aspect ratio.
The classic padding-bottom trick (e.g. padding-bottom: 56.25% for 16:9) exploits the fact that percentage padding is relative to the parent width, forcing a responsive height. The modern aspect-ratio property (e.g. aspect-ratio: 16 / 9) does the same thing in one declaration without the extra wrapper div or the fragile math. Browsers that support aspect-ratio will automatically compute the height from the width, making the hack unnecessary.
aspect-ratio works on any block-level or replaced element, including div, section, article, span (when display is changed), img, video, iframe, and canvas. It is not limited to media elements. A common pattern is to set aspect-ratio on a container div and then use object-fit on an img or video child inside it.
object-position controls which part of the image is visible when object-fit: cover crops the image. The default is center center, showing the middle. Setting it to top keeps the top of the image in frame, bottom preserves the bottom, and left or right shift the focal point horizontally. Think of it as the anchor point for the crop window.