I heard you should define sizes and distances in your stylesheet with em instead of in pixels. So the question is why should I use em instead of px when defining styles in css? Is there a good example that illustrates this?
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
Because the em (http://en.wikipedia.org/wiki/Em_(typography)) is directly proportional to the font size currently in use. If the font size is, say, 16 points, one em is 16 points. If your font size is 16 pixels (note: not the same as points), one em is 16 pixels.
This leads to two (related) things:
example:
Code: body{font-size:10px;} //keep at 10 all sizes below correct, change this value and the rest change to be e.g. 1.4 of this value
1{font-size:1.2em;} //12px
2{font-size:1.4em;} //14px
3{font-size:1.6em;} //16px
4{font-size:1.8em;} //18px
5{font-size:2em;} //20px
…
body
1
2
3
4
5
by changing the value in body the rest change automatically to be a kind of times the base value…
10×2=20 10×1.6=16 etc
you could have the base value as 8px… so 8×2=16 8×1.6=12.8 //may be rounded by browser