Absolute and font-relative lengths
Definition and purpose
A CSS pixel is a reference unit, not necessarily one physical device pixel. Absolute lengths have fixed CSS conversion ratios. Relative font units depend on font sizes or metrics: rem refers to the root font size and em generally to the element font size.
Absolute conversion and rem
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> html { font-size: 16px; } #a { width: 1in; } #b { width: 2rem; } </style> </head> <body> <div id="a">A</div> <div id="b">B</div> </body></html>The inch is 96 CSS pixels; rem uses the root 16px.
Syntax and mental model
For font-size itself, em uses the inherited font size; for other properties such as padding it uses the element computed font size. With root 16px, 2rem is 32px. With an element at 20px, padding: .5em gives 10px. CSS fixes 1in = 96px; physical measurements on screens need not match a ruler.
Em depends on the property
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { font-size: 20px; } #b { font-size: 1.5em; padding: .5em; } </style> </head> <body> <div id="a"> <span id="b">Text</span> </div> </body></html>Font size uses the parent 20px; padding uses the resulting 30px.
Common mistakes
Nested em font sizes compound. Relative units can help text scale, but do not disable zoom or assume any unit alone ensures accessibility. The examples fix root size for predictable arithmetic; real sites should respect user preferences.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Value types and grammar basics. Next lesson: Percentages and reference sizes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Absolute and font-relative lengths — knowledge check
Five questions on absolute and font-relative lengths.
What is 1in in CSS pixels?
Root font size 16px: what is 1.5rem?
Parent 20px, child font-size: 2em. Child size?
Child font-size 30px, padding .5em. Padding?
Why can nested em font sizes grow unexpectedly?