我可以定位相对于父母固定的元素吗? [英] Can I position an element fixed relative to parent?

查看:104
本文介绍了我可以定位相对于父母固定的元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,当我定位一个元素固定的时候,如果父对象是相对定位的,它是否相对于窗口位置固定?

I find that when I position an element fixed, it doesn't matter if the parent is positioned relative or not, it will position fixed, relative to the window?

CSS

#wrapper { width: 300px; background: orange; margin: 0 auto; position: relative; }
#feedback { position: fixed; right: 0; top: 120px; }

HTML

<div id="wrapper">
    ...
    <a id="feedback" href="#">Feedback</a>
</div>

http://jsbin.com/ibesa3

推荐答案

让我提供两个可能的问题的答案。请注意,您现有的标题(和原始帖子)提出的问题与您在编辑和后续评论中寻求的问题不同。

Let me provide answers to both possible questions. Note that your existing title (and original post) ask a question different than what you seek in your edit and subsequent comment.

要将相对于父元素的元素定位为固定,您需要在子元素上使用 position:absolute ,而不是您的父元素的默认值或静态值。

To position an element "fixed" relative to a parent element, you want position:absolute on the child element, and any position mode other than the default or static on your parent element.

例如:

#parentDiv { position:relative; }
#childDiv { position:absolute; left:50px; top:20px; }

这将定位 childDiv 元素50像素相对于parentDiv的位置,向左和向下移动20个像素。

This will position childDiv element 50 pixels left and 20 pixels down relative to parentDiv's position.

相对于窗口定位元素固定 c $ c>,,可以使用 right: bottom:按照您的看法定位。

To position an element "fixed" relative to the window, you want position:fixed, and can use top:, left:, right:, and bottom: to position as you see fit.

例如:

#yourDiv { position:fixed; bottom:40px; right:40px; }

这将定位 yourDiv 固定相对于网页浏览器窗口,底边40像素,右边缘40像素。

This will position yourDiv fixed relative to the web browser window, 40 pixels from the bottom edge and 40 pixels from the right edge.

这篇关于我可以定位相对于父母固定的元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆