使用 css 溢出会导致奇怪的行为
我有这个网站。
我设置了一个位置固定
的页脚,其高度
和宽度
分别为100%
和left :0px
和 bottom:0px
。
当我调整浏览器大小时,页脚上方的内容会被挡在页脚后面,即使我在其上方的 div 元素上有 overflow:auto
。
这是调整浏览器大小且内容被阻止时的屏幕截图。
I have this website.
I have set a footer with position fixed
with a certain height
and width
of 100%
and left:0px
and bottom:0px
.
The content above the footer gets blocked behind the footer when I resize the browser even though I have overflow:auto
on div element above it.
Here is the screen shot when the browser is resized and the content is blocked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过为包装器提供等于页脚高度+填充的底部边距来解决此问题,因此在这种情况下:
解释是,当您使用
position:fixed
定位某些内容时,您以与position:absolute
相同的方式将其从文档流中删除(不同之处在于,fixed 会将您的内容固定到视口而不是文档,因此不会滚动)。这意味着正常定位的内容不受它的影响,并且表现得好像它不存在一样。在您的情况下,您的包装 div 正在使用所有可用空间,其中包括页脚后面的空间。通过在包装器的底部添加边距,您可以有效地将其停止在页脚的开头,并且如果需要更多空间,则会显示滚动条。
You can fix this by giving your wrapper a bottom margin that is equal to to the height+padding of your footer, so in this case:
The explanation is that when you position something using
position:fixed
, you remove it from the flow of the document in the same way as withposition:absolute
(the difference being that fixed will pin your content to the viewport rather than the document and so will not scroll). That means that normally positioned content is not affected by it, and acts as if it is not there.In your case, your wrapper div was using all of the available space, which included space that was behind your footer. By adding a margin to the bottom of the wrapper, you are effectively stopping it at the start of the footer, and if more space is required a scrollbar will be displayed.
您可能需要这样的内容:
然后是 CSS 文件:
这样,如果页面很长,页脚就会位于正常的页面流位置。然而,如果页面比窗口高度短,它将停留在窗口的底部。
You probably want something like this:
And then the CSS file:
That way the footer is in the normal page flow place if the page is long. If however the page is shorter than the window height, it'll stay at the bottom of the window.
如果页脚只是直接到底部,您是否需要页脚
position:fixed;
?为什么不直接做position:absolute;
呢?或者降低页脚的 z-index 使其位于内容后面。Do you need to have the footer
position:fixed;
if it's just going straight to the bottom anyways? Why not just doposition:absolute;
? Either that, or lower thez-index
of the footer so it goes behind the content.你能不能在主体div和页脚之间放置一个div,然后为其添加一个clear:both;类?
could you not put a div inbetween the body div and the footer and then add a class to it of clear:both;
图像被停靠是因为它有
position:fixed
,这会将其粘合到窗口上的该点,而不是页面整体流程中的该点。人们使用同样的技术来制作不随页面滚动的导航页眉和页脚。对于您想要做的事情,您应该浮动页面内容并将
clear: Both
应用到页脚的 css,这将导致它清除左右浮动元素并强制它底部。使用固定或绝对定位将允许其他元素隐藏在页脚后面。另一种方法是使用
position:absolute
而不是position:fixed
将页脚粘合到页面底部,然后将主要内容包装在中。 div>
并指定底部边距等于页脚的高度。另一方面,在声明 html 标签和添加属性时使用小写字符被认为是最佳实践;我注意到你有很多都是大写的。将 css 和 javascript 卸载到外部文件通常也是一个好主意,然后使用
导入它们和
分别。
The image is getting docked because it has
position: fixed
, which glues it to that spot on the window, not that spot in the overall flow of the page. It's the same technique that people use to make those nav headers and footers that don't scroll with the page.For what you want to do, you should float the content of your page and apply
clear: both
to the css of your footer, which will cause it to clear both right and left floated elements and force it to the bottom. Positioning with fixed or absolute will allow other elements to be hidden behind the footer.Another approach would to use
position: absolute
instead ofposition: fixed
to glue the footer to the bottom of the page, then wrap the main content in a<div>
and give that a bottom margin equal to the height of the footer.On another note, it is considered best practice to use lowercase characters when declaring html tags and adding attributes; I noticed you had quite a few in all uppercase. It is also usually a good idea to offload your css and javascript to external files, then import them with
<link rel="stylesheet" type="text/css" src="path_to_css_file_from_html_file_location">
and<script src="path_to_javascript_file_from_html_file_location" >
, respectively.