Here is an example of HTML code featuring nested div
tags and a p
tag as the innermost element:
<!DOCTYPE html>
<html>
<head>
<title>How em unit works?</title>
<link href="style.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div>
<div>
<p> Some text</p>
</div>
</div>
</body>
</html>
If we apply the following styles to the elements:
body{
font-size: 10px;
}
body > div{
font-size: 20px;
}
body > div > div{
font-size: 50px;
}
p{
font-size: 0.5em;
width: 6em;
height: 6em;
padding-right: 3em;
margin-left: 3em;
background-color: red;
}
The paragraph's text will have a font-size
of 0.5 * 50px
, which is calculated based on the parent's font size.
Now, imagine if we add position: absolute
to the p
element and change the outermost div
to position: relative
. How would this affect the behavior of the em
unit for the p
element?