position property
Relative positioning is similar to static positioning, but the placement of relative positioned elements can be modified with the left, top, right and bottom style properties. In this case, the values of the left, top, right and bottom style properties are relative to the element's normal flow (static) position.
There are two other positioning methods that are often used, the absolute and the fixed.
The left, top, right and bottom style properties affect both absolute and fixed positioned elements similarly to relative positioned elements,
but the placement of references are different.
The placement of fixed positioned elements is always relative to the browser window's client area, so scrolling the document or an element in the document does not modify the placement of fixed positioned elements.
The placement of an absolute positioned element is relative to its first positioned (relative, fixed or absolute) ancestor element. If no positioned parent element exists, the placement is relative to the document.
In contrast to fixed positioning, scrolling affects absolute positioning.
JavaScript page for this property: position. You can find other example(s) there. |
Possible values:
The element is positioned relative to its first positioned (relative, fixed or absolute) ancestor element. | ||||||||||||
|
The element is positioned relative to the browser window's client area. | |||||||||||
Takes the value of this property from the computed style of the parent element. | ||||||||||||
The element is offset relative to its normal flow (static) position. | ||||||||||||
Default. Elements are rendered in order, as they appear in the document flow. |
Internet Explorer does not support fixed position before version 7.
In Internet Explorer 7, fixed position is only supported in STRICT and XHTML document type declarations.
From Internet Explorer 8, fixed position is supported in all document type declarations.
For a detailed description of document types, see the page for the !DOCTYPE element.
Example HTML code 1:
|
||||
<head> <style> .lefttop { position: absolute; left: 20px; top: 20px; width: 30px; height: 30px; background-color: red; } .rightbottom { position: absolute; right: 20px; bottom: 20px; width: 30px; height: 30px; background-color: blue; } </style> </head> <body> <div class="lefttop"></div> <div class="rightbottom"></div> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
|
||||
<head> <style> .lefttop { position: fixed; left: 20px; top: 20px; width: 30px; height: 30px; background-color: red; } .rightbottom { position: fixed; right: 20px; bottom: 20px; width: 30px; height: 30px; background-color: blue; } </style> </head> <body> <div class="lefttop"></div> <div class="rightbottom"></div> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> Try to scroll and resize the window!<br /><br /><br /><br /><br /> </body> |
||||
|
||||
Did you find this example helpful?
|
The headerDiv in the following code is centered and fixed, but positioned to the right if the zoom is increased to 125%:
The solution is to fixate it differently: