Question:
Why are percentages for margin and padding in CSS calculated against the width of the containing block rather than its height?
Answer:
Although there is no official documentation explaining the precise reason, a plausible explanation lies in the potential for recursive dependency in the CSS box model when calculating heights with percentages.
Imagine an element with padding-top or padding-bottom set to 10% of its parent's height. This padding will affect the parent's height, which in turn is dependent on the height of the child element. This creates a loop that can result in either inaccurate height calculations or an infinite loop.
To avoid this circular dependency, the CSS specification dictates that margin and padding percentages be calculated against the width of the containing block, thereby breaking the cycle and ensuring consistent layout calculations.
The above is the detailed content of Why Are CSS Margin and Padding Percentages Width-Relative, Not Height-Relative?. For more information, please follow other related articles on the PHP Chinese website!