CSS Units
Learn how CSS units control sizes, spacing, dimensions, typography, and responsive layouts using px, %, em, rem, vw, vh, and other measurement units.
Introduction
In the previous lesson, you learned how CSS colors control the visual appearance of text, backgrounds, borders, and other parts of a webpage.
Now you will learn how CSS controls size and measurement.
Every webpage contains measurements.
Text has a size, boxes have width and height, elements have spacing, borders have thickness, and layouts respond to different screen sizes.
CSS units tell the browser how large or small a value should be.
A value such as 20 does not always tell the browser what measurement you want. A unit such as px, %, rem, or vw gives the number meaning.
What are CSS Units?
CSS units are measurement values used to define the size, distance, spacing, position, and dimensions of elements.
.box {
width: 300px;
height: 150px;
margin: 20px;
padding: 1rem;
}In these declarations, px and rem are CSS units.
300px
300 → Numeric Value
px → Unit
Together → CSS Measurement<div class="box">
CSS Units
</div>.box {
width: 300px;
height: 100px;
font-size: 24px;
}| Property | Example Value | Purpose |
|---|---|---|
| width | 300px | Controls width |
| height | 200px | Controls height |
| font-size | 18px | Controls text size |
| margin | 20px | Controls outer spacing |
| padding | 1rem | Controls inner spacing |
| border-width | 2px | Controls border thickness |
| top | 10% | Controls position |
| gap | 2rem | Controls spacing between layout items |
Why Do We Need Units?
The browser needs to know how measurements should be calculated.
Different units allow developers to create fixed sizes, flexible sizes, scalable typography, and responsive layouts.
.fixed-box {
width: 300px;
}
.flexible-box {
width: 80%;
}
.scalable-text {
font-size: 2rem;
}
.screen-section {
height: 100vh;
}| Requirement | Possible Unit |
|---|---|
| Fixed border thickness | px |
| Flexible container width | % |
| Scalable text size | rem |
| Component-relative spacing | em |
| Full screen width | vw |
| Full screen height | vh |
One Unit for Everything
- Layouts may become rigid.
- Text may not scale well.
- Responsive design becomes difficult.
- Components may behave unexpectedly.
Appropriate Units
- Layouts can adapt.
- Typography can scale.
- Components become flexible.
- Responsive design becomes easier.
There is no single best CSS unit. The correct unit depends on what you are measuring and what the value should respond to.
Real-World Analogy
Imagine measuring objects in the real world.
You may use centimeters for a notebook, meters for a room, and percentages when describing how much space is occupied.
| Real World | CSS |
|---|---|
| Centimeters | px |
| Percentage of a room | % |
| Size relative to another object | em |
| Size relative to a common standard | rem |
| Percentage of screen width | vw |
| Percentage of screen height | vh |
Fixed Measurement
│
▼
px
Part of Available Space
│
▼
%
Relative to Text Size
│
├── em
└── rem
Relative to Screen
│
├── vw
└── vhRelative units calculate their final size from another value. Understanding that reference value is the key to understanding CSS units.
CSS Value Structure
Many CSS measurements contain a number followed immediately by a unit.
width: 500px;
font-size: 2rem;
padding: 5%;
height: 100vh;500px
│ │
│ └── Unit
│
└── Number| Value | Number | Unit |
|---|---|---|
| 20px | 20 | px |
| 50% | 50 | % |
| 2em | 2 | em |
| 1.5rem | 1.5 | rem |
| 100vw | 100 | vw |
| 100vh | 100 | vh |
Write 20px, not 20 px. The number and unit must normally remain together.
Types of CSS Units
CSS units can be divided into two major categories: absolute units and relative units.
| Category | Meaning | Examples |
|---|---|---|
| Absolute Units | Represent a generally fixed measurement | px, cm, mm, in, pt, pc |
| Relative Units | Calculated relative to another value | %, em, rem, vw, vh, vmin, vmax, ch |
CSS Units
│
├── Absolute Units
│ ├── px
│ ├── cm
│ ├── mm
│ ├── in
│ ├── pt
│ └── pc
│
└── Relative Units
├── %
├── em
├── rem
├── vw
├── vh
├── vmin
├── vmax
└── chAbsolute vs Relative Units
Absolute units use a fixed reference, while relative units calculate their size from another value.
Absolute Units
- Use a fixed measurement reference.
- Do not depend directly on parent size.
- Useful for precise small measurements.
- px is the most commonly used absolute CSS unit.
Relative Units
- Depend on another value.
- Can adapt to containers or screens.
- Useful for scalable interfaces.
- Common in responsive design.
.box {
width: 300px;
}.box {
width: 50%;
}1. Pixels (px)
The pixel unit is one of the most commonly used CSS units.
It is written using px.
.box {
width: 300px;
height: 150px;
padding: 20px;
border-width: 2px;
}<div class="box">
300px Wide Box
</div>.box {
width: 300px;
height: 100px;
background-color: #6c5ce7;
color: white;
}| Use Case | Example |
|---|---|
| Border thickness | 1px |
| Small icon size | 24px |
| Fixed component size | 300px |
| Small spacing | 8px |
| Border radius | 6px |
Advantages of px
- Easy to understand.
- Predictable measurement.
- Useful for precise details.
- Common in design tools.
Limitations of px
- Can create rigid layouts.
- May require responsive adjustments.
- Not always ideal for scalable typography.
- Large fixed values may overflow small screens.
Pixels are useful for precise measurements such as borders, icons, and small details. Problems usually occur when every part of a responsive layout is forced into fixed pixel sizes.
2. Percentage (%)
Percentage values define a measurement relative to another size.
.child {
width: 50%;
}If the parent is 600px wide, a child with width: 50% becomes 300px wide.
Parent Width = 600px
Child Width = 50%
600 × 50%
= 600 × 0.5
= 300px<div class="parent">
<div class="child">
50% Width
</div>
</div>.parent {
width: 100%;
}
.child {
width: 50%;
}| Percentage | Part of Reference |
|---|---|
| 25% | One quarter |
| 50% | One half |
| 75% | Three quarters |
| 100% | Full reference size |
Percentage Reference
A percentage value does not always use the same reference.
Its reference depends on the CSS property where the percentage is used.
| Property | Percentage Commonly Relates To |
|---|---|
| width | Width of the containing block |
| height | Height of the containing block when that reference is definite |
| font-size | Parent font size |
| line-height | Element font size |
| transform: translateX() | Element width |
| transform: translateY() | Element height |
.child {
width: 50%;
font-size: 120%;
}
.move {
transform: translateX(50%);
}The reference for a percentage depends on the CSS property. Always understand what the specific property uses as its percentage reference.
3. em Unit
The em unit is a relative unit connected to font size.
For the font-size property, em is calculated relative to the font size of the parent element.
.parent {
font-size: 20px;
}
.child {
font-size: 2em;
}The child font size becomes 40px because 2em means two times the parent font size.
Parent Font Size = 20px
Child Font Size = 2em
20px × 2
= 40px<div class="parent">
Parent Text
<div class="child">
Child Text
</div>
</div>When spacing and dimensions use em, changing the component font size can also scale related measurements.
How em is Calculated
The exact reference used by em depends on the property.
For font-size, em uses the inherited font size. For many other properties, em uses the calculated font size of the element itself.
.button {
font-size: 20px;
padding: 0.5em 1em;
}Because the button font size is 20px, 0.5em becomes 10px and 1em becomes 20px.
font-size = 20px
Vertical Padding:
0.5em × 20px = 10px
Horizontal Padding:
1em × 20px = 20pxThe em Compounding Problem
Nested elements using em for font-size can multiply relative sizes repeatedly.
<div class="level-one">
Level One
<div class="level-two">
Level Two
<div class="level-three">
Level Three
</div>
</div>
</div>.level-one,
.level-two,
.level-three {
font-size: 1.5em;
}If the starting font size is 16px, each nested level becomes larger than the previous level.
| Level | Calculation | Result |
|---|---|---|
| Level One | 16px × 1.5 | 24px |
| Level Two | 24px × 1.5 | 36px |
| Level Three | 36px × 1.5 | 54px |
When em is used for font-size on nested elements, the values can compound. This is one reason rem is often preferred for consistent typography.
4. rem Unit
The rem unit means root em.
It is calculated relative to the font size of the root html element.
html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}Because the root font size is 16px, 2rem becomes 32px.
Root Font Size = 16px
Heading Size = 2rem
16px × 2
= 32px<h1>2rem Heading</h1>
<p>1rem Paragraph</p>html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
p {
font-size: 1rem;
}How rem is Calculated
Every rem value refers back to the root font size rather than the immediate parent font size.
html {
font-size: 16px;
}
.small {
font-size: 0.75rem;
}
.normal {
font-size: 1rem;
}
.large {
font-size: 1.5rem;
}
.title {
font-size: 2rem;
}| rem Value | Calculation | Result |
|---|---|---|
| 0.75rem | 16 × 0.75 | 12px |
| 1rem | 16 × 1 | 16px |
| 1.5rem | 16 × 1.5 | 24px |
| 2rem | 16 × 2 | 32px |
html {
font-size: 16px;
}
.parent {
font-size: 30px;
}
.child {
font-size: 2rem;
}The child becomes 32px, not 60px, because rem uses the root font size rather than the parent font size.
Because rem uses one root reference, it is useful for consistent typography and spacing systems.
em vs rem
em
- Relative to a font-size reference.
- For font-size, uses the parent font size.
- Can compound in nested elements.
- Useful for component-relative scaling.
rem
- Relative to the root font size.
- Uses one consistent reference.
- Does not compound through nesting.
- Useful for global typography and spacing.
.parent {
font-size: 20px;
}
.child {
font-size: 2em;
}
/* Result: 40px */html {
font-size: 16px;
}
.parent {
font-size: 20px;
}
.child {
font-size: 2rem;
}
/* Result: 32px */| Feature | em | rem |
|---|---|---|
| Main Reference | Font size context | Root font size |
| Affected by nesting | Can be | No |
| Component scaling | Excellent | Good |
| Global consistency | Can require care | Excellent |
| Common use | Component-relative sizing | Typography and spacing |
5. Viewport Width (vw)
The vw unit is relative to the width of the viewport.
One vw represents one percent of the viewport width.
1vw = 1% of Viewport Width
50vw = 50% of Viewport Width
100vw = 100% of Viewport Width.box {
width: 50vw;
}If the viewport is 1200px wide, 50vw becomes 600px.
Viewport Width = 1200px
50vw = 50% of 1200px
1200 × 0.5
= 600pxWhen the viewport width changes, a vw-based measurement changes with it.
6. Viewport Height (vh)
The vh unit is relative to the height of the viewport.
One vh represents one percent of the viewport height.
1vh = 1% of Viewport Height
50vh = 50% of Viewport Height
100vh = 100% of Viewport Height.hero {
min-height: 100vh;
}<section class="hero">
Full Screen Hero Section
</section>Viewport height units are commonly used when a section should fill a large portion or all of the visible screen.
vw and vh Calculation
Viewport units are calculated directly from the browser viewport dimensions.
Viewport Width = 1200px
Viewport Height = 800px
1vw = 12px
1vh = 8px| Value | Calculation | Result |
|---|---|---|
| 10vw | 1200 × 10% | 120px |
| 25vw | 1200 × 25% | 300px |
| 50vw | 1200 × 50% | 600px |
| 10vh | 800 × 10% | 80px |
| 50vh | 800 × 50% | 400px |
| 100vh | 800 × 100% | 800px |
7. vmin Unit
The vmin unit uses the smaller viewport dimension.
Viewport:
Width = 1200px
Height = 800px
Smaller Dimension = 800px
1vmin = 1% of 800px
= 8px.shape {
width: 30vmin;
height: 30vmin;
}vmin can help create elements that remain proportional to the smaller side of the screen.
8. vmax Unit
The vmax unit uses the larger viewport dimension.
Viewport:
Width = 1200px
Height = 800px
Larger Dimension = 1200px
1vmax = 1% of 1200px
= 12px.background-shape {
width: 50vmax;
height: 50vmax;
}| Unit | Reference |
|---|---|
| vw | Viewport width |
| vh | Viewport height |
| vmin | Smaller viewport dimension |
| vmax | Larger viewport dimension |
Because vmax follows the larger viewport dimension, large values can easily extend beyond the visible screen.
9. ch Unit
The ch unit is based on the width of the zero character in the current font.
It is especially useful for controlling readable text line lengths.
.article {
max-width: 65ch;
}<p class="article">
Long paragraphs are easier to read when
the line length is controlled.
</p>A max-width measured in ch is commonly useful for paragraphs, documentation, articles, and other text-heavy content.
10. Unitless Values
Some CSS properties accept numbers without any unit.
.text {
line-height: 1.6;
}
.item {
flex-grow: 1;
}
.layer {
z-index: 10;
}
.transparent {
opacity: 0.5;
}| Property | Example | Unit Required? |
|---|---|---|
| line-height | 1.6 | No |
| opacity | 0.5 | No |
| z-index | 10 | No |
| font-weight | 700 | No |
| flex-grow | 1 | No |
<p class="text">
This paragraph uses a unitless line-height.
</p>.text {
font-size: 20px;
line-height: 1.6;
}A unitless line-height of 1.6 means the line height is calculated as 1.6 times the element font size.
Font Size = 20px
Line Height = 1.6
20px × 1.6
= 32pxOnly use unitless numbers where the CSS property allows them. A width such as width: 300 usually does not create a valid length.
Common CSS Units
| Unit | Name | Relative To | Common Use |
|---|---|---|---|
| px | Pixel | CSS pixel reference | Borders and precise sizes |
| % | Percentage | Property-specific reference | Flexible widths |
| em | Em | Font-size context | Component scaling |
| rem | Root Em | Root font size | Typography and spacing |
| vw | Viewport Width | Viewport width | Screen-based widths |
| vh | Viewport Height | Viewport height | Screen-based heights |
| vmin | Viewport Minimum | Smaller viewport dimension | Responsive shapes |
| vmax | Viewport Maximum | Larger viewport dimension | Large responsive effects |
| ch | Character Unit | Width of zero character | Readable text widths |
.page {
width: 90%;
max-width: 1200px;
min-height: 100vh;
padding: 2rem;
}
.article {
max-width: 65ch;
}
.title {
font-size: 3rem;
}
.button {
padding: 0.75em 1.5em;
border: 2px solid;
}Professional stylesheets commonly combine different units because each unit is suited to a different type of measurement.
Choosing the Right Unit
Choosing a unit depends on what the measurement should respond to.
| Requirement | Common Choice |
|---|---|
| Thin border | px |
| Flexible container width | % |
| Maximum page width | px or rem |
| Global text size | rem |
| Component-relative padding | em |
| Full viewport section | vh |
| Viewport-based width | vw |
| Readable paragraph width | ch |
| Responsive circular shape | vmin |
Fixed Requirement
- Small borders.
- Precise icons.
- Minor visual details.
- Controlled maximum sizes.
Flexible Requirement
- Responsive containers.
- Scalable typography.
- Full-screen sections.
- Adaptive spacing.
If the size should follow a container, screen, font size, or root size, choose a unit based on that relationship.
Complete Responsive Example
The following example combines several CSS units in one responsive component.
<section class="hero">
<div class="hero-content">
<span class="badge">
CSS Course
</span>
<h1>
Learn Modern CSS
</h1>
<p>
Build responsive and scalable
websites step by step.
</p>
<button>
Start Learning
</button>
</div>
</section>.hero {
min-height: 80vh;
width: 100%;
padding: 3rem 5%;
}
.hero-content {
width: 90%;
max-width: 65ch;
}
.badge {
font-size: 0.875rem;
padding: 0.5em 1em;
}
h1 {
font-size: 3rem;
}
p {
font-size: 1.125rem;
line-height: 1.7;
}
button {
font-size: 1rem;
padding: 0.75em 1.5em;
border: 2px solid;
}| Unit | Used For |
|---|---|
| vh | Hero minimum height |
| % | Responsive width and horizontal padding |
| ch | Readable content width |
| rem | Typography and major spacing |
| em | Button and badge spacing |
| px | Border thickness |
Browser Calculation Flow
The browser must convert CSS measurements into final values before drawing elements.
CSS Value
│
▼
Read Number
│
▼
Read Unit
│
├── px → Use Pixel Reference
├── % → Find Property Reference
├── em → Find Font Size Reference
├── rem → Find Root Font Size
├── vw → Read Viewport Width
└── vh → Read Viewport Height
│
▼
Calculate Final Value
│
▼
Create Layout
│
▼
Display Output| Value | Browser Reference |
|---|---|
| 300px | CSS pixel reference |
| 50% | Property-specific reference |
| 2em | Relevant font size |
| 2rem | Root font size |
| 50vw | Viewport width |
| 50vh | Viewport height |
The browser uses the relevant reference value to calculate the final size used for layout and rendering.
Real-World Applications
CSS units are used in every part of modern website development.
Responsive Layouts
Use percentages and viewport units to create flexible interfaces.
Typography
Use rem and em to create scalable text systems.
Components
Use em for buttons, badges, and component-relative spacing.
Hero Sections
Use viewport units for screen-based sections.
Articles
Use ch to control readable text line lengths.
Precise Details
Use pixels for borders, icons, and fine visual details.
Cards
Combine percentages, rem, and px for flexible card layouts.
Design Systems
Use consistent unit strategies for spacing and typography.
Advantages
CSS units provide multiple ways to create precise, flexible, scalable, and responsive designs.
Precise Control
Pixels provide control for small and exact visual details.
Responsive Design
Relative units allow layouts to adapt to available space.
Scalable Typography
rem and em support flexible text sizing.
Flexible Components
Component measurements can scale with their text size.
Screen-Based Layouts
Viewport units respond directly to browser dimensions.
Better Readability
The ch unit helps control comfortable text line lengths.
Reusable Systems
Consistent units support reusable spacing and typography scales.
Design Flexibility
Different units can be combined for different design requirements.
Common Beginner Mistakes
Write 20px, not 20 px.
A length such as width: 300 is usually invalid because the browser does not know which length unit to use.
Properties such as opacity and z-index use unitless values.
Large fixed pixel values can create rigid layouts that do not adapt well.
The percentage reference depends on the CSS property.
Percentage heights often require a definite height reference from the containing block.
em uses a font-size context, while rem always refers to the root font size.
Nested font sizes using em can multiply repeatedly.
rem uses the root html font size.
For font-size it uses the inherited parent size, but for many other properties it uses the element calculated font size.
vw is based on the viewport, while percentages use a property-specific reference.
vh is based on the viewport height.
A full viewport width can sometimes contribute to horizontal overflow depending on the page and scrollbar behavior.
Mobile browser interface changes can affect visible viewport behavior, so screen-height layouts should be tested carefully.
Viewport-relative values can become unexpectedly large on some screen shapes.
ch is based on the width of the zero character and does not guarantee an exact number of visible characters.
A large fixed width can overflow smaller screens.
Different units should be chosen according to clear sizing relationships.
Changing the html font size affects every rem-based measurement.
Relative units should be tested across small and large screens.
Best Practices
- Choose units according to what the measurement should respond to.
- Use px for precise small visual details.
- Use px carefully for large layout dimensions.
- Avoid forcing every layout measurement into fixed pixels.
- Use percentages for flexible widths when appropriate.
- Understand the reference used by each percentage value.
- Do not assume every percentage is relative to parent width.
- Use rem for consistent global typography.
- Use rem for reusable spacing scales when appropriate.
- Understand that rem always refers to the root font size.
- Use em when a component should scale with its text size.
- Use em for component-relative padding when useful.
- Watch for compounding when using em for nested font sizes.
- Understand that em references can depend on the property.
- Use vw for measurements that should follow viewport width.
- Use vh for measurements that should follow viewport height.
- Test viewport-based layouts on multiple screen sizes.
- Test viewport-height layouts on mobile devices.
- Use vmin when a measurement should follow the smaller viewport dimension.
- Use vmax carefully because values can become very large.
- Use ch to control readable text line lengths.
- Do not treat ch as an exact character counter.
- Use unitless line-height when appropriate.
- Use unitless values only where the property allows them.
- Do not add px to opacity values.
- Do not add px to z-index values.
- Do not add px to flex-grow values.
- Do not place spaces between numbers and units.
- Write 20px instead of 20 px.
- Remember that zero can often be written without a unit.
- Use 0 instead of 0px when the unit adds no useful meaning.
- Combine units intentionally.
- Use % for flexible containers.
- Use max-width to prevent content from becoming too wide.
- Use min-width carefully to avoid overflow.
- Use max-width with large fixed widths when responsive behavior is required.
- Use min-height instead of fixed height when content may grow.
- Avoid fixed heights for text-heavy components.
- Allow content to determine height when possible.
- Use viewport units for screen-relative design effects.
- Avoid using viewport units for every measurement.
- Use rem for headings and body text when building a scalable type system.
- Use consistent spacing units throughout a project.
- Create a spacing scale instead of choosing random values.
- Use consistent typography ratios.
- Avoid random combinations such as 13px, 1.27rem, and 2.8em without a design reason.
- Document the unit strategy in large projects.
- Keep component units predictable.
- Use relative units when the design should adapt.
- Use fixed units when the measurement should remain controlled.
- Understand the difference between viewport and container references.
- Remember that 50vw means half the viewport width.
- Remember that width: 50% does not necessarily equal 50vw.
- Remember that 100vh means the full viewport height reference.
- Remember that 1rem equals one root font size.
- Remember that 1em depends on a font-size context.
- Check inherited font sizes before debugging em calculations.
- Check the html font size before debugging rem calculations.
- Check parent or containing-block dimensions before debugging percentages.
- Check viewport dimensions before debugging vw and vh.
- Use browser developer tools to inspect computed values.
- Inspect the final pixel value calculated by the browser.
- Resize the browser while testing relative units.
- Test narrow mobile screens.
- Test wide desktop screens.
- Test portrait orientation.
- Test landscape orientation.
- Check for horizontal overflow.
- Check whether fixed widths exceed the available space.
- Use box-sizing appropriately when dimensions include padding and borders.
- Remember that width calculations can interact with padding and borders.
- Do not blame the unit before checking the box model.
- Use max-width for responsive images and containers.
- Use min() and max() when combining responsive limits in advanced layouts.
- Use clamp() when a value needs minimum, preferred, and maximum limits.
- Learn modern viewport units when building complex mobile full-screen interfaces.
- Use dvh, svh, and lvh when their specific viewport behavior is needed.
- Do not introduce advanced units before understanding basic viewport units.
- Keep beginner code simple before optimizing the unit strategy.
- Choose readability over clever calculations.
- Use relative units consistently in design systems.
- Use absolute units where precision is genuinely required.
- Avoid unnecessary unit conversions.
- Do not convert px to rem only to make the code appear modern.
- Use rem when root-relative behavior is actually useful.
- Use em when component-relative behavior is actually useful.
- Use % when container-relative behavior is useful.
- Use viewport units when viewport-relative behavior is useful.
- Use ch when text-width behavior is useful.
- Understand the reference before choosing the unit.
- Test the result rather than relying only on calculations.
- Check accessibility when sizing text.
- Avoid making essential text extremely small.
- Allow browser zoom to remain useful.
- Avoid layouts that break when text becomes larger.
- Test components with longer content.
- Test buttons with longer labels.
- Test cards with multiple lines of text.
- Test headings on small screens.
- Use responsive limits for very large text.
- Use flexible widths for reusable components.
- Avoid fixed widths for unknown content.
- Keep line lengths comfortable for reading.
- Use a maximum text width for articles.
- Use consistent units for related values.
- Keep the sizing system understandable.
- Keep the sizing system responsive.
- Keep the sizing system maintainable.
Use px for precise details, % for container relationships, rem for root-relative scaling, em for component-relative scaling, and viewport units for screen relationships.
Frequently Asked Questions
What is a CSS unit?
A CSS unit defines how a numeric measurement such as width, height, spacing, or font size should be interpreted.
What are the main types of CSS units?
CSS units are commonly grouped into absolute units and relative units.
What is an absolute unit?
An absolute unit uses a generally fixed measurement reference. The most commonly used example in screen design is px.
What is a relative unit?
A relative unit calculates its final value from another reference such as a parent size, font size, root font size, or viewport size.
What does px mean?
px means pixel and is commonly used for precise CSS measurements.
Are pixels bad for responsive design?
No. Pixels are useful for many measurements. Problems occur when large fixed sizes are used without considering smaller screens.
What does percentage mean in CSS?
A percentage creates a value relative to a reference determined by the CSS property.
Is width: 50% always half the screen?
No. It is commonly half the width of the containing block, not necessarily half the viewport.
Why does percentage height sometimes not work?
Percentage heights often need a definite height reference from the containing block.
What does em mean?
em is a relative unit connected to font size.
What is 2em?
It means two times the relevant font-size reference.
Is em always relative to the parent?
For font-size, it uses the inherited parent font size. For many other properties, it uses the calculated font size of the element.
What is em compounding?
Nested elements using em for font-size can repeatedly multiply the inherited size.
What does rem mean?
rem means root em and is relative to the root html element font size.
What is 1rem?
It equals one root font size.
Is 1rem always 16px?
No. It is often 16px by default, but it depends on the root font size and user or browser settings.
What is the difference between em and rem?
em uses a font-size context, while rem always uses the root font size.
Which is better, em or rem?
Neither is always better. rem is useful for global consistency, while em is useful when a component should scale relative to its own text size.
What does vw mean?
vw means viewport width. One vw equals one percent of the viewport width.
What does 100vw mean?
It represents the full viewport width reference.
What does vh mean?
vh means viewport height. One vh equals one percent of the viewport height.
What does 100vh mean?
It represents the full viewport height reference.
What is the difference between 100% and 100vw?
A percentage uses a property-specific reference, while 100vw uses the viewport width.
What is the difference between 100% height and 100vh?
A percentage height depends on a containing-block height reference, while 100vh uses the viewport height.
What does vmin mean?
vmin uses the smaller of the viewport width and viewport height.
What does vmax mean?
vmax uses the larger of the viewport width and viewport height.
What does ch mean?
ch is based on the width of the zero character in the current font.
Why is ch useful?
It is useful for controlling readable text widths, such as limiting an article to around 60 or 70ch.
Does 65ch mean exactly 65 characters?
No. Character widths vary, so ch does not guarantee an exact visible character count.
Can CSS values have no unit?
Yes. Some properties accept unitless numbers, such as opacity, z-index, flex-grow, and unitless line-height.
Does zero need a unit?
In many CSS length contexts, zero can be written without a unit.
Should I write 0px or 0?
Both may work in many length contexts, but 0 is usually simpler.
Why is width: 300 not working?
A non-zero CSS length normally needs a unit, such as width: 300px.
Why is 20 px invalid?
The number and unit must remain together, so write 20px.
Which unit should I use for font size?
rem is commonly useful for global typography, while em can be useful for component-relative text sizing.
Which unit should I use for width?
It depends on the design. Percentages are useful for flexible widths, pixels or rem can provide limits, and viewport units can create screen-relative widths.
Which unit should I use for spacing?
rem is useful for consistent global spacing, while em is useful when spacing should scale with a component font size.
Which unit should I use for borders?
Pixels are commonly used for thin and precise border widths.
Can I mix CSS units?
Yes. Real projects commonly combine multiple units according to different sizing requirements.
What comes after CSS units?
The next lesson covers CSS backgrounds, including background colors, images, repeat behavior, position, size, attachment, and shorthand syntax.
Key Takeaways
- CSS units define measurements.
- Measurements are used for width, height, spacing, typography, borders, and positioning.
- Many CSS measurements contain a number and a unit.
- Write the number and unit together.
- Write 20px, not 20 px.
- CSS units can be absolute or relative.
- Absolute units use a generally fixed reference.
- Relative units calculate values from another reference.
- px is the most commonly used absolute unit for screen design.
- Pixels are useful for precise measurements.
- Large fixed pixel values can create rigid layouts.
- Percentage values are relative.
- The reference for a percentage depends on the property.
- width: 50% commonly means half the containing block width.
- em is connected to font size.
- For font-size, em uses the inherited parent font size.
- Nested em font sizes can compound.
- rem means root em.
- rem always uses the root font size.
- 1rem equals one root font size.
- rem is useful for consistent typography.
- em is useful for component-relative scaling.
- vw is relative to viewport width.
- 1vw equals one percent of viewport width.
- vh is relative to viewport height.
- 1vh equals one percent of viewport height.
- 100vh represents the full viewport height reference.
- vmin uses the smaller viewport dimension.
- vmax uses the larger viewport dimension.
- ch is based on the width of the zero character.
- ch is useful for readable text widths.
- Some CSS properties accept unitless values.
- opacity is unitless.
- z-index is unitless.
- Unitless line-height can scale with font size.
- Different units solve different problems.
- Real projects commonly combine several units.
- Use px for precise details.
- Use % for flexible container relationships.
- Use rem for root-relative scaling.
- Use em for component-relative scaling.
- Use viewport units for screen-relative measurements.
- Use ch for text-width control.
- Understand the reference before choosing a unit.
- Test relative units across different screen sizes.
- A good unit strategy improves responsiveness and maintainability.
Summary
CSS units tell the browser how measurements such as width, height, spacing, typography, and positioning should be calculated.
Absolute units use a generally fixed measurement reference, while relative units calculate their values from another reference.
Pixels are commonly used for precise measurements such as borders, icons, and controlled dimensions.
Percentages create flexible values based on a reference determined by the CSS property.
The em unit is connected to font size and can be useful for component-relative scaling.
Nested em font sizes can compound because each level may use the calculated size of its parent.
The rem unit uses the root font size and provides a consistent reference for typography and spacing.
Viewport units such as vw and vh create measurements based on the width and height of the browser viewport.
The vmin and vmax units use the smaller and larger viewport dimensions.
The ch unit is useful for controlling readable text widths.
Some CSS properties accept unitless values, including opacity, z-index, flex-grow, and unitless line-height.
Professional CSS commonly combines multiple units because each unit is designed for a different relationship.
The best unit depends on what the measurement should follow: a fixed reference, a container, a font size, the root font size, or the viewport.
In the next lesson, you will learn CSS backgrounds and understand how to use background colors, images, repeat behavior, positioning, sizing, attachment, and shorthand properties.