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

Min and Max Dimensions

Set boundaries with min-width, max-width, min-height, and max-height properties.

Min and Max Dimensions

You'll learn how to set flexible size boundaries that keep elements responsive yet constrained.

What Are Min and Max Dimensions?

Sometimes you want an element to grow or shrink with the page, but not too much. Min and max dimension properties let you set boundaries on how small or large an element can become. Think of them as guardrails that keep your layout from breaking at extreme viewport sizes.

The Four Boundary Properties

CSS gives you four properties to control size limits:

  • min-width — the smallest width an element can shrink to
  • max-width — the largest width an element can expand to
  • min-height — the smallest height an element can shrink to
  • max-height — the largest height an element can expand to

How They Work

Imagine you have a container with width: 50%. On a huge screen, that might be 1000 pixels wide—too much! On a tiny phone, it might be 150 pixels—too cramped!

.container {
  width: 50%;
  min-width: 300px;  /* Never narrower than 300px */
  max-width: 800px;  /* Never wider than 800px */
}

Now your container flexibly scales between 300px and 800px, ignoring the 50% calculation if it would violate those boundaries.

Common Use Case

.article {
  width: 100%;
  max-width: 700px;  /* Prevents lines of text from getting too wide */
  margin: 0 auto;    /* Centers the article */
}

This pattern creates readable content that spans full-width on mobile but stays comfortably narrow on desktop.

Key Takeaway: Use min-width, max-width, min-height, and max-height to set size boundaries that keep elements flexible but controlled, preventing layouts from breaking at extreme sizes.