Javatpoint Logo
Javatpoint Logo

CSS top property

This top property in CSS is used to specify the top position for the vertical positioned elements. It does not affect the non-positioned elements. It is one of the four offset properties that are left, right, and bottom.

The effect of this property depends on how the corresponding element is positioned i.e., the value of the position property. The top property has no effect when the position property is set to the value static.

The effects of this property on positioned elements other than the value static are listed as follows:

  • When the element is absolutely or fixed positioned (i.e., position: absolute; and position: fixed;), the top property specifies the distance between the element's top edge and the top edge of its containing block (ancestor to which the element is relatively positioned).
  • If the element is relatively positioned (i.e., position: relative;), the top property moves the element's top edge to above/below from its normal position.
  • If the position is set to sticky (i.e., position: sticky;) then, the positioning context is the viewport. When the element is inside the viewport, the top property behaves like its position is relative. When the element is outside, then the top property behaves like its position is fixed.


Property Values

The values of this property are defined as follows:

auto: This is the default value. It allows the browser to calculate the top edge position.

length: This value defines the position of top property in px, cm, pt, etc. It allows negative values.

percentage: This value defines the position of top property in percentage (%). It is calculated with respect to the height of the element's containing block. It also allows negative values.

initial: It sets the property to its default value.

inherit: It inherits the property from its parent element.

Now, let's understand this property by using some examples.

Example - Using negative values

In this example, there are four relatively positioned div elements. We are applying the top property to them with negative values. Here, the negative length values of the top property are defined in px and em.

Test it Now


CSS top property


In this example, there are four absolutely positioned (i.e., position: absolute;) div elements. We are applying top property to them. The div elements with top: initial; and top: auto; will overlap because of having similar dimensions and default values. Here, the length of the top property is defined in px and %.

Test it Now


CSS top property

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA