CSS aspect ratio padding calculator
This CSS aspect ratio calculator converts a width and height into the padding-top percentage used by the classic responsive box technique.
Run — free
Enter any positive dimensions, such as 16 and 9, and receive a ready-to-copy percentage string. The calculation is useful when maintaining older stylesheets, supporting browsers where a padding-based fallback is required, or generating embedded media wrappers from stored dimensions. It performs one deterministic calculation, keeps no data, and returns the same result in the browser and through the API.
Turn dimensions into responsive vertical space
A percentage used for vertical padding in CSS is resolved against the containing block's width. That behavior makes padding-top a practical way to reserve height before an image, video, iframe, or other embedded item loads. This calculator divides the supplied height by the supplied width, multiplies the result by one hundred, and appends the percent sign. For a widescreen box, the familiar 16 by 9 dimensions therefore produce a value that can be placed directly in a padding-top declaration. The dimensions do not need to be pixels and they do not need to describe a large asset. They only need to use the same unit because the calculation depends on their proportion. A 1600 by 900 asset, a 160 by 90 thumbnail, and a 16 by 9 design frame all reserve the same relative space. Use positive finite numbers for both fields; zero, negative values, missing values, and nonnumeric text are rejected rather than producing misleading or unusable CSS.
Apply the percentage in a CSS fallback
The calculated string is intended for the established intrinsic-ratio box pattern. Put the percentage on a wrapper's padding-top, give that wrapper a relative position, and position its child absolutely so the child fills the reserved area. The wrapper can then follow the available width while its vertical padding preserves the requested proportion. Modern CSS provides the aspect-ratio property, which is usually clearer for new code, but padding-based boxes remain relevant in legacy stylesheets, compatibility fallbacks, email-adjacent rendering systems, generated snippets, and codebases that already standardize on this technique. Treat the returned value as CSS text rather than as a new ratio. If your source dimensions are width 4 and height 3, enter them in that order; reversing them intentionally creates a portrait box instead of a landscape box. The tool does not inspect an image or infer orientation. It performs only the explicit conversion, making the output predictable and suitable for build scripts, template generators, and migration tools.
Choose dimensions and interpret precision
Use the intrinsic width and height when you want the wrapper to match a real asset, or use design dimensions when you are reserving a planned slot. Large dimension values are acceptable as long as they are finite positive numbers, but scaling both values down by the same factor makes no difference to the result. Decimal dimensions are also supported, which is useful when the ratio comes from measured layout data rather than an integer media size. The result is formatted compactly as a percentage string while retaining enough significant precision for practical CSS layout. Browsers may display subpixel rounding at particular viewport widths, so a mathematically precise percentage does not guarantee that every rendered edge lands on a whole device pixel. That is normal layout behavior, not a changed aspect ratio. When exact modern behavior is available, consider declaring aspect-ratio as the primary rule and using this calculated padding value only for the fallback. Through the API, each deterministic request costs $0.002; the browser version can perform the same pure calculation locally.
What you can do with it
Reserve space for responsive video
Convert a video's intrinsic dimensions into the padding-top value for a wrapper that prevents layout shift before the player loads.
Generate legacy aspect-ratio fallbacks
Create padding-based fallback declarations while a stylesheet migration adopts the modern aspect-ratio property progressively.
Build media embed templates
Turn stored width and height metadata into a percentage string when generating reusable iframe, image, or component markup.
FAQ
What formula does the calculator use?
It calculates height divided by width, multiplies that value by one hundred, and returns the result with a percent sign.
Why does vertical percentage padding preserve an aspect ratio?
CSS resolves percentage padding against the containing block's width, so the vertical space changes proportionally as that width changes.
Should I use padding-top or the aspect-ratio property?
Prefer the aspect-ratio property in modern code. Use the calculated padding-top value for compatibility, existing patterns, or generated fallbacks.
Can width and height be decimal numbers?
Yes. Both values may be integers or decimals, provided each is finite and greater than zero.
What happens if a dimension is zero or negative?
The request fails with an invalid input error because neither zero nor a negative dimension defines a valid responsive box.
How much does an API request cost?
Each API request costs $0.002. The same deterministic calculation is also available in the browser.
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/image/css-aspect-ratio \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"width":16,"height":9}'const res = await fetch("https://api.kit.forhosting.com/image/css-aspect-ratio", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"width": 16,
"height": 9
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/image/css-aspect-ratio",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"width": 16,
"height": 9
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/image/css-aspect-ratio", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"width":16,"height":9}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"width":16,"height":9}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/image/css-aspect-ratio", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"width": 16,
"height": 9
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "image.css_aspect_ratio",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
max_mb | 15 |
max_megapixels | 12 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |