Course contentsShow
← Web Development
Lesson 157 of 2,9823. CSS FundamentalsFree lesson

Percentage Values

Learn how percentages work relative to parent dimensions and when to use them for fluid layouts.

Percentage Values

You'll learn how to use percentage-based sizes that scale fluidly with their parent container.

How Percentages Work

In CSS, percentage values are always relative to the parent element's dimensions. When you set width: 50%, you're saying "make this element half the width of its parent container." If the parent is 800px wide, the child becomes 400px wide. If the parent shrinks to 400px, the child automatically adjusts to 200px.

Think of it like inheritance: a child gets a percentage of what the parent has. The parent's size is the "100%" reference point.

Horizontal vs Vertical Percentages

  • Width percentages reference the parent's width
  • Height percentages reference the parent's height (but the parent must have an explicit height set!)
  • Padding and margin percentages always reference the parent's width, even for top/bottom values (this surprises many developers)

When to Use Percentages

Use percentages for fluid layouts that adapt to screen size:

.container {
  width: 80%; /* Fills 80% of viewport/parent */
  margin: 0 auto; /* Centers it */
}

.column {
  width: 48%; /* Two columns with gap room */
}

Percentages shine when you want elements to grow and shrink proportionally as the browser resizes, creating responsive designs without media queries.

Common Gotcha

Remember: percentages need a reference point. If a parent has no defined height, height: 50% on the child has nothing to calculate from and won't work as expected.

Key Takeaway: Percentage values are relative to the parent element's dimensions, making them perfect for fluid, responsive layouts that adapt automatically to container size changes.