CSS 背景附件已修复 - 流量不足?

发布于 2024-12-05 16:48:59 字数 577 浏览 1 评论 0原文

我有一些像这样的 HTML:

<div id='container'>

    <div id='main'>

    </div>

</div>

并且 CSS

div#container {
    width:960px;
    margin:0 auto;
}

div#main {
    background: #000000 url('images/bg_placeholder.jpg') no-repeat 85% 100px fixed;
    color:white;
    padding-bottom:30px;
}

#main 与其 #container 具有相同的宽度。我试图在#main 的右上角放置一个固定的背景图像。但是,当我放置背景附件:固定时,它似乎将其从流程中删除...

其中,图像没有放置在主程序的右上角,但似乎位于主程序的右上角页面,或者可能是容器。

在滚动模式下,它在主窗口中位置很好。仅当切换到固定时才有效。

是这样的吗?有办法解决这个问题吗?

I have some HTML like this:

<div id='container'>

    <div id='main'>

    </div>

</div>

And the CSS

div#container {
    width:960px;
    margin:0 auto;
}

div#main {
    background: #000000 url('images/bg_placeholder.jpg') no-repeat 85% 100px fixed;
    color:white;
    padding-bottom:30px;
}

The #main is the same width as its #container. Im trying to put a fixed background image on the top-right side of #main. However, when I put the background-attachment:fixed, it seems to remove it from the flow...

In that, the image, does not get placed in the top-right of main, but seems like the top-right side of the page, or possibly the container.

In scroll mode, it sits great in the main. it's only when a switch to fixed.

Is this how it works? Is there a way around this?

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

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

发布评论

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

评论(2

朦胧时间 2024-12-12 16:48:59

不确定您到底想要实现什么目标,但希望这至少部分回答了您的问题。

我在此处重现了您的情况。如果我理解您想要正确执行的操作,则删除 background-attachment:fixed; 似乎可以解决您的问题。

来自有关 background-attachment 的 Sitepoint 文章

固定值会阻止背景图像随其滚动
包含块。请注意,虽然固定背景图像可能是
应用于整个文档的元素,其背景位置
始终相对于视口放置。这意味着
背景图像仅在其背景位置时可见
与元素的内容、填充或边框区域一致
它被应用。因此,固定的背景图像不会随着
有滚动条的元素——参见溢出——因为它被放置在
与视口的关系。

编辑:这是解决您的问题的可能方法:http://jsfiddle.net/ ep6kQ/3/

编辑编辑:当用户滚动到包含元素下方时,图像消失时出现问题。有人知道如何解决这个问题吗?

Not sure exactly what you're trying to achieve, but hopefully this at least partially answers your question.

I've recreated your situation here. Removing background-attachment:fixed; seems to fix your problem if I understand what you're trying to do correctly.

From the Sitepoint article on background-attachment:

The value fixed stops the background-image from scrolling with its
containing block. Note that although the fixed background-image may be
applied to elements throughout the document, its background-position
is always placed in relation to the viewport. This means the
background-image is only visible when its background-position
coincides with the content, padding, or border area of the element to
which it is applied. Thus, a fixed background-image doesn’t move with
elements that have a scrollbar—see overflow—because it’s placed in
relation to the viewport.

EDIT: Here's a possible way to solve your problem: http://jsfiddle.net/ep6kQ/3/

EDIT EDIT: Having issues getting the image to disappear when the user scrolls below the containing element. Anyone know how to fix this?

稳稳的幸福 2024-12-12 16:48:59

删除固定并在图像上的 CSS 中使用边距属性。这是一个丑陋的修复,但它可能会起作用。也可以尝试使用绝对而不是固定。 (我现在无法测试,安装软件。)呃。

Remove fixed and use your margin properties in CSS on the image. It's an ugly fix but it would probably work. Also might try using absolute instead of fixed. (I can't test at the moment, installing software.) Ugh.

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