Convert px to percentage of a container width, or a percentage back to pixels, plus px to vw/vh and font sizes in em, rem and pt for your CSS.

Choose the reference required by your CSS property.

Optional settings


Related Calculators

Calculator Functions

FunctionWhat it doesWhat it requires
Pixels to container percentageConverts a pixel size to a percentage of a reference sizeRequires the pixel value and the reference size, both in CSS px
Container percentage to pixelsConverts a percentage of a reference size to pixelsRequires the percentage and the reference size in CSS px
Container size from pixels and %Finds the reference size at which a pixel value equals a given percentageRequires the pixel value and the percentage
Pixels to vw / vhConverts a pixel size to vw or vh unitsRequires the pixel value, the viewport width or height in CSS px and the unit, vw or vh
vw / vh to pixelsConverts a vw or vh value to pixelsRequires the vw or vh value, the viewport width or height in CSS px and the unit
Viewport size from pixels and vw / vhFinds the viewport width or height at which a pixel value equals a given vw or vh valueRequires the pixel value, the vw or vh value and the unit
Font-size: px / % / em / rem / ptConverts a font size between px, %, em, rem and ptRequires the font-size value and its unit. The parent and root font sizes are optional and count as 16 px when blank

The reference size is whatever the CSS property measures against: the width of the containing block for width and padding percentages [3][4], and the parent's font size for font-size percentages [2]. The two viewport conversions can fill the reference from the current browser window. Each result comes with a CSS declaration ready to copy.

Pixel to Percentage Formulas

% = px รท ref ร— 100
%Percentage of the reference size
pxSize being converted, in CSS pixels
refReference size in CSS pixels, such as the width of the containing block [3]

A 300 px column inside a 1,200 px container is 300 รท 1,200 ร— 100 = 25% wide. Percentage padding is also measured against the containing block's width, even top and bottom padding [4].

Pixels to container percentage

The formula above applies directly. A 12 px size against a 15 px reference is 12 รท 15 ร— 100 = 80%.

Container percentage to pixels

px = % รท 100 ร— ref

25% of a 1,200 px container is 25 รท 100 ร— 1,200 = 300 px.

Container size from pixels and %

ref = px รท % ร— 100

An element that must be 300 px wide at 25% needs a 300 รท 25 ร— 100 = 1,200 px container. The percentage cannot be 0, and the pixel value and percentage need the same sign.

Pixels to vw / vh

vw = px รท W ร— 100 [1]

W is the viewport width in CSS px. One vw is 1% of the viewport's width and one vh is 1% of its height [1], so for vh the viewport height takes the place of W. On a 1,920 px wide viewport, 96 px is 96 รท 1,920 ร— 100 = 5 vw.

vw / vh to pixels

px = vw รท 100 ร— W

On a viewport 800 px tall, 50 vh is 50 รท 100 ร— 800 = 400 px.

Viewport size from pixels and vw / vh

W = px รท vw ร— 100

A 400 px element equals 50 vh when the viewport is 400 รท 50 ร— 100 = 800 px tall.

Font-size: px / % / em / rem / pt

px = pt ร— 4 รท 3 [1]

CSS fixes 1 in at 96 px and 1 pt at 1/72 in, so 1 pt is 4/3 px and 12 pt is 16 px [1]. In a font-size declaration an em value is multiplied by the parent's font size and a rem value by the root element's font size [1], and a percentage is taken of the parent's font size [2].

The calculator first turns the entered value into px, then gives % and em against the parent, rem against the root and pt as px ร— 0.75. With a 20 px parent and a 16 px root, 1.5 em is 30 px, which is also 150%, 1.875 rem and 22.5 pt.

Limits of This Estimate

  • A percentage only means something against the right reference. Width and padding use the containing block's width and font-size uses the parent's font size [2][3][4]. Other properties can use other references.
  • vw and vh are defined against the large viewport, the size with the browser's retractable toolbars hidden, so on a phone the visible area can be smaller [1]. The current-viewport shortcut reads the browser window's inner size, which may not be the same.
  • Blank parent and root font sizes are taken as 16 px. A page that sets different sizes needs them entered.

Sources

  1. W3C, CSS Values and Units Module Level 4: vw and vh, the px, pt and in ratios, em and rem
  2. W3C, CSS Fonts Module Level 4, font-size property: font-size percentages refer to the parent's font size
  3. W3C, CSS 2 Specification, Visual formatting model details, width property: width percentages refer to the containing block's width
  4. W3C, CSS 2 Specification, Box model, padding properties: padding percentages refer to the containing block's width