使用 css 溢出会导致奇怪的行为

发布于 2024-11-28 02:35:44 字数 450 浏览 3 评论 0原文

我有这个网站

我设置了一个位置固定的页脚,其高度宽度分别为100%left :0pxbottom:0px

当我调整浏览器大小时,页脚上方的内容会被挡在页脚后面,即使我在其上方的 div 元素上有 overflow:auto

这是调整浏览器大小且内容被阻止时的屏幕截图。

https://i.sstatic.net/y4L6v.png

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.

https://i.sstatic.net/y4L6v.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寂寞笑我太脆弱 2024-12-05 02:35:44

您可以通过为包装器提供等于页脚高度+填充的底部边距来解决此问题,因此在这种情况下:

#wrapper {
    margin-bottom: 213px;
    overflow: auto;
}

解释是,当您使用 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:

#wrapper {
    margin-bottom: 213px;
    overflow: auto;
}

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 with position: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.

懒猫 2024-12-05 02:35:44

您可能需要这样的内容:

<body>
  <div id="page">
    <div id="logo">...</div>
    <div id="head">...</div>
    <div id="wrapper">...</div>
    <div id="footerSpacer"></div>
    <div id="footer">...</div>
  </div>
</body>

然后是 CSS 文件:

html, body {
    height: 100%;
}
page {
    min-height: 100%;
    position: relative;
}
footerSpacer {
    height: 200px;
}
footer {
    position: absolute;
    bottom: 0;
    height: 200px;
}

这样,如果页面很长,页脚就会位于正常的页面流位置。然而,如果页面比窗口高度短,它将停留在窗口的底部。

You probably want something like this:

<body>
  <div id="page">
    <div id="logo">...</div>
    <div id="head">...</div>
    <div id="wrapper">...</div>
    <div id="footerSpacer"></div>
    <div id="footer">...</div>
  </div>
</body>

And then the CSS file:

html, body {
    height: 100%;
}
page {
    min-height: 100%;
    position: relative;
}
footerSpacer {
    height: 200px;
}
footer {
    position: absolute;
    bottom: 0;
    height: 200px;
}

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.

酒绊 2024-12-05 02:35:44

如果页脚只是直接到底部,您是否需要页脚 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 do position:absolute;? Either that, or lower the z-index of the footer so it goes behind the content.

甲如呢乙后呢 2024-12-05 02:35:44

你能不能在主体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;

£烟消云散 2024-12-05 02:35:44

图像被停靠是因为它有 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 of position: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文