we use length in width, height, margin, padding, font-size. There are two type of units in css .
They are not relative to anything else, and are generally considered to always be the same size. The only value that you will commonly use is px (pixels).
px
(1inch = 96px)pt
(1inch = 72pt)pc
(1inch = 12pt)Relative length units are relative to something else, perhaps the size of the parent element’s font, or the size of the viewport.
em
- It is relatvie to the size of direct parent. It is multiplier of direct parent elementrem
- It is relative to the root of the HTML element. It is multiplier of root elementvh
- 1% of viewport height(Viewport = the browser window size)vw
- 1% of viewport width(Viewport = the browser window size)%
- relative to parentHTML
<div id="parent">
<p>This is parent element</p>
<div id="child">
<p>This is child element</p>
</div>
</div>
CSS
#parent {
width: 700px;
border: 1px solid black;
}
#child {
width: 900px;
border: 1px solid red;
}
This will produce the following result −
#parent {
width: 700px;
border: 1px solid black;
font-size: 20px;
}
#child {
width: 700px;
border: 1px solid black;
font-size: 2em;
}
This will produce the following result −
#parent {
width: 700px;
border: 1px solid black;
font-size: 1rem;
}
#child {
width: 700px;
border: 1px solid black;
font-size: 2rem;
}
This will produce the following result −
#parent {
width: 50%;
border: 1px solid black;
}
#child {
width: 90%;
border: 1px solid black;
}
This will produce the following result −