top property
Sets or retrieves the position of the object relative to the top of the next positioned object in the document hierarchy.
![]() |
Syntax
top:
<length>
|
<percentage>
| auto
Property values
A Variant that specifies or receives one of the following values:
auto-
Default. Default position according to the regular HTML layout of the document.
- length
-
Floating-point number, followed by an absolute units designator (
cm,mm,in,pt, orpc) or a relative units designator (em,ex, orpx). For more information about the supported length units, see CSS Values and Units Reference. - percentage
-
Integer, followed by a percent sign (%). This value is a percentage of the height of the parent object.
CSS information
| Applies To | All elements |
|---|---|
| Media | visual |
| Inherited | no |
| Initial Value | auto |
Standards information
- CSS 2.1, Section 9.3.2
Remarks
The top attribute should be used only when the position attribute is set; otherwise, the value of the top attribute is ignored.
Because the value of the top property is a string, you cannot use the property in script to calculate the position of the object in the document; instead, use the pixelTop or the posTop property.
For more information about how to access the dimension and location of objects on the document through the Dynamic HTML (DHTML) Document Object Model (DOM), see Measuring Element Dimension and Location with CSSOM in Internet Explorer 9.
Examples
The following example uses the top property to change the position of the green div. In the example below, there are four div elements with absolute positioning. The green div is positioned 50 pixels from the top.
Code example: http://samples.msdn.microsoft.com/workshop/samples/css/positioning/positioning.html
#green {
height: 175px;
width: 175px;
background-color: #7FBA00;
position: absolute;
top: 50px;
}
This example uses inline script to change the position of the image set by an inline style. The change occurs during onmouseover and onmouseout events.
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/top_s.htm
<img src="cone.jpg" style="position:absolute; top:80px;" onmouseover="this.style.top='100px''" onmouseout="this.style.top='80px'">
See also
