CSS
π¨ CSS Sizing Units
CSS sizing units are used to define the size of elements on a webpage.
They control properties such as:
- Width
- Height
- Font size
- Margin and padding
- Borders
- Positioning
CSS units are divided into two main categories:
- 1.Relative Units
- 2.Absolute Units
πΉ 1. Relative Units
Relative units are flexible units calculated relative to another value, such as:
- Parent element size
- Root font size
- Viewport size
Because they are flexible, they are very useful for responsive web design.
π Common Relative Units
| Unit | Description | Example |
|---|---|---|
| em | Relative to the current elementβs font size | 1em = 16px (example) |
| rem | Relative to the root (html) font size | 1rem = 16px |
| ex | Based on the height of lowercase (x) | Rarely used |
| ch | Based on the width of the (0) character | 1ch β width of one character |
| vw | 1% of the viewport width | 50vw = half screen width |
| vh | 1% of the viewport height | 100vh = full screen height |
| vmin | 1% of the smaller viewport dimension | Responsive layouts |
| vmax | 1% of the larger viewport dimension | Large-screen layouts |
| % | Relative to the parent element | 50% = half of parent size |
.container {
width: 80%;
font-size: 1.2rem;
height: 50vh;
}
π Explanation:
- 80% β Relative to the parent width
- 1.2rem β Relative to the root font size
- 50vh β Half of the viewport height
πΉ 2. Absolute Units
Absolute units are fixed-size units.
They do not automatically adapt to screen size or parent elements.
These units are often used for:
- Print design
- Static layouts
- Precise measurements
π Common Absolute Units
| Unit | Description |
|---|---|
| px | Pixel β most common screen unit |
| pt | Point β commonly used in print |
| pc | Pica β equal to 12 points |
| mm | Millimeter |
| cm | Centimeter |
| in | Inch (1in = 96px) |
π‘ Example of Absolute Units
h1 {
font-size: 32px;
}
.print-area {
width: 10cm;
}
π± Viewport Units Explained
Viewport units are based on the browser window size.
| Unit | Meaning |
|---|---|
| 1vw | 1% of viewport width |
| 1vh | 1% of viewport height |
.hero {
width: 100vw;
height: 100vh;
}
β This creates a section that fills the entire screen.
π§ Difference Between em and rem
This is one of the most important CSS concepts.
π em
em is relative to the font size of the current element.
.parent {
font-size: 20px;
}
.child {
font-size: 2em;
}
β Result:
2em = 40px
Because 2 Γ 20px = 40px.
π rem
rem is always relative to the root ( html ) font size.
html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
β Result:
2rem = 32px
π Recommended Usage
| Purpose | Recommended Units |
|---|---|
| Responsive layouts | %, vw, vh, rem, em |
| Font sizing | rem, em |
| Fixed UI elements | px |
| Printing | cm, mm, pt, in |
β οΈ Best Practices
β Use rem for Typography
rem creates more consistent and scalable font sizing.
β Use %, vw, and vh for Responsive Layouts
These units adapt better to different screen sizes.
β οΈ Avoid Overusing px
Fixed pixel values may create responsiveness issues on smaller devices.
π§ Quick Summary
- Relative units are flexible and responsive
- Absolute units are fixed-size measurements
- rem and % are widely used in modern web design
- vw and vh are useful for fullscreen layouts
- px is still common for precise control
