Aspect ratio transfer and replaced sizing
Definition and purpose
A preferred aspect ratio connects the inline and block dimensions when at least one size is automatic. Replaced elements such as images may supply natural dimensions and a natural aspect ratio. A non-replaced panel can acquire a preferred ratio through CSS.
Preferred panel ratio
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 160px; aspect-ratio: 2 / 1; background: #dbeafe; } </style> </head> <body> <div id="a">Panel</div> </body></html>The auto height is derived from the width.
Syntax and mental model
A 160px width and aspect-ratio: 2 / 1 produce an automatic height of 80px in the simple panel geometry, assuming no overriding constraints. With max-height: 50px and explicit width, the height is clamped; the ratio is preferred, not inviolable. An intrinsic 120 by 60 image with width 180px and auto height uses its natural 2:1 ratio. Constraints can transfer through a preferred ratio when sizing rules permit; two definite dimensions do not have to satisfy it.
Natural image ratio
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { display: block; width: 180px; height: auto; } </style> </head> <body> <img id="a" alt="Blue rectangle" src="data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 width=%27120%27 height=%2760%27%3E%3Crect width=%27120%27 height=%2760%27 fill=%27navy%27/%3E%3C/svg%3E"> </body></html>The SVG supplies intrinsic dimensions without network loading.
Common mistakes
Do not confuse object-fit, which fits image content inside its box, with aspect-ratio, which participates in box sizing. A ratio does not override every explicit size or minimum.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Definite and indefinite sizes. Next lesson: Formatting context edge cases.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Aspect ratio transfer and replaced sizing — knowledge check
Five questions on aspect ratio transfer and replaced sizing.
Which size follows a 2:1 ratio with width 160px?
Can max-height override a ratio-derived height?
Where does a natural image ratio come from?
What does object-fit change?
Must two explicit definite dimensions honor the ratio?