+ 2

What's the difference between 20px and 20% in css

1st Jul 2018, 8:52 PM
Amedjoe
Amedjoe - avatar
2 Answers
+ 2
px are absolute units (size hold same) while % its a relative unit (size depend on another size)... At example if you set an div to be 20px width it will be always 20px but if you set to 20%, widh of div will be 20% of parent div then if parent width is 100px, div width will be 20px, if parent width 400px, div width 80px
1st Jul 2018, 9:04 PM
KrOW
KrOW - avatar
- 1
‱ 20% is based on screen size (width/height). eg:- <hr width="20%/> will show a horizontal line from 0% to 20% of the screen. ‱ 20px is pixel based (pixels are squares that filled with colour). eg:- <hr width="20px"/> will show a horizontal line with 20 pixels widh.
3rd Jul 2018, 10:24 AM
Joshua Raison
Joshua Raison - avatar