While defining the width or height of any element in CSS, you might have already used the CSS Units unknowingly.
Say for example:
img { width:150px; height:190px; }
In the above CSS properties we have specified the width and height for the img element. And the height and width is specified in px or pixels.
This px is a Unit in CSS.
Units in CSS define the length in CSS. Say for example, the width, height expresses the length in units.
There are two types of Units in CSS :
Absolute Units in CSS are fixed measurements that remains fixed even if we change the screen size.
Below are the list of Absolute Units in CSS :
height:12px;
width:12cm;
height:12mm;
width:2in;
height:12pt;
Relative Units in CSS are fixed measurements that changes when we change the screen size.
Below are the list of Relative Units in CSS :
The em unit is relative to the parent elements font size.
<html> <head> <style> p { font-size: 20px; } span { font-size: 2em; } </style> </head> <body> <p> This is the parent. <span> This is the child </span> </p> </body> </html>
So, if you look at the above example, the <span> element is the child of the parent <p> element.
<p> This is the parent. <span> This is the child </span> </p>
Now, if you look at the font-size of the parent element, it is 20px.
p { font-size: 20px; }
And the font-size of the child element is 2em. Which is 2 times the font-size of parent element i.e. 2x20=40px.
The rem unit is relative to the font size of the root elements.
<html> <head> <style> body { font-size: 10px; } p { font-size: 20px; } span { font-size: 2rem; } </style> </head> <body> <p> This is the parent. <span> This is the child </span> </p> </body> </html>
So, if you look at the above example, the <span> element is the child of the parent <p> element.
<p> This is the parent. <span> This is the child </span> </p>
However, this time the <span> element will not take the font-size from it parent. Rather it would take the font-size of the root element(i.e. <body>).
And the font-size of <body> element is 10px.
So the font-size of the <span> element becomes 2x10=20px.
Viewport widths or vw is relative to the viewport width.
<html> <head> <style> p { font-size: 20vw; } </style> </head> <body> <p> This is the paragraph </p> </body> </html>
So, if you increase or decrease the width of your browser the font-size of the <p> element changes. That is because Viewport widths or vw is relative to the viewport width.
Viewport heights or vh is relative to the viewport height.
<html> <head> <style> p { font-size: 20vh; } </style> </head> <body> <p> This is the paragraph </p> </body> </html>
So, if you increase or decrease the height of your browser the font-size of the <p> element changes. That is because Viewport height or vh is relative to the viewport height.
Percentage or % is relative to the font-size of the parent element.
<html> <head> <style> p { font-size: 40px; } span { font-size: 50%; } </style> </head> <body> <p> This is the parent. <span> This is the child </span> </p> </body> </html>
So, in the above example, <span> element is the child of the <p> element.
<p> This is the parent. <span> This is the child </span> </p>
The font-size of <p> element is 40px.
p { font-size: 40px; }
And we have made the font-size of the <span> element as 50%. Which is half the size of <p> element.
And if you see the font-size of the <span> element, it is half the font-size of <p> element.