CSS Reference Property

top 4m1s6

The top property is an offset property that is used to specify the top offset of a positioned element with respect to its positioning context. 3w3768

It is one of four available offset properties that also include left.

Offset properties are used to specify the exact position value other than the default static.

The top property specifies how far an absolutely positioned element’s top margin edge is offset below (or above) the top edge of the element’s positioning context, which is usually its containing block. For relatively positioned elements, the offset is with respect to the top edge of the element itself (i.e., the element is given a position in the normal flow, then offset from that position according to the offset properties). For fixed and sticky elements the positioning context is the viewport.

You should read the position property entry for details on how to choose a position for an element to use the offset properties on it.

Official Syntax 55184y

  • Syntax:

    top: <length> | <percentage> | auto
  • Initial: auto
  • Applies To: positioned elements
  • Animatable: yes, as a length, percentage or calc();

Values 584j31

<length>

The top offset is specified as a fixed length. See the <length> entry for a list of possible length values and units.

Negative values are allowed. A positive value will offset the element downwards from the top edge of its positioning context, while a negative value will offset it upwards with respect to the top edge of the context.

<percentage>

The top offset is specified as a percentage of its containing block’s height. See the <length> entry for a list of possible length values and units.

Negative values are allowed. A positive value will offset the element downwards from the top edge of its positioning context, while a negative value will offset it upwards with respect to the top edge of the context.

auto
When top is set to auto then:

  • If the element is relatively positioned, it will be positioned (vertically) according to the value of the right.)
  • If the element is absolutely positioned, it will be positioned (vertically) according to the value of the height is height: auto, it will get its height based on its content.

Notes z5jo

The top offset property also accepts a value of inherit, which allows the element to inherit its top offset from its parent. The element’s parent may not be its positioning context.

Examples 3e5p5z

The following example offsets an absolutely positioned element by 30px downwards from the top edge of its containing block (which in this case is its positioning context).

.container {
    position: relative; /* establishes a positioning context for its absolutely positioned descendants and for itself */
}

.absolutely-positioned {
    position: absolute;
    top: 30px;
    left: -20px;
}
                

Browser k5t66

The top property is ed in all major browsers: Chrome, Firefox, Safari, Opera, Internet Explorer, and on Android and iOS.

Further Reading 4g1p1d

Written by

Last updated June 3, 2020 at 12:35 pm by Pedro Botelho

Do you have a suggestion, question or want to contribute? Submit an issue.