XML technologies / CSS / Advanced websites with CSS / CSS property reference / position

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

The property position describes how an element is positioned on the page.

Inherited: no

See also: bottom, left, right, top, z-index

Values

The property is declared with the following constants:

  • absolute: This setting positions an element at a specified location within its parent element. For example, top: 0 and left: 0 place the element in the top-left corner of its nearest ancestor whose property position has a value other than static. If there is none, it is positioned directly within the element body. Elements positioned with absolute are removed from an HTML page’s document structure and from the positioning context for other elements on the page.

  • fixed: This setting positions an element in the same way as absolute. When the page is scrolled, however, the element keeps its position in the browser window rather than moving with the rest of the page.

  • relative: This setting positions an element relative to where it would appear with the setting static. For example, top: 30px and left: 30px place the element 30 pixels below and 30 pixels to the left of its unpositioned location. Elements positioned with relative remain part of the positioning context for elements positioned with static or absolute. If elements positioned with absolute are aligned with elements positioned with relative, they too remain within the page’s positioning context.

  • static: This setting positions an element according to the normal document structure. The properties bottom, left, right and top have no effect.

Default value: static

Compatibility

CSS version: 2

Works in all CSS-capable browsers. However, the setting fixed is implemented correctly only by Mozilla-based browsers (including Netscape version 6 and later), Opera 7 and Internet Explorer 5 for Macintosh. Internet Explorer for Windows (through version 6) interprets fixed in the same way as absolute.

Example

The following style rule positions the element with ID logo 30 pixels from the top and right edges of the browser window. In the positioning context, the element is directly inside the element body. It remains fixed in the browser window even when the user scrolls the page:

#logo {
 position: fixed;
 top: 30px;
 right: 30px;
}

  

 

 

 

back to top