CSS 位置:固定在定位元素内
我有一个定位的 div,其内容可能太长,因此会出现滚动条(overflow:auto
设置)。它充当 ajax 应用程序中的对话框。我想修复右上角的关闭按钮,这样当用户滚动 div 时它就不会滚动走。
我尝试使用 position:fixed;右:0; top:0
但它将按钮放置在页面的右上角,而不是放在 div 中(在 firefox 中)。
是否可以仅使用 CSS 来完成此按钮放置,而无需在每个滚动事件上使用 js 中的 offsetWidth/Height 进行黑客攻击?
ps:div的高度和宽度不是固定值,它取决于内容的大小和浏览器窗口的大小。用户还可以根据需要调整其大小。
I have a positioned div whose content can be too long so scrollbars appear (overflow:auto
set). It functions as a dialog box in an ajax app. I want to fix a close button on it's right top corner so when the user scrolls the div it won't scroll away.
I tryed it with position:fixed; right:0; top:0
but it placed the button on the right top of the page not in the div (in firefox).
Is it possible to do this button placement using CSS only without hacking with the offsetWidth/Height in js on every scroll event?
ps: the div's height and width is not a fixed value it depends on the content's size and the browser window's size. User can also resize it if he want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以使用
position:fixed;
,但不设置left
和top
。然后,您将使用margin-left
将其推到右侧,将其放置在您希望的正确位置。在此处查看演示:http://jsbin.com/icili5
You can use the
position:fixed;
, but without setleft
andtop
. Then you will push it to the right usingmargin-left
, to position it in the right position you wish.Check a demo here: http://jsbin.com/icili5
当前选择的解决方案似乎误解了问题。
诀窍是既不要使用绝对定位也不要使用固定定位。相反,将关闭按钮放在 div 外部,将其位置设置为相对,并设置左浮动,使其紧邻 div 右侧。接下来,设置负左边距和正 z 索引,使其出现在 div 上方。
这是一个例子:
The current selected solution appears to have misunderstood the problem.
The trick is to neither use absolute nor fixed positioning. Instead, have the close button outside of the div with its position set to relative and a left float so that it is immediately right of the div. Next, set a negative left margin and a positive z index so that it appears above the div.
Here's an example:
我知道这是一篇旧帖子,但我有同样的问题,但没有找到将元素相对于父
div
设置为固定的答案。 medium.com 上的滚动条是一个很棒的纯 CSS 解决方案,用于设置position:fixed; 相对于父元素而不是视口(有点*)。这是通过将父
div
设置为position:relative;
并使用position:absolute;
按钮包装来实现的,当然按钮是位置:固定;
如下:工作示例
*由于固定元素不不随页面滚动,垂直位置仍将相对于视口,但水平位置相对于此解决方案的父级。
I know this is an old post but I had the same question but didn't find an answer that set the element fixed relative to a parent
div
. The scroll bar on medium.com is a great pure CSS solution for setting somethingposition: fixed;
relative to a parent element instead of the viewport (kinda*). It is achieved by setting the parentdiv
toposition: relative;
and having a button wrapper withposition: absolute;
and the button of course isposition: fixed;
as follows:working example
*Since fixed elements don't scroll with the page the vertical position will still be relative to the viewport but the horizontal position is relative to the parent with this solution.
Position:fixed
给出浏览器窗口的绝对位置。所以它当然会去那里。虽然
position:absolute
引用父元素,所以如果您将按钮放置在容器的
div>
内,它应该位于您想要的位置。像编辑这样的东西
:感谢@Sotiris,他有一个观点,可以使用position:fixed和margin-left来实现解决方案。像这样: http://jsfiddle.net/NeK4k/
Position:fixed
gives an absolute position regarding the BROWSER window. so of course it goes there.While
position:absolute
refers to the parent element, so if you place your<div>
button inside the <div>
of the container, it should position where you meant it to be.Something like
EDIT: thanks to @Sotiris, who has a point, solution can be achieved using a position:fixed and a margin-left. Like this: http://jsfiddle.net/NeK4k/
似乎,CSS变换可以使用
“'transform'属性在元素上建立一个新的本地坐标系”,
但是......这不是跨浏览器的,似乎只有Opera可以正常工作
Seems, css transforms can be used
"‘transform’ property establishes a new local coordinate system at the element",
but ... this is not cross-browser, seems only Opera works correctly
尝试在要固定的元素的父 div 上使用 position:sticky 。
Try position:sticky on parent div of the element you want to be fixed.
我实现了一个具有固定位置(wiewport)但相对于其父级宽度的元素。
我只需要包装我的固定元素并给父元素一个 100% 的宽度。同时,包裹的固定元素和父元素都在一个宽度根据页面而变化的div中,包含网站的内容。通过这种方法,我可以让固定元素始终与内容保持相同的距离,具体取决于该元素的宽度。在我的例子中,这是一个“到顶部”按钮,始终显示在距离底部 15 像素和距离内容右侧 15 像素的位置。
https://codepen.io/rafaqf/pen/MNqWKB
I achieved to have an element with a fixed position (wiewport) but relative to the width of its parent.
I just had to wrap my fixed element and give the parent a width 100%. At the same time, the wrapped fixed element and the parent are in a div which width changes depending on the page, containing the content of the website. With this approach I can have the fixed element always at the same distance of the content, depending on the width of this one. In my case this was a 'to top' button, always showing at 15px from the bottom and 15px right from the content.
https://codepen.io/rafaqf/pen/MNqWKB
如果您的关闭按钮将是文本,这对我来说非常有效:
那么您的
HTML
可以是:If your close button is going to be text, this works very well for me:
Then your
HTML
can just be:位置固定但相对于parent-div
position fixed but relative to parent-div
不确定它是否与其他解决方案不同,但这就是我理解没有任何顶部、左侧等的固定位置的方式:
您使用包装器将元素放置在您想要的任何位置,然后在元素本身上,您只需使用“固定” “ 位置。这将“冻结”元素。
仅当您确保主体上没有全局滚动条时,这才有效。
所以这应该适用于问题的用例。
Not sure if it's different from the other solutions, but this is how I understand the fixed position without any top, left, etc:
You place your element wherever you want using a wrapper, and then on the element itself, you just use "fixed" position. This will "freeze" the element.
This is only working if you make sure you don't have a global scrollbar on the body.
So this should work for the use case of the question.