CSS <ratio> Data Type
The <ratio> data type is a ratio of two numbers, written as width / height, such as 16 / 9. It is used by aspect-ratio and in media queries.
Description
The <ratio> CSS data type represents a proportional relationship between two numbers, typically used to define an aspect ratio for elements. Unlike fixed units such as <length> or percentages, a <ratio> expresses a relative width-to-height relationship, allowing elements to scale dynamically while maintaining a specific proportion. This is particularly useful for responsive designs, where preserving the shape of an element, such as an image, video, or container, is important regardless of the screen size.
In practice, <ratio> is often employed in properties like aspect-ratio, which can accept values in the form of “width / height.” For example, setting an aspect ratio of 16:9 ensures that an element retains that rectangular shape even when resized:
.video-container {
aspect-ratio: 16 / 9;
width: 100%;
background-color: #000;
}
One of the advantages of the <ratio> type is that it allows for flexible, content-aware layouts without relying on fixed heights or complex JavaScript calculations. It can also interact seamlessly with other CSS features, such as min-width or max-height, enabling developers to constrain the scaling behavior while keeping the intended proportion intact.
Additionally, the <ratio> data type is not limited to integer values. Decimal ratios are valid, which allows for fine-tuning layouts:
.square-box {
aspect-ratio: 1 / 1; /* Perfect square */
width: 150px;
background-color: coral;
}
.banner {
aspect-ratio: 2.35 / 1; /* Cinematic banner ratio */
width: 100%;
background-color: #333;
}
By using <ratio>, designers can ensure consistent visual experiences across different devices, making it a powerful addition to the modern CSS toolbox.
Interactive Demo
Syntax
<ratio> = <number [0,∞]> [ / <number [0,∞]> ]?
Values
- <ratio>A positive <number> (the width), optionally followed by a solidus (/) and a second positive <number> (the height), with optional whitespace around the slash. Decimals are allowed, so 2.35 / 1 is valid. If the second number is omitted, it defaults to 1, so 1.5 is the same as 1.5 / 1.
Example
Browser Support
The following information will show you the current browser support for the CSS ratio data type. Hover over a browser icon to see the version that first introduced support for this CSS data type.
Desktop





Tablets & Mobile





