html - 为什么页面宽度会影响 Firefox 中的 "margin-top:50%"?

标签 html css firefox

在这个 JSFiddle 中

http://jsfiddle.net/9UMRk/

div 的 margin-top 为 50%。我希望将其调整为页面高度。

但是,如果您在 Firefox(3 和 4)中调整窗口大小,您会看到 div 的垂直定位受页面宽度影响,但不受页面高度影响。

为什么?

最佳答案

这看起来确实违反直觉,但实际上符合边距的盒模型,即 in the CSS level 2.1 spec :

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.

in the CSS3 spec (更清楚的是 IMO):

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).

(粗体强调我的意思。)

关于html - 为什么页面宽度会影响 Firefox 中的 "margin-top:50%"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7386353/

相关文章:

javascript - 使用数据表打开模式

html - 如何让导航栏元素在 bootstrap 3 中内联显示?

javascript - Bootstrap 下拉菜单表现怪异

html - 如何在 <ion-img> 上设置高度

javascript - 'perspective' 更改后,Firefox 和 Safari 不尊重 z-index

javascript - 点击图片映射时addClass

html - 如何使水平对齐的文本和图标在屏幕调整大小时保持一致?

javascript - 使用 moment.js 从时区偏移转换为本地时间

javascript - 如何使用 JavaScript/XUL 向新的浏览器选项卡发出发布请求?

javascript - Noob JavaScript 错误(无法设置 null 的属性 'src')