October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Use the CSS aspect-ratio Property

Use CSS aspect-ratio to give a box a preferred width-to-height proportion, especially when one dimension is automatic. Learn the syntax, image behavior, and sizing pitfalls.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set aspect-ratio to describe a box’s preferred width-to-height proportion—for example, aspect-ratio: 16 / 9; for a widescreen shape. It helps determine an automatic dimension; it does not override an element when both its width and height are explicitly set.

What aspect-ratio does

The ratio is width divided by height. Thus, 16 / 9 means sixteen parts wide for every nine parts high, while 1 means 1 / 1, or a square. The property supplies a preferred ratio to sizing calculations; it is not a command to set two fixed dimensions. The browser can use it when at least one relevant dimension is automatic, subject to other layout constraints. See MDN’s aspect-ratio reference.

As an Amazon Associate I earn from qualifying purchases.

Basic syntax and responsive examples

The property accepts auto, a ratio, or both. Its initial value is auto, and it is not inherited. A ratio is written as width, slash, then height; if the denominator is omitted, it defaults to 1.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
/* Square box */
.square {
  aspect-ratio: 1;
}

/* Widescreen region: the container determines width */
.video-frame {
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
}

/* Natural ratio for replaced content when available;
   otherwise use the preferred 3:4 ratio */
.media {
  aspect-ratio: auto 3 / 4;
}

For a non-replaced element such as a div, a common pattern is to let the containing layout determine its width and leave height automatic. The ratio can then contribute a preferred height. For example:

#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
.card {
  width: 100%;
  aspect-ratio: 4 / 3;
}

As the available width changes, the preferred sizing helps the card retain a 4:3 shape. This is not a guarantee that every final rendered box will have that exact shape: other sizing constraints and layout rules can affect its used dimensions. The practical behavior for different kinds of content is described in MDN’s guide to understanding and setting aspect ratios.

When the ratio affects size—and when it does not

  • One dimension is automatic: the declared ratio can help calculate that dimension from the other one.
  • Both width and height are explicitly set: the preferred ratio does not replace those specified sizes.
  • Other layout constraints apply: min/max sizes and the surrounding layout can influence the final used dimensions.

To preserve a shape responsively, make sure the dimension you expect the ratio to determine is not independently fixed. If both dimensions are fixed by your CSS or layout, changing aspect-ratio alone will not make the box resize to match it.

Using aspect-ratio with images and other replaced content

Images and similar replaced elements can have an intrinsic ratio supplied by their content. The value auto uses a natural ratio when one exists. A combined declaration such as aspect-ratio: auto 3 / 4 uses the natural ratio for replaced content when available, with the specified ratio serving as the preferred ratio otherwise.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Keep the element’s box sizing separate from how its content is displayed. A preferred ratio affects sizing calculations; it does not by itself promise that an image will be stretched or cropped to fill the box. Content fitting or cropping is a separate styling decision.

Account for box-sizing

Ratio calculations use dimensions associated with the element’s box-sizing. If you are matching a visual width and height precisely, check whether the relevant sizing includes or excludes padding and borders under your chosen box-sizing rule. A mismatch between the dimensions you expect and the box being sized can make the result appear off-ratio.

Troubleshooting unexpected proportions

  • The box ignores the ratio: check whether both width and height are explicitly specified. Leave the dimension you want calculated automatic.
  • The box is close, but not exact: inspect other layout constraints and min/max sizing that can affect the final dimensions.
  • An image does not fill or crop as expected: distinguish the image element’s box ratio from the image content’s intrinsic ratio; content fitting is a separate concern.
  • Padding or borders change the apparent result: verify box-sizing and which box dimensions are being compared.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need a screenshot of a page showing your CSS change, you can use ScreenshotNeo’s screenshot API instead of setting up a browser capture script. One GET request returns a screenshot; the request below saves a WebP image of the target page. See the ScreenshotNeo API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The Free plan includes 1,000 screenshots per month with no card required; paid plans start at $5 for 3,000 screenshots. Learn about ScreenshotNeo or sign up free for 1,000 screenshots a month, with no card.

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

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 *

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

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.