What is the difference between px, em, rem and % in CSS?
px is absolute. The others are relative.
| Unit | Relative to |
|---|---|
px | Nothing, a fixed pixel size |
em | The font size of the element (of the parent for font-size) |
rem | The font size of the root html element |
% | The same property of the parent (like width of the parent) |
html { font-size: 16px; }
h1 { font-size: 2rem; } /* 32px */
.box { width: 50%; } /* half of the parent */
Use rem for font sizes. See CSS Units.
Learn more about the similar topics:
| Tutorials |
|---|
| CSS Units |
| CSS Text |
| CSS Fonts |
| CSS Links |
| CSS Lists |
| CSS Tables |
| Exercises & Assignments |
|---|
| Write CSS to style a heading (centre, uppercase, letter spacing) |
| Write CSS to style a table with zebra stripes |
| Interview Questions & Answers |
|---|
| Why do we use shorthand properties in CSS? |
| What is the difference between px, em, rem and % in CSS? |