CSS Units
1. Introduction to CSS Units
CSS Units specify the size or length of elements and text. Choosing the correct unit helps create responsive, consistent, and user-friendly web pages.
Why Use CSS Units?
- Control element sizes.
- Create responsive layouts.
- Improve readability.
- Maintain consistent spacing.
- Support different screen sizes.
h1{
font-size:32px;
}
.container{
width:80%;
}
2. What are CSS Units?
CSS Units are values used to define measurements in CSS. They are mainly divided into two categories:
| Category | Description |
|---|---|
| Absolute Units | Fixed-size units that do not change based on screen size. |
| Relative Units | Units that depend on another measurement, such as font size or viewport. |
.box{
width:300px;
}
.text{
font-size:2rem;
}
3. Absolute Units (px, cm, mm, in, pt, pc)
Absolute units have fixed sizes and are not affected by the size of the parent element or viewport.
| Unit | Description |
|---|---|
| px | Pixels (most commonly used). |
| cm | Centimeters. |
| mm | Millimeters. |
| in | Inches. |
| pt | Points (1pt = 1/72 inch). |
| pc | Picas (1pc = 12pt). |
.box{
width:300px;
height:150px;
}
h2{
font-size:24pt;
}
4. Relative Units (%, em, rem)
Relative units change according to another value such as the parent element or the root element.
| Unit | Relative To |
|---|---|
| % | Parent element. |
| em | Current element's font size. |
| rem | Root (<html>) font size. |
.container{
width:80%;
}
p{
font-size:1.5em;
}
h1{
font-size:2rem;
}
5. Viewport Units (vw, vh, vmin, vmax)
Viewport units are based on the size of the browser window, making them ideal for responsive layouts.
| Unit | Description |
|---|---|
| vw | 1% of the viewport width. |
| vh | 1% of the viewport height. |
| vmin | 1% of the smaller viewport dimension. |
| vmax | 1% of the larger viewport dimension. |
.hero{
width:100vw;
height:100vh;
}
h1{
font-size:5vw;
}
6. Other Useful Units (ch, ex, fr)
Besides absolute, relative, and viewport units, CSS also provides several useful units for typography and modern layouts.
| Unit | Description |
|---|---|
| ch | Width of the "0" (zero) character. |
| ex | Height of the lowercase "x" character. |
| fr | Represents a fraction of available space in CSS Grid. |
input{
width:30ch;
}
.grid{
display:grid;
grid-template-columns:1fr 2fr;
}
fr unit is one of the most useful units for creating responsive CSS Grid layouts.
7. Practical CSS Unit Examples
Example 1 : Responsive Container
.container{
width:80%;
margin:auto;
}
Example 2 : Responsive Font Size
body{
font-size:1rem;
}
h1{
font-size:2.5rem;
}
Example 3 : Full Screen Section
.hero{
height:100vh;
width:100vw;
}
Example 4 : CSS Grid Layout
.grid{
display:grid;
grid-template-columns:1fr 1fr 1fr;
gap:20px;
}
8. Advantages of CSS Units
- Create responsive layouts.
- Improve website accessibility.
- Provide consistent spacing and sizing.
- Support different screen sizes.
- Reduce layout issues.
- Improve user experience.
- Easy to maintain responsive designs.
| Feature | Available |
|---|---|
| Responsive Design | ✔ Yes |
| Flexible Layouts | ✔ Yes |
| Cross-Device Support | ✔ Yes |
9. Best Practices for Using CSS Units
- Use
remfor font sizes. - Use
%for flexible container widths. - Use
vwandvhfor full-screen layouts. - Use
frin CSS Grid layouts. - Avoid excessive use of fixed pixel values.
- Choose units based on the design requirement.
- Test layouts on different screen sizes.
10. CSS Units Comparison Table
| Unit | Category | Best Use |
|---|---|---|
| px | Absolute | Fixed-size elements |
| % | Relative | Flexible widths |
| em | Relative | Element-based font sizing |
| rem | Relative | Root-based font sizing |
| vw | Viewport | Responsive width |
| vh | Viewport | Full-screen height |
| fr | Grid | Grid column sizing |
| ch | Typography | Input width and text fields |
.container{
width:80%;
}
h1{
font-size:2rem;
}
.hero{
height:100vh;
}
.grid{
grid-template-columns:1fr 2fr;
}
11. Common CSS Unit Mistakes
Choosing the wrong CSS unit can make a website difficult to maintain and less responsive. Understanding common mistakes helps create better layouts.
| Mistake | Better Practice |
|---|---|
Using only px everywhere. |
Use rem, %, or vw where appropriate. |
Using em in deeply nested elements. |
Prefer rem for consistent font sizing. |
| Ignoring responsive units. | Use viewport and percentage units for responsive layouts. |
| Using fixed heights unnecessarily. | Allow content to grow naturally whenever possible. |
| Using the wrong unit for Grid layouts. | Use fr for flexible grid columns. |
❌ Poor
.container{
width:1200px;
}
✔ Better
.container{
width:90%;
max-width:1200px;
}
12. Browser Support
Modern CSS units are supported by all major web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
px, rem,
%, vw, vh, and
fr work reliably in modern browsers.
13. Real-World Use Cases
| Use Case | Recommended Unit |
|---|---|
| Font Size | rem |
| Container Width | % |
| Hero Section Height | vh |
| Responsive Heading | vw |
| CSS Grid Columns | fr |
| Input Width | ch |
.hero{
height:100vh;
}
.grid{
grid-template-columns:1fr 2fr;
}
input{
width:25ch;
}
14. CSS Units Interview Questions
- What are CSS Units?
- What is the difference between absolute and relative units?
- When should you use
reminstead ofem? - What is the purpose of the
vwunit? - How does the
vhunit work? - What is the
frunit used for? - Which unit is commonly used for responsive font sizes?
- Why is
%useful in layouts? - What does the
chunit represent? - Which CSS unit is best for Grid layouts?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Units.
- ✔ Absolute Units.
- ✔ Relative Units.
- ✔ Viewport Units.
- ✔ Other useful units such as
ch,ex, andfr. - ✔ Practical examples.
- ✔ Advantages and Best Practices.
- ✔ Common mistakes.
- ✔ Browser support.
- ✔ Real-world use cases and interview questions.
Quick Quiz
Which CSS unit is recommended for scalable font sizes?