Each unit is relative to something different:
pxis a fixed CSS pixel. It is fine for borders and small details.remis relative to the root (html) font size. So it respects the user's browser font setting. It is the usual choice for font sizes and spacing.emis relative to the element's own font size. When used infont-size, it uses the parent's font size. It suits things that should scale with the nearby text, like button padding or icon size. But nestedemfont sizes compound: 1.2em inside 1.2em gives 1.44 times the size.%is relative to the containing block, which is usually the parent.width: 50%uses the containing block's width, andheight: 50%uses its height. Padding and margin percentages always use the width, even for top and bottom.vwandvhare 1% of the viewport's width and height. The viewport is the visible browser window. They are useful for full-screen sections and fluid text withclamp(). Butvwalone ignores the user's font settings.
On mobile, 100vh can be taller than the visible area, because browser toolbars cover part of the screen. The newer units dvh (dynamic), svh (small) and lvh (large) solve this.
remrespects the user's font size, so it is a good default for text and spacing.emscales with the local font size, and nestedemfont sizes compound.%uses the containing block: its width forwidthand its height forheight. Padding and margin percentages always use the width.vwandvhare viewport units.dvhandsvhfix the mobile toolbar problem.
Read more
- CSS values and units (MDN) (https://developer.mozilla.org/en-US/docs/Web/CSS/length)developer.mozilla.org