CSS Units
In CSS, units are used to specify values for various properties, such as width, height, margin, padding, font-size, etc. Correctly choosing and using units is crucial for creating scalable, maintainable, and responsive layouts.
CSS units can be divided into two main categories: Absolute Units and Relative Units.
Absolute Units
Absolute units are fixed; they do not depend on the size of any other element. They represent a physical length on any device.
px(Pixels): This is the most common and basic unit. 1 pixel corresponds to one physical dot on the display device. For screen displays,pxis a very precise unit.cm,mm,in: Centimeters, millimeters, inches. These units are more commonly used for print style sheets (@media print).pt(Points): 1/72 of an inch.pc(Picas): 12 points.
div {
width: 200px;
border: 1px solid black;
}Relative Units
The values of relative units are calculated relative to other length values. Using relative units makes your design more flexible and scalable.
Font-Relative Units
em: Relative to the current element'sfont-size. If used on thefont-sizeproperty itself, it is relative to the parent element'sfont-size. For example, if an element'sfont-sizeis16px, then2emequals32px. Theemunit can compound, making it difficult to manage in deep nesting.rem(root em): Relative to the root element's (<html>)font-size. This is a very powerful unit because it is not affected by the parent element's font size, avoiding the compounding issue ofem. In modern Web design,remis the preferred unit for setting font sizes, spacing, and component dimensions.
html {
font-size: 16px;
}
.parent {
font-size: 1.25em; /* 16px * 1.25 = 20px */
}
.child {
font-size: 1.5rem; /* 16px * 1.5 = 24px, not affected by parent */
padding: 1rem; /* 16px */
}Viewport-Relative Units
The Viewport is the visible area in the browser window.
vw(viewport width): 1% relative to the viewport width.10vwequals 10% of the viewport width.vh(viewport height): 1% relative to the viewport height.100vhequals the height of the entire viewport.vmin: The smaller value betweenvwandvh.vmax: The larger value betweenvwandvh.
Viewport units are very useful when creating full-screen hero sections or fonts and layouts that need to scale with screen size.
.fullscreen-section {
height: 100vh;
}
h1 {
font-size: 5vw;
}Percentages (%)
The % unit is always relative to a property value of the parent element. For example, width: 50% means the element's width is 50% of its parent element's width.
.parent {
width: 400px;
}
.child {
width: 50%; /* 400px * 0.5 = 200px */
}Unit Selection Suggestions
- Font Size: Prefer
remfor accessibility and scalability. - Margins and Padding: Use
remto keep spacing in harmonious proportion with font size. In some cases,pxis used for fine-tuning, and%for fluid layouts. - Borders: Usually use
px, as borders often need to be precise thin lines. - Layout Width/Height: Use
%to create fluid layouts, andvw/vhfor viewport-related layouts. - Media Queries: Use
emorremto set breakpoints, which allows your responsive design to better adapt to user-adjusted font sizes.