The difference between static and relative positioning

In CSS, what is the difference between static (default) positioning and relative positioning?

+61
css static css-position positioning
Feb 16 2018-11-11T00:
source share
7 answers

Static positioning is the default positioning model for elements. They appear on the page where they appear as part of a normal HTML stream. Statically positioned elements do not obey the rules left , top , right and bottom :

statically-positioned elements obey normal HTML flow.

Relative positioning allows you to specify a specific offset ( left , top , etc.) that refers to the normal position of the element in the HTML stream. Therefore, if I have a text box inside a div , I could apply relative positioning in the text box so that it appears in a specific place relative to where it usually fits in the div :

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

There is also absolute positioning - in this case you indicate the exact location of the element relative to the entire document, or the next relatively positioned element further down the tree of elements :

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

And when a position: relative is applied to the parent in the hierarchy:

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

Please note that our position element is absolutely associated with a relatively positioning element.

And finally fixed. Fixed positioning restricts an element to a certain position in the viewport, which remains in place during scrolling:

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

You can also observe that elements with fixed positions do not cause scrolling, since they are not considered to be associated with the viewport:

fixed-positioned elements have no effect on scroll.

While absolutely positioned elements are still connected by the viewport and will cause scrolling:

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

.. unless, of course, your parent uses overflow: ? to determine the scroll behavior (if any).

With absolute positioning and fixed positioning, elements are inferred from the HTML stream.

+119
Feb 16 2018-11-11T00:
source share

The position position allows you to use the top / bottom / left / right position for positioning. Static will not allow you to do this if you are not using margin options. There is a difference between the top and top.

You do not need to use static a lot since it is the default

+5
Feb 16 2018-11-11T00:
source share

You can see a simple overview here: W3School

In addition, if I correctly recall that when declaring a relative element, by default it will remain in the same place as otherwise, but you will get the opportunity to completely position the elements inside it relative to this element, which I was very useful in the past.

+4
Feb 16 2018-11-11T00:
source share

Relative position relative to normal flow. The relative position of this element (with offsets) refers to the position at which this element would be normal if it had not been moved.

+2
Sep 07
source share

Matthew Abbott has a really good answer.

Absolute and relative positioned elements obey the top , left , right and bottom commands (offsets), where the static positioned elements do not match.

Relatively positioned elements move offsets from where they are usually in html.

Absolutely positioned elements move offsets from the document or the next relatively located element up the DOM tree.

+2
Sep 08 '15 at 20:33
source share

In response to "why CSS will still execute the position: static;" in one scenerio, using position: relative for the parent and position: absolute for the child limits the width of the zoom for the child. In a horizontal menu system where you may have “columns” of links, using “width: auto” does not work with relative parents. In this case, changing it to "static" will allow you to change the width depending on the contents inside.

I spent several hours figuring out why I can’t configure my container based on the amount of content inside it. Hope this helps!

+2
Oct 14 '16 at 14:24
source share

Static: The STATIC element that we get has the value DEFAULT (Normal Object Positioning).

Relative: Relative to its current position, but can be moved. Or the RELATIVE positioned item is positioned relative to the ITSELF.

0
May 25 '16 at 16:55
source share



All Articles