margin/padding百分比实现

本文依赖于一个基础却又容易混淆的css知识点:当margin/padding取形式为百分比的值时,无论是left/right,还是top/bottom,都是以父元素的width为参照物的!
也许你会说,left/right以父元素的width为参照物好理解,但是top/bottom为什么也是以父元素的width为参照物的呢?网上众说纷纭,关键还是看W3C的规范:
Note that in a horizontal flow, percentages on ‘margin-top’ and ‘margin-bottom’ are relative to the width of the containing block, not the height (and in vertical flow, ‘margin-left’ and ‘margin-right’ are relative to the height, not the width).

Note that percentages on ‘padding-top’ and ‘padding-bottom’ are relative to the width of the containing block, not the height (at least in a horizontal flow; in a vertical flow they are relative to the height).

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,181评论 1 92
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,208评论 0 23
  • 武汉的天气不好 你,有没有带伞 你,好不好 不要脚步匆匆 你看,青天还有星空里 有飞鸟略过的痕迹 它有没有衷诉着 ...
    侧珥倾聽阅读 154评论 8 4
  • 第四站:上海 顺风车带了4个人,另外一个乘客在这次旅行的路上给我上了一课,车上的1.5个小时,声音没有停歇过。 美...
    Bbbbbb嘭阅读 152评论 0 2

友情链接更多精彩内容