如何消除与位置的差距:相对横幅

发布于 2024-11-01 19:30:24 字数 343 浏览 1 评论 0原文

有什么推荐和推荐?摆脱position:relative造成的差距的最优雅的方法?

我有一个首页,想要放置一个横幅,该横幅将部分位于标题和内容部分上方,但使用位置:相对会产生一个空白区域...

参见示例(我希望文本位于红色的正下方框):

http://jsfiddle.net/Ru2CT/

我知道我可以创建另一个亲戚将 div 定位为我的文本的父级,但随后我仍然会有间隙,但内容部分和内容部分之间仍然存在间隙。页脚...

有什么想法吗? :)

What's the recommended & most elegant way of getting rid of the gap caused by position:relative?

I have a front page and want to put a banner that will be partially above the header and content section, but using position:relative produces an empty area...

See example (I want the text to be just below the red box):

http://jsfiddle.net/Ru2CT/

I know I could create another relative positioned div as a parent of my text, but then I'll still have the gap but between content section & footer...

Any ideas? :)

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

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

发布评论

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

评论(4

雨巷深深 2024-11-08 19:30:24

获取灰色框中的全部内容,并将其放置在 div 中(拉伸到相同大小)。然后使用position:relative 向上移动该框。这将具有使用红色“滑块”/横幅事物移动文本的效果,而不移动灰色背景。

开始了:
http://jsfiddle.net/4BLFJ/ [动画和注释]

Take the entire contents of the grey box, and place it within a div (stretched to be the same size). Then move that box up with position:relative. This will have the effect of moving the text with the red "slider"/banner thing, without moving the gray background.

Here we go:
http://jsfiddle.net/4BLFJ/ [animated and annotated]

风吹雨成花 2024-11-08 19:30:24

这不是你要求的,而是我会这样做的两种方法之一:

这里的主要想法是使横幅成为绝对定位的 div(不是绝对定位在页面上,尽管你也可以这样做;它事实上可能会更好)。

首先将#content 区域设置为position:relative,但不要更改其他任何内容。这将创建一个新的堆叠上下文(子元素使用上/右/下/左以及相对于它的百分比)。

然后将banner-thing作为#content区域的子元素,设置如下:

position:absolute;
width:80%; height:100px; /*there are other ways to set the height and width*/
bottom:100%; /*this puts it at the top*/
/*you can also use bottom:105% or bottom:90% or other things, or if you really
  want to use non-relative units like px, you can create a third nested div that is
  relatively positioned by whatever px amount*/

This is not what you asked, but is one of the two ways I would do it:

The main idea here is to make the banner an absolutely-positioned div (not absolutely-positioned on the page, though you can do that too; it may in fact be better).

First set the #content area to be position:relative, but NOT change anything else. This creates a new stacking context (child elements use top/right/bottom/left and percentages relative to it).

Then put the banner-thing as a child element of the #content area, and set it as follows:

position:absolute;
width:80%; height:100px; /*there are other ways to set the height and width*/
bottom:100%; /*this puts it at the top*/
/*you can also use bottom:105% or bottom:90% or other things, or if you really
  want to use non-relative units like px, you can create a third nested div that is
  relatively positioned by whatever px amount*/
戒ㄋ 2024-11-08 19:30:24

在这种情况下,负边距将是一个更优雅的解决方案(修订版 jsFiddle)。请注意,我必须将 #eee 背景移至 div#main,否则它会覆盖在 div#top 的背景上。

作为一般经验法则,我还建议避免相对定位,除非绝对必要 - 这通常会导致旧版本 IE 中的 z-index 问题。

Negative margin would be a much more elegant solution in this situation (revised jsFiddle). Note that I've had to move the #eee background to div#main, as it would otherwise overlay on the background of div#top.

As a general rule of thumb, I'd also recommend avoiding relative positioning unless absolutely necessary - can often lead to z-index headaches in older versions of IE.

感悟人生的甜 2024-11-08 19:30:24

我终于解决了这个问题,很简单:

   position: relative;
   bottom: 200px;
   margin-bottom: -200px;

神奇! :)

I've finally resolved this issue, simple:

   position: relative;
   bottom: 200px;
   margin-bottom: -200px;

Does the magic! :)

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