CSS Units
CSS units define measurements and are used for various properties like width, height, margin, padding, font-size, and more. CSS supports both absolute and relative units.
Absolute Units
Absolute units are fixed and don’t change based on other elements.
- px (pixels): A single dot on the screen, relative to the resolution.
width: 200px;- pt (points): 1 point = 1/72 of an inch.
font-size: 12pt;- cm (centimeters): For specifying measurements in centimeters.
margin: 2cm;css- mm (millimeters): For measurements in millimeters.
padding: 10mm;- n (inches): 1 inch = 96 pixels.
width: 3in;- pc (picas): 1 pica = 12 points.
font-size: 1pc;Relative Units
Relative units are flexible and relative to other measurements.
- % (percentage): Relative to the parent element.
width: 50%;- em: Relative to the font-size of the parent element.
padding: 2em; /* Twice the size of the current font */- rem (root em): Relative to the font-size of the root element (html).
font-size: 1.5rem; /* 1.5 times the root font-size */- vw (viewport width): 1% of the width of the viewport.
width: 50vw; /* 50% of the viewport width */- vh (viewport height): 1% of the height of the viewport.
height: 100vh; /* 100% of the viewport height */- vmin: 1% of the viewport’s smaller dimension (width or height).
font-size: 5vmin;- vmax: 1% of the viewport’s larger dimension.
width: 10vmax;- ex: Relative to the x-height of the current font (height of lowercase ‘x’).
font-size: 2ex;- ch: Relative to the width of the “0” (zero) character in the current font.
width: 20ch;