CSS Reference ยท Units and Values
Percentage values
Reference for CSS percentages: what each property's % is relative to, including width, height, padding, margin, font-size, line-height and transforms.
A percentage is a value written as a number followed by %. It is not a fixed size: it is a fraction of some reference value, and which value that is depends on the property. width: 50% is half of the containing block's width, while font-size: 50% is half of the parent's font size.
Most layout confusion with percentages comes from this: the same % sign means different things in different properties. This page lists the references you will meet most often.
.column { width: 50%; padding: 5%; font-size: 90%; }
Try it
The blue box is half as wide as its parent's content area (about 198px of 396px) and half as tall (78px of 156px); the 2px border is not part of the content area. Its padding is 5% of the parent's width, about 20px on every side, even at the top and bottom.
Syntax
<percentage> = <number>% <length-percentage> = <length> | <percentage>
- Reference value: defined per property.
- Computed value: as specified, or resolved to an absolute length when layout is known (for properties where the computed value is the percentage, the used value is a length).
- Animatable: yes, and percentages can be mixed with lengths through
calc().
Values
| Property | Percentage refers to |
|---|---|
width, min-width, max-width |
Width of the containing block |
height, min-height, max-height |
Height of the containing block, if it is definite; otherwise auto |
margin, padding (all sides) |
Width of the containing block |
top, bottom |
Height of the containing block |
left, right |
Width of the containing block |
font-size |
Parent's font size |
line-height |
The element's own font size |
vertical-align |
The element's own line height |
translate() in transform |
The element's own box size |
border-radius |
The element's own border box (width for horizontal radii, height for vertical) |
background-position, background-size |
The background positioning area |
flex-basis |
Size of the flex container's main axis |
grid-template-* track sizes |
Size of the grid container in that axis |
Usage notes
- Percent height needs a definite parent. If the parent's height is
auto, a child withheight: 50%computes toauto. Give the parent an explicit height, or use flex or grid. - Padding and margin always use the width. This is why
padding-top: 56.25%makes a 16:9 box. Todayaspect-ratiois clearer. - Percentages do not include margins, padding or borders by default. With
box-sizing: content-box,width: 100%plus padding overflows the parent. Usebox-sizing: border-box. - Mixing units.
calc(100% - 2rem)combines a percentage with a fixed length at layout time. - Unitless numbers are different.
line-height: 1.5multiplies the element's own font size and is inherited as a number;line-height: 150%is computed once and its resulting length is inherited. Prefer the number. - Percentages in
font-sizecompound just likeem.
Browser support
Supported in every browser.
Related
calc()โ mix percentages with lengthswidthandheightโ the most common usesvw,vhand viewport units โ percentages of the viewport insteadline-heightโ number versus percentage- Lesson: CSS Units