修复了相对父 div 内的定位 div
我目前正在构建一个响应式网站,需要修复一个菜单,因此当网站的其余部分滚动时不会滚动。问题是它是一个流体布局,我希望“固定定位”菜单项相对于包含的父元素而不是浏览器窗口是固定的。无论如何这可以做到吗?
I am currently building a responsive website and need a menu to be fixed, thus not scrolling when the rest of the site scrolls. the issue is that it is a fluid layout and i want the "fixed positioned" menu item to be fixed relative to the containing parent element and not to browser window. is there anyway this can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这个问题首先出现在谷歌上,虽然是一个老问题,所以我发布了我找到的工作答案,这对其他人来说可能有用。
这需要 3 个 div,包括固定 div。
HTML
CSS
This question came first on Google although an old one so I'm posting the working answer I found, which can be of use to someone else.
This requires 3 divs including the fixed div.
HTML
CSS
加文,
你遇到的问题是对定位的误解。如果您希望它相对于父级“固定”,那么您确实希望您的
#fixed
为position:absolute
,这将更新其相对于父级的位置。这个问题完整地描述了定位类型以及如何有效地使用它们。
总之,你的 CSS 应该是
Gavin,
The issue you are having is a misunderstanding of positioning. If you want it to be "fixed" relative to the parent, then you really want your
#fixed
to beposition:absolute
which will update its position relative to the parent.This question fully describes positioning types and how to use them effectively.
In summary, your CSS should be
在父元素上尝试
position:sticky;
。Try
position:sticky;
on the parent element.一个不涉及诉诸 JavaScript 并且不会破坏 CSS 转换的简单解决方案是简单地使用一个非滚动元素,其大小与滚动元素相同,绝对定位在其上。
基本的 HTML 结构是
CSS
HTML
parent-to-position-by
是相对于位置的div
相对于固定的东西。scrolling-contents
将跨越此div
的大小并包含其主要内容fixed-elements
只是一个绝对定位的div< /code> 跨越
scrolling-contents
div
顶部的相同空间。fixed
类对div
进行绝对定位,它可以达到与相对于父div
固定位置相同的效果。 (或滚动内容,因为它们跨越整个空间)这是一个带有工作示例的 js-fiddle
An easy solution that doesn't involve resorting to JavaScript and will not break CSS transforms is to simply have a non-scrolling element, the same size as your scrolling element, absolute-positioned over it.
The basic HTML structure would be
CSS
HTML
parent-to-position-by
would be the relativediv
to position something fixed with respect to.scrolling-contents
would span the size of thisdiv
and contain its main contentsfixed-elements
is just an absolute-positioneddiv
spanning the same space over top of thescrolling-contents
div
.div
with thefixed
class, it achieves the same effect as if it were fixed-positioned with respect to the parentdiv
. (or the scrolling contents, as they span that full space)Here's a js-fiddle with a working example
如果您使用边距而不是位置移动固定的
,这是可能的:
并且此 HTML:
使用此 jsfiddle.
This is possible if you move the fixed
<div>
using margins and not positions:And this HTML:
Play around with this jsfiddle.
您可以做的一个简单的事情是使用 % 值相对于页面的其余部分定位您的固定 DIV。
看看这个 jsfiddle ,其中固定的 DIV 是侧边栏。
下面的简短图片描述了上面的布局:
A simple thing you can do is position your fixed DIV relative to the rest of your page with % values.
Check out this jsfiddle here where the fixed DIV is a sidebar.
And a brief picture below describing the layout above:
这是一个更通用的解决方案,不依赖于菜单/标题高度。
其完全响应式的纯 CSS 解决方案,在 IE8+、Firefox、Chrome、Safari、opera 上运行良好。
支持内容滚动而不影响菜单/标题。
使用 工作小提琴 进行测试
HTML:
CSS:
here is a more generic solution, that don't depends on the Menu/Header height.
its fully responsive, Pure CSS solution, Works great on IE8+, Firefox, Chrome, Safari, opera.
supports Content scrolling without affecting the Menu/Header.
Test it with that Working Fiddle
The Html:
The CSS:
您可以使用绝对定位来修复包装器。
并给内部 div 一个固定位置。
和
div 里面
试试这个它可能对你有用
you can fix the wrapper using absolute positioning.
and the give inside div a fixed position.
and
div inside that
try this It might work for you
样品溶液。检查一下,这是否是您所需要的。
对于 CSS,
请在 codepen https://codepen.io/FelySpring/pen/jXENXY 上查看
Sample solution. Check, if this is what you need.
And for CSS
See it on codepen https://codepen.io/FelySpring/pen/jXENXY
只是将位置从固定更改为粘性
justy change position from fixed to sticky
如果您希望其中一个 div 看起来是固定的,而另一个同级 div 可滚动到父 div(如侧面导航栏和内容区域),这里是实现它的一种方法。
If you would like to have one of the div appear to be fixed while the other sibling div to be scrollable agains parent div(like side navigation bar and content area), here is one way to achieve it.