The Wayback Machine - https://web.archive.org/web/20261015173355/https://www.geeksforgeeks.org/css/css-top-property/

CSS Top Property

Last Updated : 13 Oct, 2026

The CSS top property specifies the distance between the top edge of a positioned element and its reference position.

Syntax:

selector {
position: relative;
top: value;
}

Property Values of top Property

  • Length: Sets a fixed offset, e.g. top: 20px;
  • Percentage: Sets an offset relative to the containing block's height, e.g., top: 50%;
  • Auto: Lets the browser determine the offset, e.g., top: auto;
  • Initial: Resets the value to auto.
  • Inherit: Uses the parent's computed top value.
  • Unset: Resets to auto because top is not inhertited by default.
  • Negatice values: Moves a positioned element upward, e.g., top: -10px;

Position Values of top Property

  • Static: top has no effect
  • Relative: Offsets the element from its normal position.
  • Absolute: Positions the element relative to its nearest positioned ancestor.
  • Fixed: Positions the element relative to the viewport.
  • Sticky: Makes the element stick to a specified top offset while scrolling, within its container.

Example: Different Values of top property

The following example demonstrates the top property values. Each box shows how the value affects the position of a positioned element.

HTML
<!--Driver Code Starts-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS top Property Values</title>

<!--Driver Code Ends-->

    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f1f5f9;
            padding: 20px;
        }

        .container {
            max-width: 600px;
            margin: auto;
        }

        .parent {
            position: relative;
            height: 100px;
            margin-bottom: 15px;
            background: white;
            border: 1px dashed #94a3b8;
            border-radius: 8px;
        }

        .box {
            position: absolute;
            left: 10px;
            padding: 10px;
            color: white;
            background: #2563eb;
            border-radius: 5px;
        }

        /* Moves the element 20px down */
        .length {
            top: 20px;
        }

        /* Places the element 50% down the parent */
        .percentage {
            top: 50%;
        }

        /* Uses the browser's automatic positioning rules */
        .auto {
            top: auto;
            bottom: 0;
        }

        /* Resets top to its initial value: auto */
        .initial {
            top: initial;
            bottom: 0;
        }

        /* Uses the parent's computed top value */
        .inherit {
            top: inherit;
        }

        /* Resets top to auto because it is not inherited */
        .unset {
            top: unset;
            bottom: 0;
        }

        /* Moves the element 10px above the parent's top edge */
        .negative {
            top: -10px;
        }

        /* Gives the parent a top value for inheritance */
        .inherit-parent {
            top: 25px;
        }
    </style>

<!--Driver Code Starts-->
</head>
<body>

    <div class="container">
        <h1>CSS top Property Values</h1>

        <div class="parent">
            <div class="box length">Length: 20px</div>
        </div>

        <div class="parent">
            <div class="box percentage">Percentage: 50%</div>
        </div>

        <div class="parent">
            <div class="box auto">Auto</div>
        </div>

        <div class="parent">
            <div class="box initial">Initial</div>
        </div>

        <div class="parent inherit-parent">
            <div class="box inherit">Inherit</div>
        </div>

        <div class="parent">
            <div class="box unset">Unset</div>
        </div>

        <div class="parent">
            <div class="box negative">Negative: -10px</div>
        </div>
    </div>

</body>
</html>
<!--Driver Code Ends-->

Example: Different Position Values of top property

The following example demonstartes how the top property behaves with static, relative, absolute, fixed and sticky positioning.

HTML
<!--Driver Code Starts-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS top and Position Values</title>

<!--Driver Code Ends-->

    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f1f5f9;
            padding: 20px;
            min-height: 1500px;
        }

        .container {
            max-width: 600px;
            margin: auto;
        }

        .box {
            padding: 15px;
            margin-bottom: 20px;
            background: white;
            border-left: 5px solid #2563eb;
            border-radius: 6px;
        }

        /* top has no effect on static elements */
        .static {
            position: static;
            top: 20px;
        }

        /* Moves 20px down from its normal position */
        .relative {
            position: relative;
            top: 20px;
        }

        /* Positions 20px from the top of its positioned parent */
        .parent {
            position: relative;
            height: 110px;
            background: #dbeafe;
            margin-bottom: 20px;
            border-radius: 6px;
        }

        .absolute {
            position: absolute;
            top: 20px;
            left: 15px;
            right: 15px;
            background: #2563eb;
            color: white;
            padding: 12px;
            border-radius: 5px;
        }

        /* Stays 20px from the viewport top while scrolling */
        .fixed {
            position: fixed;
            top: 20px;
            right: 20px;
            background: #16a34a;
            color: white;
            padding: 12px;
            border-radius: 6px;
            z-index: 10;
        }

        /* Sticks 0px from the top while scrolling within its container */
        .sticky {
            position: sticky;
            top: 0;
            background: #7c3aed;
            color: white;
            padding: 12px;
            border-radius: 6px;
            z-index: 5;
        }
    </style>

<!--Driver Code Starts-->
</head>
<body>

    <div class="container">
        <h1>CSS top Property</h1>

        <div class="box static">
            <strong>Static:</strong> top: 20px has no effect.
        </div>

        <div class="box relative">
            <strong>Relative:</strong> Moves 20px downward.
        </div>

        <h2>Absolute Positioning</h2>
        <div class="parent">
            <div class="absolute">
                Absolute: top: 20px
            </div>
        </div>

        <div class="fixed">
            Fixed: top: 20px
        </div>

        <h2>Sticky Positioning</h2>
        <div class="sticky">
            Sticky: top: 0
        </div>

        <p>Scroll down to observe the fixed and sticky elements.</p>
        <p>Additional content to demonstrate scrolling.</p>
        <p style="margin-top: 700px;">You have scrolled down the page.</p>
    </div>

</body>
</html>
<!--Driver Code Ends-->

The CSS top property controls the vertical position of positioned elements. By combining it with values such as lengths, percentages, and auto, developers can adjust element placement and create flexible layouts.

Comment