Why are margin/padding percentages in CSS always calculated against width?

后端 未结 5 1796
迷失自我
迷失自我 2020-11-22 17:07

If you look at the CSS box model spec, you\'ll observe the following:

The [margin] percentage is calculated with respect to the width

5条回答
  •  猫巷女王i
    2020-11-22 18:03

    For "n%" margin (and padding) to be the same for margin-top/margin-right/margin-bottom/margin-left, all four have to be relative to the same base. If top/bottom used a different base than left/right', then "n%" margin (and padding) wouldn't mean the same thing on all four sides.

    (Also note having the top/bottom margin relative to the width enables a weird CSS hack that allows you to specify a box with an unchanging aspect ratio ...even if the box is rescaled.)

提交回复
热议问题