CSS Portal

CSS top Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The top property controls the vertical offset of an element when that element participates in positioning. It does not itself change how the element is laid out in normal flow; instead it tells the browser how far to move a positioned element along the y‑axis relative to the reference point defined by its positioning context. Because it only applies to elements that are positioned, its effects depend entirely on how that element’s positioning is established via position.

How the offset created by top is interpreted differs by positioning scheme. For an element whose position is the relative type, changing top shifts the element visually away from its normal location without altering the space it originally occupied. For absolutely or fixed positioned elements, top is measured from the relevant containing block (typically the padding edge of the nearest positioned ancestor) and determines the element’s placement within that context. For sticky positioning, top defines the threshold at which the element becomes fixed to the viewport as the user scrolls. Related horizontal offset properties like left, right and the vertical counterpart bottom work the same way conceptually along their respective axes.

The top offset interacts with other layout concerns in ways that are important to anticipate. Offsetting an element with top does not create margin collapse with adjacent boxes and, for relatively positioned elements, does not affect document flow — other elements behave as if the relatively positioned element were still in its original place. When used with stacking controls, top contributes to the element’s placement but the painting order and overlap are ultimately governed by stacking context rules and z-index. Transformations such as those from transform can also affect the visual relationship between an offset element and its surroundings because transforms create new containing contexts for things like painting and hit testing.

In practical layout work, consider how the containing block is formed and how offsets will behave across different viewport sizes and scroll states. Offsetting an element can be a simple way to nudge visuals into place without changing document flow, but relying heavily on fixed offsets can reduce responsiveness; often a combination of offsets and other layout techniques (flexbox/grid or controlled margin spacing) yields more robust, maintainable results.

Definition

Initial value
auto
Applies to
Positioned elements
Inherited
No
Computed value
If specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, 'auto'.
Animatable
Yes
JavaScript syntax
object.style.top

Interactive Demo

I am absolutely positioned.
The rusty swing set creaked a lonely lullaby in the twilight, shadows lengthening like grasping fingers across the dew-kissed grass. A lone dandelion seed, adrift on the breeze, whispered secrets of faraway fields, of dandelion suns and galaxies spun from fluff.

Syntax

top: auto | <length> | <percentage>

Values

  • <length>Floating-point number, followed by an absolute units designator (cm, mm, in, pt, or pc) or a relative units designator (em, ex, or px).
  • <percentage>Integer, followed by a percent sign (%). The value is a percentage of the height of the parent object.
  • autoDefault position, according to the regular HTML layout of the page.

Example

<body>
<header class="fixed-header">Fixed header (position: fixed; top: 0)</header>

<main class="container">
<div class="box relative-box">Relative box (position: relative; top: 10px)</div>

<div class="box absolute-box">Absolute box (position: absolute; top: 40px)</div>

<div class="spacer">Scroll down to see the sticky example</div>

<div class="sticky-box">Sticky box (position: sticky; top: 10px)</div>
</main>
</body>
/* Styles demonstrating the top property with different positioning */
body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #f7f7fb;
}

.fixed-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 52px;
    background: #2b6cb0;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 6px rgba(0,0,0,0.15);
    z-index: 10;
}

.container {
    margin-top: 72px; /* space for the fixed header */
    padding: 20px;
    height: 360px;
    overflow: auto; /* enables scrolling so sticky can take effect */
    background: #fff;
    border: 1px solid #e2e8f0;
    position: relative; /* positioning context for absolute children */
}

.box {
    padding: 12px 16px;
    background: #edf2ff;
    border: 1px solid #c3dafe;
    border-radius: 6px;
    margin-bottom: 16px;
}

.relative-box {
    position: relative;
    top: 10px; /* moves the element 10px down from its normal position */
}

.absolute-box {
    position: absolute;
    top: 40px; /* 40px from the top edge of the .container (its positioned ancestor) */
    right: 20px;
    background: #fff7ed;
    border-color: #fbd38d;
}

.spacer {
    height: 400px; /* creates space to allow scrolling */
}

.sticky-box {
    position: -webkit-sticky; /* Safari support */
    position: sticky;
    top: 10px; /* when the element would scroll past 10px from the container's top, it sticks */
    background: #e6fffa;
    border-color: #9ae6b4;
}

Browser Support

The following information will show you the current browser support for the CSS top property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!