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.
Related Calculators
- Pixel Resize Calculator
- Pixels to Inches Calculator
- DPI Calculator (PPI)
- Resolution Calculator
- cm to Pixel Calculator
- Pixels Per Degree to Resolution Calculator
- All Unit Converters
Calculator Functions
| Function | What it does | What it requires |
|---|---|---|
| Pixels to container percentage | Converts a pixel size to a percentage of a reference size | Requires the pixel value and the reference size, both in CSS px |
| Container percentage to pixels | Converts a percentage of a reference size to pixels | Requires 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 percentage | Requires the pixel value and the percentage |
| Pixels to vw / vh | Converts a pixel size to vw or vh units | Requires the pixel value, the viewport width or height in CSS px and the unit, vw or vh |
| vw / vh to pixels | Converts a vw or vh value to pixels | Requires the vw or vh value, the viewport width or height in CSS px and the unit |
| Viewport size from pixels and vw / vh | Finds the viewport width or height at which a pixel value equals a given vw or vh value | Requires the pixel value, the vw or vh value and the unit |
| Font-size: px / % / em / rem / pt | Converts a font size between px, %, em, rem and pt | Requires 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
| % | Percentage of the reference size |
| px | Size being converted, in CSS pixels |
| ref | Reference 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
25% of a 1,200 px container is 25 รท 100 ร 1,200 = 300 px.
Container size from pixels and %
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
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
On a viewport 800 px tall, 50 vh is 50 รท 100 ร 800 = 400 px.
Viewport size from pixels and vw / vh
A 400 px element equals 50 vh when the viewport is 400 รท 50 ร 100 = 800 px tall.
Font-size: px / % / em / rem / pt
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
- W3C, CSS Values and Units Module Level 4: vw and vh, the px, pt and in ratios, em and rem
- W3C, CSS Fonts Module Level 4, font-size property: font-size percentages refer to the parent's font size
- W3C, CSS 2 Specification, Visual formatting model details, width property: width percentages refer to the containing block's width
- W3C, CSS 2 Specification, Box model, padding properties: padding percentages refer to the containing block's width
