Only px for margins and padding?

Do we only use “px” for margins and padding? Is “em” ever used?

You can generally use px and em interchangeably. 1em is equal to the font height of that element (usually 16px by default). Em will also scale a bit better on different screen resolutions, so it’s usually better than px.

In case you’re interested, here are some of the other handy units you can use in place of px:
% size relative to the size of the parent
vh (viewport height): size relative to browser window height
vw (viewport width): size relative to browser window width (eg 100vw would be the full width of the screen)
rem: this is almost identical to em, but some people find rem simpler. Em is relative to the font size of that element. Rem is relative to the font height of the html element (usually 16px by default). But the difference is mostly personal preference, use whatever you find easier.


Thank you, andreobriennz, for clearing this up. The exercises on Codecademy kept using px for margins and padding, so, I didn’t know if we could use em, as well.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.