Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - should em be used for margin and padding

Tags:

css

In my stylesheet I use em for font sizes.

Is it bad practice to use this for padding and margin as well so that when the font scales the margin and padding will scale with it?

like image 372
AJM Avatar asked Feb 27 '12 10:02

AJM


People also ask

Should you use em for padding and margin?

It totally depends on your need. Em should be avoided because it depends on the native browser settings, but if you use px then css of the page will be cross browser compatible.

Should I use em or REM for padding?

Use EM where you have to make more scaling than the root font size. And use REM where you need value according to the root there you can use REM units.

Can you use em for margin?

em units can be used for much more than just setting font-size however, and they can be used pretty much everywhere units are expected (padding, margin, width, height, max-width,…you get the picture!) When em units are used on other properties than font-size , the value is relative to the element's own font-size .

Can padding be in em?

The em is simply the font size. In an element with a 2in font, 1em thus means 2in. Expressing sizes, such as margins and paddings, in em means they are related to the font size, and if the user has a big font (e.g., on a big screen) or a small font (e.g., on a handheld device), the sizes will be in proportion.


2 Answers

I don't think that its a bad practice, but I don't think that most of developers will use it nowadays: some thought on it:

  • http://davidbcalhoun.com/2010/does-it-still-make-sense-to-use-em-rather-than-px
  • http://jontangerine.com/log/2007/09/the-incredible-em-and-elastic-layouts-with-css
like image 168
mas-designs Avatar answered Sep 28 '22 07:09

mas-designs


It totally depends on your need. Em should be avoided because it depends on the native browser settings, but if you use px then css of the page will be cross browser compatible.

Please refer to this post : Why em instead of px? This post will tell you when to use EM and when PX.

like image 44
waseem Avatar answered Sep 28 '22 07:09

waseem