Why em instead of px?

后端 未结 15 1042
闹比i
闹比i 2020-11-22 06:38

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 c

相关标签:
15条回答
  • 2020-11-22 07:24

    The general consensus is to use percentages for font sizing, because it's more consistent across browsers/platforms.

    It's funny though, I always used to use pt for font sizing and I assumed all sites used that. You don't normally use px sizes in other apps (eg Word). I guess it's because they're for printing - but the size is the same in a web browser as in Word...

    0 讨论(0)
  • 2020-11-22 07:25

    I have a small laptop with a high resolution and have to run Firefox in 120% text zoom to be able to read without squinting.

    Many sites have problems with this. The layout becomes all garbled, text in buttons is cut in half or disappears entirely. Even stackoverflow.com suffers from it:

    Screenshot of Firefox at 120% text zoom

    Note how the top buttons and the page tabs overlap. If they would have used em units instead of px, there would not have been a problem.

    0 讨论(0)
  • 2020-11-22 07:26

    Pixel is an absolute unit whereas rem/em are relative units. For more: https://drafts.csswg.org/css-values-3/

    You should use the relative unit when you want the font-size to be adaptive according to the system's font size because the system provides the font-size value to the root element which is the HTML element.

    In this case, where the webpage is open in google chrome, the font-size to the HTML element is set by chrome, try changing it to see the effect on webpages with fonts of rem/ em units.

    If you use px as the unit for fonts, the fonts will not resize whereas the fonts with rem/ em unit will resize when you change the system's font size.

    So use px when you want the size to be fixed and use rem/ em when you want the size to be adaptive/ dynamic to the size of the system.

    0 讨论(0)
  • 2020-11-22 07:27

    use px for precise placement of graphical elements. use em for measurements having to do positioning and spacing around text elements like line-height etc. px is pixel accurate, em can change dynamically with the font in use

    0 讨论(0)
  • 2020-11-22 07:30

    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

    0 讨论(0)
  • 2020-11-22 07:31

    The reason I asked this question was that I forgot how to use em's as it was a while I was hacking happily in CSS. People didn't notice that I kept the question general as I wasn't talking about sizing fonts per se. I was more interested in how to define styles on any given block element on the page.

    As Henrik Paul and others pointed out em is proportional to the font-size used in the element. It's a common practice to define sizes on block elements in px, however, sizing up fonts in browsers usually breaks this design. Resizing fonts is commonly done with the shortcut keys Ctrl++ or Ctrl+-. So a good practice is to use em's instead.

    Using px to define the width

    Here is an illustrating example. Say we have a div-tag that we want to turn into a stylish date box, we may have HTML-code that looks like this:

    <div class="date-box">
        <p class="month">July</p>
        <p class="day">4</p>
    </div>
    

    A simple implementation would defining the width of the date-box class in px:

    * { margin: 0; padding: 0; }
    
    p.month { font-size: 10pt; }
    
    p.day { font-size: 24pt; font-weight: bold; }
    
    div.date-box {
        background-color: #DD2222;
        font-family: Arial, sans-serif;
        color: white;
        width: 50px;
    }
    

    The problem

    However, if we want to size the text up in our browser the design will break. The text will also bleed outside the box which is almost the same what happens with SO's design as flodin points out. This is because the box will remain the same size in width as it is locked to 50px.

    Using em instead

    A smarter way is to define the width in ems instead:

    div.date-box {
        background-color: #DD2222;
        font-family: Arial, sans-serif;
        color: white;
        width: 2.5em;
    }
    
    * { margin: 0; padding: 0; font-size: 10pt; }
    
    // Initial width of date-box = 10 pt x 2.5 em = 25 pt
    // Will also work if you used px instead of pt
    

    That way you have a fluid design on the date-box, i.e. the box will size up together with the text in proportion to the font-size defined for the date-box. In this example, the font-size is defined in * as 10pt and will size up 2.5 times to that font size. So when you're sizing the fonts in the browser, the box will have 2.5 times the size of that font-size.

    0 讨论(0)
提交回复
热议问题