Toolmingo
Guides6 min read

Aspect Ratio Calculator: What It Is and How to Calculate It

Learn what aspect ratio means, how to calculate it from width and height, simplify it to its lowest terms, and convert between common ratios. Includes code examples in JavaScript, Python, Go, and PHP.

Aspect Ratio Calculator: What It Is and How to Calculate It

Aspect ratio is the proportional relationship between a width and a height. It tells you the shape of a rectangle — whether it is wide, square, or tall — without tying you to specific pixel dimensions. A 1920×1080 frame and a 1280×720 frame are different sizes but share the same aspect ratio: 16:9.

This guide explains the math, the common ratios you will encounter, and how to implement an aspect ratio calculator in four languages.


The Formula

Aspect ratio = Width : Height  (simplified by dividing both by their GCD)

Example: an image that is 1920 × 1080 pixels.

GCD(1920, 1080) = 120
1920 / 120 = 16
1080 / 120 =  9
Aspect ratio = 16:9

The greatest common divisor (GCD) is the key. Dividing both dimensions by the GCD gives the simplest integer ratio that describes the shape.


Finding a Missing Dimension

Given two of the three values (width, height, ratio) you can always find the third.

Known Unknown Formula
Width + ratio (W:H) Height Height = Width × H / W
Height + ratio (W:H) Width Width = Height × W / H
Width + Height Ratio GCD → W/GCD : H/GCD

Example: you need a thumbnail that is 800 px wide in a 16:9 ratio.

Height = 800 × 9 / 16 = 450 px

Common Aspect Ratios

Ratio Decimal Typical use
1:1 1.00 Instagram square, profile photos
4:3 1.33 Old TV, point-and-shoot cameras
3:2 1.50 DSLR sensors, 35 mm film
16:9 1.78 HD/4K video, YouTube, most monitors
21:9 2.33 Ultrawide monitors, cinema scope
9:16 0.56 Vertical video (Reels, TikTok, Stories)
4:5 0.80 Instagram portrait
2:3 0.67 Portrait photography

The decimal form (width ÷ height) is useful for quick comparisons and CSS. The integer ratio (e.g. 16:9) is easier to read and more widely recognised.


How GCD Works

Euclid's algorithm calculates the GCD in a handful of steps:

GCD(a, b):
  while b ≠ 0:
    temp = b
    b    = a mod b
    a    = temp
  return a

Trace for 1920 and 1080:

a b a mod b
1920 1080 840
1080 840 240
840 240 120
240 120 0

GCD = 120. Divide: 1920/120 = 16, 1080/120 = 9 → 16:9.


Code Examples

JavaScript

function gcd(a, b) {
  while (b) {
    [a, b] = [b, a % b];
  }
  return a;
}

function aspectRatio(width, height) {
  const divisor = gcd(width, height);
  return {
    ratio:   `${width / divisor}:${height / divisor}`,
    decimal: parseFloat((width / height).toFixed(4)),
  };
}

function missingDimension({ width, height, ratioW, ratioH }) {
  if (!width)  return { width:  Math.round(height * ratioW / ratioH), height };
  if (!height) return { width, height: Math.round(width  * ratioH / ratioW) };
  return aspectRatio(width, height);
}

console.log(aspectRatio(1920, 1080));          // { ratio: '16:9', decimal: 1.7778 }
console.log(aspectRatio(1080, 1080));          // { ratio: '1:1',  decimal: 1 }
console.log(missingDimension({ width: 800, ratioW: 16, ratioH: 9 }));
// { width: 800, height: 450 }

Python

import math

def aspect_ratio(width: int, height: int) -> dict:
    divisor = math.gcd(width, height)
    return {
        "ratio":   f"{width // divisor}:{height // divisor}",
        "decimal": round(width / height, 4),
    }

def missing_dimension(*, width=None, height=None, ratio_w=None, ratio_h=None):
    if width is None:
        return {"width": round(height * ratio_w / ratio_h), "height": height}
    if height is None:
        return {"width": width, "height": round(width * ratio_h / ratio_w)}
    return aspect_ratio(width, height)

print(aspect_ratio(1920, 1080))          # {'ratio': '16:9', 'decimal': 1.7778}
print(aspect_ratio(800, 600))            # {'ratio': '4:3',  'decimal': 1.3333}
print(missing_dimension(width=800, ratio_w=16, ratio_h=9))
# {'width': 800, 'height': 450}

math.gcd is available from Python 3.5 and handles the Euclidean algorithm for you.

Go

package main

import "fmt"

func gcd(a, b int) int {
    for b != 0 {
        a, b = b, a%b
    }
    return a
}

type Ratio struct {
    W, H    int
    Decimal float64
}

func aspectRatio(width, height int) Ratio {
    d := gcd(width, height)
    return Ratio{
        W:       width / d,
        H:       height / d,
        Decimal: float64(width) / float64(height),
    }
}

func missingHeight(width, ratioW, ratioH int) int {
    return (width * ratioH) / ratioW
}

func missingWidth(height, ratioW, ratioH int) int {
    return (height * ratioW) / ratioH
}

func main() {
    r := aspectRatio(1920, 1080)
    fmt.Printf("Ratio: %d:%d, Decimal: %.4f\n", r.W, r.H, r.Decimal)
    // Ratio: 16:9, Decimal: 1.7778

    fmt.Println("Height for 800px wide at 16:9:", missingHeight(800, 16, 9))
    // Height for 800px wide at 16:9: 450
}

PHP

function gcd(int $a, int $b): int {
    while ($b !== 0) {
        [$a, $b] = [$b, $a % $b];
    }
    return $a;
}

function aspectRatio(int $width, int $height): array {
    $d = gcd($width, $height);
    return [
        'ratio'   => ($width / $d) . ':' . ($height / $d),
        'decimal' => round($width / $height, 4),
    ];
}

function missingHeight(int $width, int $ratioW, int $ratioH): int {
    return (int) round($width * $ratioH / $ratioW);
}

function missingWidth(int $height, int $ratioW, int $ratioH): int {
    return (int) round($height * $ratioW / $ratioH);
}

print_r(aspectRatio(1920, 1080));   // ratio: 16:9, decimal: 1.7778
echo missingHeight(800, 16, 9);     // 450

Aspect Ratio in CSS

CSS has a native aspect-ratio property that eliminates the old padding-top hack:

/* Modern — all major browsers since 2021 */
.video-wrapper {
  width: 100%;
  aspect-ratio: 16 / 9;
}

/* Old padding-top hack (still works everywhere) */
.video-wrapper-legacy {
  position: relative;
  padding-top: 56.25%;   /* 9 / 16 × 100 */
  height: 0;
  overflow: hidden;
}

For dynamic sizing in JavaScript, multiply the container's current width by H / W:

const container = document.querySelector('.video-wrapper');
container.style.height = `${container.clientWidth * 9 / 16}px`;

FAQ

What is the most common aspect ratio for video?
16:9 is the standard for YouTube, Netflix, Vimeo, and most broadcast video. It is also the native ratio for 1080p and 4K monitors. Use 9:16 (the vertical flip) for Reels, TikTok, and Stories.

What aspect ratio should I use for social media images?
It depends on the platform: 1:1 for Instagram square posts, 4:5 for Instagram portrait, 1.91:1 for Facebook/Twitter link previews, 16:9 for LinkedIn shared images, and 9:16 for any short-form vertical video.

Is 16:9 the same as 1920×1080?
No — 16:9 is a ratio describing shape. 1920×1080, 1280×720, 3840×2160 (4K), and 854×480 are all 16:9 because they share the same proportional relationship between width and height.

Why is my calculated ratio not a clean integer (e.g. 1.78:1)?
When dimensions don't share a convenient GCD, the simplified ratio has large numbers (e.g. 960:541). In that case, the decimal form (1.78:1) or the nearest standard ratio (16:9) is more practical to communicate.

How do I maintain aspect ratio when resizing an image?
Calculate the scale factor from the dimension you are changing: scale = new_width / original_width. Then new_height = original_height × scale. Most image-processing libraries accept width: X, height: auto (or equivalent) to do this automatically.

What is a 2.39:1 ratio?
This is the standard anamorphic scope ratio used in cinema (sometimes written as 2.40:1 or called "Scope"). Films like Dune and most Hollywood blockbusters use it. On a 16:9 screen you see black bars at the top and bottom; in a dedicated cinema the screen is wider.


Calculate Your Aspect Ratio Now

Use the Aspect Ratio Calculator to instantly find the simplified ratio from any width and height, calculate a missing dimension, or convert between common ratios — no mental maths required.

Related tools

Keep reading

All Toolmingotools are free & run in your browser

No sign-up, no upload, no watermark. Your files never leave your device.

Browse all tools