如何消除与位置的差距:相对横幅
有什么推荐和推荐?摆脱position:relative造成的差距的最优雅的方法?
我有一个首页,想要放置一个横幅,该横幅将部分位于标题和内容部分上方,但使用位置:相对会产生一个空白区域...
参见示例(我希望文本位于红色的正下方框):
我知道我可以创建另一个亲戚将 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):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
获取灰色框中的全部内容,并将其放置在 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]
这不是你要求的,而是我会这样做的两种方法之一:
这里的主要想法是使横幅成为绝对定位的 div(不是绝对定位在页面上,尽管你也可以这样做;它事实上可能会更好)。
首先将#content 区域设置为position:relative,但不要更改其他任何内容。这将创建一个新的堆叠上下文(子元素使用上/右/下/左以及相对于它的百分比)。
然后将banner-thing作为#content区域的子元素,设置如下:
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:
在这种情况下,负边距将是一个更优雅的解决方案(修订版 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.
我终于解决了这个问题,很简单:
神奇! :)
I've finally resolved this issue, simple:
Does the magic! :)