Minimum and maximum sizes
Definition and purpose
Minimum and maximum size properties bound an element’s preferred size. They help make flexible panels without forcing one width on all screens.
Responsive cap
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>body { margin: 0; } #a { width: 100%; max-width: 500px; }</style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>The panel follows a narrow viewport and stops at 500px on a wide one.
Syntax and mental model
width: 100%; max-width: 320px lets a block grow up to its cap. min-width prevents shrinking below a floor and can cause overflow. When min exceeds max, the minimum wins. The same ideas apply to height, subject to percentage resolution and layout context.
Conflicting bounds
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>#a { width: 80px; min-width: 150px; max-width: 100px; }</style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>The minimum wins over the contradictory maximum.
Common mistakes
Avoid a large minimum width on narrow screens. A max-width alone does not guarantee that long unbroken text will wrap.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Width, height and box sizing. Next lesson: Margin collapse and negative margins.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Minimum and maximum sizes — knowledge check
Five questions on minimum and maximum sizes.
Which caps a preferred width?
Which wins when min-width exceeds max-width?
What can a large min-width cause on small screens?
What lets a text panel grow beyond 80px high?
Does max-width alone ensure all long words wrap?