About PX and EM in web phone applications

Source: Internet
Author: User

In fact, I just want to say that the difference between PX and EM, online information is actually a pile of heap, but it is not as clear as their own test summary. The front-end expert may think this thing is simple, but as a kitten, still good to learn a bit.

A little bit below said ~ ~

PX and EM are length units, the difference is that the PX value is fixed, specifying how much is how much, the calculation is relatively easy. The EM value is not fixed, and EM inherits the font size of the parent element.

The default font height for any browser is 16px. So the unmodified browser is compliant with: 1EM=16PX. So 12px=0.75em, 10px=0.625em.

To simplify the calculation, declare the font-size=62.5% in the body selector in the CSS, which makes the EM value 16px*62.5%=10px, so 12px=1.2em, 10px=1em, that is, simply dividing your original PX value by 10, And then put EM in the unit on the line.

Just like this:


<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<style>
body{font-size:62.5%}
. div_px{font-size:10px; border:1px red solid;width:120px; height:20px}
. div_em{font-size:1em; border:0.1em red solid;width:12em; Height:2em}

</style>


<body>
<div class= "div_px" >1, this is div_px</div><br/>

<div class= "Div_em" >2, this is div_em</div><br/>

<br/>
</body>

The effect is as follows:

The two Div is basically the same.

But if that's the case, it's good. A direct replacement would be fine.

' Em will inherit the parent element's font size ' This one, to the death ...

For example, a page like this:


<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<style>
body{font-size:62.5%}

. div_out{font-size:1em; border:1px red solid;width:12em; Height:4em }
. div_in{font-size:1em; border:1px blue solid;width:12em; Height:4em }
</style>


<body>

<div class= "Div_out" >
3, this is Div_out
<div class= "Div_in" >
4, this is div_in
</div>
</div>
<br/><br/><br/><br/><br/><br/><br/><br/>
<div class= "Div_in" >
5, this is outside the div_in
</div>

</body>

Here are 3 div,3 and 4 are set together, and 5 are for comparison.

Now the effect is this:

Well, it looks like these div effects are the same, div_out and div_in settings, font-size are 1em, so the inheritance doesn't show up.

Through detection, 3 and 4 of the fonts are 10px, the length is 122px, the width is 42px. (Set the border, so 2px more)

Now change it:

. div_out{font-size:1.5em; border:1px red solid;width:12em; Height:4em  }

The effect is like this

It's obvious that the top two div is getting bigger, stating ...
Red Box div_out, because Font-size set 1.5em, the real font size is 10*1.5=15px, width 12*10*1.5=180px; 4*10*1.5=60px (no calculation border)

Style div_in did not make changes, Font-size is still 1em, but nested in 3 inside the 4th div and 5 effect is not the same, 4 inherited div_out.

One more:

. div_out{font-size:1.5em; border:1px red solid;width:12em; Height:4em  }
. div_in{font-size:1.5em; border:1px blue solid; width:12em; Height:4em }

This time, the two-style font-size are set to 1.5em. Look at the effect.


What is this for?

3rd # div True font size is 10*1.5=15px, width 12*10*1.5=180px; height 4*10*1.5=60px (no border calculated)

5th # div True font size is 10*1.5=15px, width 12*10*1.5=180px; height 4*10*1.5=60px (no border calculated)

4th # div True font size is 10*1.5*1.5=22.5px, width 12*10*1.5*1.5=270px; height 4*10*1.5*1.5=90px (no border calculated)

................................................................................................................................................................................................................................................

If all with PX, then completely no problem, you set how much is how much, will not let people calculate the dizziness. I usually use PX as the size, width, height and other units, the only use of EM is the font indentation.

About PX and EM in web phone applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.