CSS 百分比 margin & padding

转载:https://swordair.com/css-persentage-margin-and-padding/?utm_source=tuicool&utm_medium=referral

margin和padding都可以使用百分比值的,但有一点可能和通常的想法不同,就是 margin-top | margin-bottom | padding-top | padding-bottom 的百分比值参照的不是容器的高度,而是宽度

引用标准(2.1)原来的表达:

The percentage is calculated with respect to the width of the generated box's containing block. Note that this is true for 'margin-top' and 'margin-bottom' as well. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1.

The percentage is calculated with respect to the width of the generated box's containing block, even for 'padding-top' and 'padding-bottom'. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1.

宽度参照宽度这点毫无疑问,但高度参照的也是宽度这点,可能就和通常的思路相左,因为毕竟height应用百分比参照的,依然是容器的高度。

关于为什么标准要这么定义,为了保持padding(margin)四个值的一统一

padding(margin)的值无论引用何种计量,四个值都一直保持统一,难道单独给百分比开特例?结合第一条的多情况处理,如果标准定义padding-top参照容器高度的话,恐怕要列出至少4条以上的例外说明——而这对无论谁,都没有好处:)

事实上,垂直padding参照体是宽度这点也非常有用。虽然早期固化像素的设计中百分比值几乎不应用在padding或者margin上,但随着移动自适应的布局的需要,百分比也逐渐稀疏平常。比如配合background-sizing保持背景的比例,配合media query调整对应的间距,不一而足。这些应用都基于一个事实:无论垂直还是水平,百分比值始终参考宽度

而宽度,实际上,正是自适应和现代web设计的灵魂。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值