Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the same font size gives different results?

Why font size is different in divs?

<div style="font-size: 30px;">
    <h1>Sample text</h1>
</div>

<div>
    <h1 style="font-size: 30px;">Sample text</h1>
</div>

Why is this happening?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The default value for h1 tag in Chrome is 2em.

Ems are relative to the font size set in the CSS (parent element).

So in your second div you are overwriting the 2em value with 30px for the h1 element – font size is now equal to 30px;

In your first div you set the font-size of the (container) div to 30px, so now 2em is using this as the base font size – computed font size is now equal to 60px.

<section style="font-size: 15px">
  <div>base font-size: 15px</div>
  <div style="font-size: 3em">this should be 45px (15px x 3em)</div>
</section>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading