Advertisement
Google Ad Slot: content-top
CSS Units
In CSS, units are used to define the size of elements such as fonts, margins, padding, borders, and other properties. There are two types of units: relative units and absolute units.
Units categories:
- Absolute Units
- Relative Units
Absolute Units:
Absolute units are fixed and don’t change based on other elements. These units are typically used when you need precise control over sizing.
- px (Pixels)
- cm (Centimeters), mm (Millimeters), in (Inches)
- pt (Points)
- pc (Picas)
px (Pixels):
The most commonly used unit. 1px equals one dot on the screen.
cm (Centimeters), mm (Millimeters), in (Inches):
These are physical measurements, mostly used for print styles, not for screens.
pt (Points):
Used in typography, where 1pt = 1/72 of an inch.
pc (Picas):
1 pica equals 12 points.
Relative Units:
Relative units are more flexible and scalable. They change in relation to other measurements (such as the viewport size or a parent element’s size).
- % (Percentage)
- em
- rem (Root em)
- vw (Viewport Width)
- vh (Viewport Height)
- vmin
- vmax
- ch
% (Percentage):
Defines size as a percentage of the parent element’s size.
em:
Relative to the font-size of the element itself or its parent. For example, if the parent element’s font-size is 16px, 2em would be 32px.
rem (Root em):
Similar to em, but relative to the font-size of the root element (usually the <html> tag).
vw (Viewport Width) and vh (Viewport Height):
- vw (Viewport Width): 1vw equals 1% of the viewport’s width (browser window width).
- vh (Viewport Height): 1vh equals 1% of the viewport’s height.
vmin and vmax:
- vmin: The smaller value between
vwandvh. - vmax: The larger value between
vwandvh.
ch:
Relative to the width of the 0 character of the element’s font.