为什么overflow-x:hidden让我绝对定位的元素变得固定? [英] Why does overflow-x: hidden make my absolutely positioned element become fixed?

查看:150
本文介绍了为什么overflow-x:hidden让我绝对定位的元素变得固定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚,为什么设置 overflow-x:hidden 到HTML页面的主体使我的元素 position:fixed 即使我将它设置为 position:absolute



效果更好理解此演示



以下是代码:



HTML:

 < div class ='background'> ; / div> 
< div class ='page'>< / div>
< div class ='page'>< / div>
< div class ='page'>< / div>
< div class ='page'>< / div>

CSS:

  html,body {
width:100%;
height:100%;
padding:0; margin:0;
overflow-x:hidden; / *如果我删除这行一切都是我期望它是! * /
}

div.page {
positon:relative;
width:100%;
height:100%;
min-height:100%;
border:1px solid red;
margin-bottom:200px;
}

div.background {
background:blue;
position:absolute;
width:100%;
height:20%;
}



问题



overflow-x:hidden 和定位之间的关系是什么?为什么设置该属性导致我的元素变成 position:fixed 而不是 position:absolute

解决方案

元素仍然是 position:absolute ,但它看起来是固定的,和位置溢出和框模型之间的交互。令人难以置信的是,这些行为都不是未指定的,或者是任何浏览器中的错误 - 实际上完全是通过设计,如果有点反直觉。



基本上归结为以下:





奇怪的是,这也适用于IE6



< hr>

更长的解释:




I'm trying to figure out, why setting overflow-x: hidden to body of an HTML page makes my element position: fixed even if I set it to position: absolute.

The effect is better understood in this demo.

Here is the code:

HTML:

<div class='background'></div>
<div class='page'></div>
<div class='page'></div>
<div class='page'></div>
<div class='page'></div>

CSS:

html, body { 
  width: 100%;
  height: 100%;
  padding: 0; margin: 0; 
  overflow-x: hidden; /* If I remove this line everything is how I expect it to be! */
}

div.page {
  positon: relative;
  width: 100%;
  height: 100%;    
  min-height: 100%;
  border: 1px solid red;
  margin-bottom:200px;
}

div.background {
  background: blue;
  position: absolute;
  width: 100%;
  height: 20%;
}

Question

What's the relationship between overflow-x: hidden and positioning? Why does setting that property cause my element to become position: fixed instead of position: absolute?

解决方案

The element is still position: absolute, but it appears fixed thanks to some rather complicated interactions between position, overflow and the box model. Incredibly, none of these behaviors is unspecified or a bug in any browser — it's actually completely by design, if a little counter-intuitive.

It basically boils down to the following:

  • An absolutely positioned element is anchored to the viewport unless any of its ancestors is positioned. (This is why adding position: relative to body works as suggested in another answer.)

  • You have width: 100%; height: 100%; on both html and body; this prevents the viewport from ever expanding beyond its visible area, so the viewport never scrolls.

  • Since the viewport doesn't scroll, neither does the absolutely positioned element. This causes it to appear fixed even though the rest of the page does scroll.

Oddly enough, this works on IE6 too.


Longer explanation:

  • An absolutely positioned element is anchored to the viewport unless any of its ancestors is positioned.

    The spec on the overflow property, which incidentally contains another example of the same issue you're observing where an element with overflow: scroll is interacting with an absolutely positioned descendant element, states the following:

    This property specifies whether content of a block container element is clipped when it overflows the element's box. It affects the clipping of all of the element's content except any descendant elements (and their respective content and descendants) whose containing block is the viewport or an ancestor of the element.

    Your absolutely positioned element is a descendant whose containing block is the viewport (also the containing block of the html element), because both html and body are not positioned. This is according to another section of the spec. This prevents overflow clipping on html and body from having any effect on your absolutely positioned element since it's anchored to the viewport.

  • You have width: 100%; height: 100%; on both html and body; this prevents the viewport from ever expanding beyond its visible area, so the viewport never scrolls.

    The spec then states the following, further down the same section:

    UAs must apply the 'overflow' property set on the root element to the viewport. When the root element is an HTML "HTML" element or an XHTML "html" element, and that element has an HTML "BODY" element or an XHTML "body" element as a child, user agents must instead apply the 'overflow' property from the first such child element to the viewport, if the value on the root element is 'visible'. The 'visible' value when used for the viewport must be interpreted as 'auto'. The element from which the value is propagated must have a used value for 'overflow' of 'visible'.

    To put it more simply:

    • If html is not overflow: visible, apply that to the viewport instead, and turn html to overflow: visible. The overflow value given to body is not affected.

    • If html is overflow: visible, but body is not, apply that to the viewport instead, and turn body to overflow: visible.

    (Setting overflow-x or overflow-y to anything other than visible for an element causes the shorthand overflow to no longer be equal to visible for that element.)

    Normally, this means the viewport should scroll naturally along with html and body, since only one scrollbar should exist at a time.

    However... you also give both html and body a width and height of 100%! That means 100% those of its container. The container of body is html, and the container of html is the viewport. But since you can't actually use CSS to control the size of the viewport — that's entirely handled by the browser — you're left with the two elements being constrained to 100% the height of the visible portion of the viewport (also known as the fold). The viewport itself does not have to expand beyond the fold since none of its contents require more space than is visible (note that absolutely positioned elements are never taken into account). The viewport therefore does not generate a scrollbar (and neither does html); the scrollbar that you see belongs to body.

    If you had not set the width or height properties, then they would have defaulted to auto, causing both html and body to expand with their contents and always be the same size as the entire viewport area, including the area below the fold. This prevents body from ever generating a scrollbar because it will always stretch to fit its contents, so you only see the viewport scrollbar, and the absolutely positioned element will scroll with the rest of the page.

  • Since the viewport doesn't scroll, neither does the absolutely positioned element. This causes it to appear fixed even though the rest of the page does scroll.

    What happens when you scroll, then, is that you're really scrolling the body element. Since the absolutely positioned element is anchored to the viewport, which never scrolls, it appears to be fixed instead of scrolling.

    By the way, this is also why the element appears to overlap the scrollbar when it doesn't scroll at all. The scrollbar belongs to body, which is beneath the absolutely positioned element. If you remove the overflow-x declaration or the width and height declarations from html and body, the scrollbar that you see belongs to the viewport instead. However, if you position body, the scrollbar still belongs to body, but the element also becomes a child of body so it won't overlap the scrollbar.

这篇关于为什么overflow-x:hidden让我绝对定位的元素变得固定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆