A percentage width is calculated relative to the width of the element's containing block, its parent, in normal flow, while a percentage height is calculated relative to the parent's height only if that parent has an explicitly defined height itself, otherwise a percentage height typically has no effect and the element's height instead simply grows to fit its content. min-width/max-width and min-height/max-height set additional constraints that cap or floor an element's size, taking priority over a conflicting width/height value, which is especially useful for responsive designs, letting an element grow or shrink within defined bounds rather than a single rigid, fixed size.
1Understanding Dimensions
A percentage width is calculated relative to the width of the element's containing block, its parent, in normal flow, while a percentage height is calculated relative to the parent's height only if that parent has an explicitly defined height itself, otherwise a percentage height typically has no effect and the element's height instead simply grows to fit its content. min-width/max-width and min-height/max-height set additional constraints that cap or floor an element's size, taking priority over a conflicting width/height value, which is especially useful for responsive designs, letting an element grow or shrink within defined bounds rather than a single rigid, fixed size.
A percentage height on a child element only works reliably if its parent has an explicitly set height, whether fixed or itself a percentage resolving to a defined value further up the chain — without that, a percentage height typically computes to nothing and has no effect, a common, confusing gotcha compared to how percentage widths behave.
.container {
width: 100%;
max-width: 1200px;
}2Practical Example
Here is a real-world application of Dimensions showing how it is used in production CSS code.
.parent {
height: 300px;
}
.child {
height: 50%;
}3Best Practices
Follow these guidelines when working with Dimensions:
1. Use max-width, often paired with width: 100%, for responsive elements that should shrink on small screens but never exceed a certain size on larger ones
2. Ensure a parent element has an explicitly defined height before relying on a percentage height on its children, since percentage heights typically don't work without one
3. Prefer min-content or max-content over a hardcoded pixel width when you want an element to size itself intrinsically based on its actual content
Tip: A percentage height on a child element only works reliably if its parent has an explicitly set height, whether fixed or itself a percentage resolving to a defined value further up the chain — without that, a percentage height typically computes to nothing and has no effect, a common, confusing gotcha compared to how percentage widths behave.
.container {
width: 100%;
max-width: 1200px;
}