Mobile app version of vmapp.org
Login or Join
Connie744

: Difference between px and em in html5 Check this sample code <p style="font-size:30px">HTML</p> <br> <p style="font-size:30em">HTML</p> What is the difference between em and

@Connie744

Posted in: #Html #Html5

Check this sample code

<p style="font-size:30px">HTML</p>
<br>
<p style="font-size:30em">HTML</p>


What is the difference between em and px ?
In which place should I use em and in which place should I use px?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Connie744

2 Comments

Sorted by latest first Latest Oldest Best

 

@Fox8124981

From Kyle Schaeffer's blog article: CSS Font-Size: em vs. px vs. pt vs. percent


Generally, 1em = 12pt = 16px = 100%.

There are four different units by which you can measure the size of text

“Ems” (em): The “em” is a scalable unit that is used in web document media. An em is equal to the current font-size, for instance, if the font-size of the document is 12pt, 1em is equal to 12pt. Ems are scalable in nature, so 2em would equal 24pt, .5em would equal 6pt, etc. Ems are becoming increasingly popular in web documents due to scalability and their mobile-device-friendly nature.

Pixels (px): Pixels are fixed-size units that are used in screen media (i.e. to be read on the computer screen). One pixel is equal to one dot on the computer screen (the smallest division of your screen’s resolution). Many web designers use pixel units in web documents in order to produce a pixel-perfect representation of their site as it is rendered in the browser. One problem with the pixel unit is that it does not scale upward for visually-impaired readers or downward to fit mobile devices.

Points (pt): Points are traditionally used in print media (anything that is to be printed on paper, etc.). One point is equal to 1/72 of an inch. Points are much like pixels, in that they are fixed-size units and cannot scale in size.

Percent (%): The percent unit is much like the “em” unit, save for a few fundamental differences. First and foremost, the current font-size is equal to 100% (i.e. 12pt = 100%). While using the percent unit, your text remains fully scalable for mobile devices and for accessibility.

10% popularity Vote Up Vote Down


 

@Chiappetta492

Pixels is absolute. If you tell it to be 30px, it's gonna be 30px, no matter how you use it.

EM is relative. It multiplies with the inherited definition it has. 30em makes it 30 times the inherited definition.

Some examples assuming

body{ font-size: 10px;}
px{ font-size: 15px; }
em{ font-size: 2.5em; }


In pixels things are absolute (no matter where you place it, always same result):

<div class="px">
This text is exactly 15 pixels
<div class="px">
This text is exactly 15 pixels
<div class="px">
This text is exactly 15 pixels
</div>
</div>
</div>


Em is relative, it depends on other elements' values:

<div class="em">
This text is 2.5 times larger than it's parent -> 25px
<div class="em">
This text is 2.5 times larger than it's parent -> 62.5px
<div class="em">
This text is 2.5 times larger than it's parent -> 156.25px
</div>
</div>
</div>




Other examples:

<div class="em">
This text is 2.5 times larger than it's parent -> 25px
<div class="px">
This text is exactly 15 pixels
</div>
</div>

<div class="em">
This text is 2.5 times larger than it's parent -> 25px
<div class="px">
This text is exactly 15 pixels
<div class="em">
This text is 2.5 times larger than it's parent -> 37.5px
</div>
</div>
</div>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme