gpt4 book ai didi

css - 静态定位和相对定位的区别

转载 作者:数据小太阳 更新时间:2023-10-29 09:07:43 26 4
gpt4 key购买 nike

在 CSS 中,静态(默认)定位和相对定位有什么区别?

最佳答案

静态定位是元素的默认定位模型。它们显示在作为正常 HTML 流的一部分呈现的页面中。静态定位元素不遵守 lefttoprightbottom 规则:

statically-positioned elements obey normal HTML flow.

相对定位允许您指定相对于元素在 HTML 流中的正常位置的特定偏移量(lefttop 等)。因此,如果我在 div 中有一个文本框,我可以在文本框上应用相对定位,使其显示在相对于通常放置在 div 中的特定位置:

relatively-positioned elements obey HTML flow, but provide the ability to adjust their position relative to their normal position in HTML flow.

还有绝对定位——您可以指定元素相对于整个文档的确切位置,或元素树上的下一个相对定位元素:

absolutely-positioned elements are taken out of HTML flow and can be positioned at a specific place in the document...

position: relative 应用于层次结构中的父元素时:

...or positioned relative to the first parent element in the HTML tree that is relatively positioned.

请注意我们的绝对定位元素是如何被相对定位元素绑定(bind)的。

最后是固定的。固定定位将元素限制在视口(viewport)中的特定位置,该位置在滚动期间保持原位:

fixed-positioned elements are also taken out of HTML flow, but are not bound by the viewport and will not scroll with the page.

您可能还会观察到固定位置元素不会导致滚动的行为,因为它们不被视为受视口(viewport)限制:

fixed-positioned elements have no effect on scroll.

而绝对定位的元素仍然受视口(viewport)限制,并会导致滚动:

absolutely-positioned elements are still affected by the boundaries of the viewport, unless overflow is used on a parent element.

..当然除非您的父元素使用 overflow: ? 来确定滚动的行为(如果有的话)。

通过绝对定位和固定定位,元素脱离了 HTML 流。

关于css - 静态定位和相对定位的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5011211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com