静态定位和相对定位的区别
在CSS中,静态(默认)定位和相对定位有什么区别?
In CSS, what is the difference between static (default) positioning and relative positioning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在CSS中,静态(默认)定位和相对定位有什么区别?
In CSS, what is the difference between static (default) positioning and relative positioning?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
静态定位是元素的默认定位模型。它们显示在页面中,并作为正常 HTML 流的一部分呈现。静态定位元素不遵守
left
、top
、right
和bottom
规则:相对定位允许您指定特定的偏移量(
left
、top
等),它相对于 HTML 流中元素的正常位置。因此,如果我在div
内有一个文本框,我可以在文本框上应用相对定位,使其显示在相对于它通常放置在div
内的位置的特定位置:>也是绝对定位 - 您可以指定元素相对于整个文档的确切位置,或元素树上的下一个相对定位的元素
: sstatic.net/fHUPW.gif" alt="绝对定位元素从 HTML 流中取出,可以定位在文档中的特定位置...">
当
position:relative
应用于层次结构中的父元素:请注意我们的绝对位置元素是如何与相对定位元素绑定的。
最后是固定的。固定定位将元素限制在视口中的特定位置,该位置在滚动期间保持在原位:
您还可能会观察到固定位置元素不会导致滚动的行为,因为它们不被视为受视口约束:
而绝对定位元素仍然受视口约束并会导致滚动:
..当然除非您的父元素使用
overflow: ?
来确定滚动的行为(如果有)。通过绝对定位和固定定位,元素被从 HTML 流中取出。
Static positioning is the default positioning model for elements. They are displayed in the page where they rendered as part of normal HTML flow. Statically positioned elements don't obey
left
,top
,right
andbottom
rules:Relative positioning allows you to specify a specific offset (
left
,top
etc) which is relative to the element's normal position in HTML flow. So if I have a textbox inside adiv
I could apply relative positioning on the textbox to have it display at specific place relative to where it would normally be placed within thediv
:There is also absolute positioning - whereby you specify the exact location of the element relative to the entire document, or the next relatively positioned element further up the element tree:
And when a
position: relative
is applied to a parent element in the hierarchy:Note how our absolutely-position element is bound by the relatively-positioned element.
And lastly there is fixed. Fixed positioning restricts an element to a specific position in the viewport, which stays in place during scroll:
You may also observe the behaviour that fixed-positioned elements do not cause scroll because they are not considered to be bound by the viewport:
Whereas absolutely-positioned elements are still bound by the viewport and will cause scrolling:
..unless of course your parent element uses
overflow: ?
to determine the behaviour of the scroll (if any).With absolute positioning and fixed positioning, the elements are taken out of HTML flow.
回答“为什么CSS仍然会实现position: static;”在一种场景中,对父级使用position:relative,对子级使用position:absolute,会限制子级的缩放宽度。在水平菜单系统中,您可以拥有链接的“列”,使用“width:auto”不适用于相对父级。在这种情况下,将其更改为“静态”将允许宽度根据其中的内容而变化。
我花了好几个小时想知道为什么我无法让我的容器根据其中的内容量进行调整。希望这有帮助!
In answer to "why CSS would still implement position: static;" in one scenerio, using position:relative for a parent and position:absolute for the child limits the scaling width of the child. In a horizontal menu system, where you could have 'columns' of links, using 'width:auto' does not work with relative parents. In this case, changing it to 'static' will allow the width to be variable dependent on the content within.
I spent a good few hours wondering why I couldn't get my container to adjust based on the amount of content within it. Hope this helps!
您可以在这里看到一个简单的概述: W3School
另外,如果我没记错的话,在声明相对于元素,默认情况下它会保持在原来的位置,但您可以获得相对于该元素绝对定位其中元素的能力,我在过去发现这非常有用。
You can see a simple overview here: W3School
Also, if I recall correctly, when declaring an element relative, it will by default stay in the same place as it otherwise should, but you gain the ability to absolutely position elements inside it relatively to this element, which I've found very useful in the past.
相对位置允许您使用上/下/左/右进行定位。静态不会让你这样做,除非你使用边距参数。 Top 和 margin-top 之间是有区别的。
您不需要过多使用 static,因为它是默认的
Position relative lets you use top/bottom/left/right for positioning. Static won't let you do this unless you use margin parameters. There's a difference between Top and margin-top.
You won't need to use static much as it's default
相对位置是相对于正常流动的。该元素的相对位置(带有偏移)是相对于该元素在不移动时通常所处的位置的。
Relative position is relative to the normal flow. The relative position of that element (with offsets) is relative to the position where that element would have been normally if not moved.
Matthew Abbott 有一个非常好的答案。
绝对和相对定位的项目遵循静态定位项目的
top
、left
、right
和bottom
命令(偏移量)不是。相对定位的项目会从它们通常在 html 中的位置移动偏移量。
绝对定位的项目从文档或 DOM 树上的下一个相对定位的元素移动偏移量。
Matthew Abbott has a really good answer.
Absolute and relative positioned items obey
top
,left
,right
andbottom
commands (offsets) where static positioned items do not.Relatively positioned items move offsets from where they would normally be in the html.
Absolute positioned items move offsets from the document or the next relatively positioned element up the DOM tree.
静态:默认情况下元素的位置是静态的。如果您添加顶部、底部、右侧或左侧等属性,则不会执行任何操作。
相对:位置的变化将与该 div 的原始位置相关。
绝对:它相对于其最近定位的祖先(如果有)进行定位;否则,它是相对于初始包含块放置的。来源:MDN
固定:即使我们滚动页面,固定属性也会保持在同一位置。该位置始终相对于包含块。
Static: By default the position of elements is static. If you add property such as top, bottom, right, or left nothing will be implemented.
Relative: The change in position will be relevant to that div's original place.
Absolute: It is positioned relative to its closest positioned ancestor, if any; otherwise, it is placed relative to the initial containing block. Source:MDN
Fixed: The fixed property would stay at the same place even after we scroll the page. The position is relative to the containing block always.