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?
Use em only for sizing that needs to scale based on the font size of an element other than the html (root) element. Use rem unit for elements that scale depending on a user's browser font size settings. Use rem as the unit for most of your property value.
PX units ensure consistent results even if that's not ideal. EM is relative to the parent element's font size, so if you wish to scale the element's size based on its parent's size, use EM.
The Pros and Cons of using em This is because this unit is relative to the font size of its parent element, allowing an element's size, spacing and text content to grow proportionally as the font-size of parent elements change.
It is wrong to say that one is a better choice than the other (or both wouldn't have been given their own purpose in the spec). It may even be worth noting that StackOverflow makes extensive use of px units. It is not the poor choice Spoike was told it was.
Definition of units
px is an absolute unit of measurement (like in, pt, or cm) that also happens to be 1/96 of an in unit (more on why later). Because it is an absolute measurement, it may be used any time you want to define something to be a particular size, rather than being proportional to something else like the size of the browser window or the font size.
Like all the other absolute units, px units don't scale according to the width of the browser window. Thus, if your entire page design uses absolute units such as px rather than %, it won't adapt to the width of the browser. This is not inherently good or bad, just a choice that the designer needs to make between adhering to an exact size and being inflexible versus stretching but in the process not adhering to an exact size. It would be typical for a site to have a mix of fixed-size and flexible-sized objects.
Fixed size elements often need to be incorporated into the page - such as advertising banners, logos or icons. This ensures you almost always need at least some px-based measurements in a design. Images, for example, will (by default) be scaled such that each pixel is 1px in size, so if you are designing around an image you'll need px units. It is also very useful for precise font sizing, and for border widths, where due to rounding it makes the most sense to use px units for the majority of screens.
All absolute measurements are rigidly related to each other; that is, 1in is always 96px, just as 1in is always 72pt. (Note that 1in is almost never actually a physical inch when talking about screen-based media). All absolute measurements assume a nominal screen resolution of 96ppi and a nominal viewing distance of a desktop monitor, and on such a screen one px will be equal to one physical pixel on the screen and one in will be equal to 96 physical pixels. On screens that differ significantly in either pixel density or viewing distance, or if the user has zoomed the page using the browser's zoom function, px will no longer necessarily relate to physical pixels.
em is not an absolute unit - it is a unit that is relative to the currently chosen font size. Unless you have overridden font style by setting your font size with an absolute unit (such as px or pt), this will be affected by the choice of fonts in the user's browser or OS if they have made one, so it does not make sense to use em as a general unit of length except where you specifically want it to scale as the font size scales.
Use em when you specifically want the size of something to depend on the current font size.
rem is like em, but it's relative to the base font size of the document (specifically, of the root element), rather than the font size given to the current element.
% is also a relative unit, in this case, relative to either the height or width of a parent element. They are a good alternative to px units for things like the total width of a design if your design does not rely on specific pixel sizes to set its size.
Using % units in your design allows your design to adapt to the width of the screen/device, whereas using an absolute unit such as px does not.
vh, vw, vmin, and vmax are relative units like %, but they are relative to the size of the viewport (size of the viewable area of the window) rather than of the parent element. Respectively, they are relative to the viewport's height, width, smaller of the two, and greater of the two.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With