CSS top Property
The CSS top property specifies the vertical inset of a positioned element relative to its containing block or normal position.
Its behavior depends on the element's position value. It can move relatively positioned elements, position absolutely or fixed-positioned elements, and establish a threshold for sticky positioning. The property has no effect on statically positioned elements.
top Syntax
The top property accepts the keyword auto, a length, a percentage, or a CSS-wide keyword.
Basic Syntax
.example {
position: absolute;
top: 20px;
}
Available Values
.example {
top: auto;
}
.example {
top: 20px;
}
.example {
top: 2rem;
}
.example {
top: 10%;
}
.example {
top: -15px;
}
.example {
top: calc(50% - 20px);
}
CSS-Wide Keywords
.example {
top: initial;
}
.example {
top: inherit;
}
.example {
top: unset;
}
.example {
top: revert;
}
.example {
top: revert-layer;
}
Invalid Examples
.example {
top: center;
}
.example {
top: middle;
}
.example {
top: 20;
}
.example {
top: 10px 20px;
}
The property accepts one inset value. Nonzero lengths require units, and keywords such as center and middle are not valid.
Property Values
| Value | Description |
|---|---|
auto |
Allows the browser to determine the inset according to the positioning rules. |
<length> |
Specifies a distance using units such as px, rem, em, or vh. |
<percentage> |
Specifies a percentage relative to the height of the containing block. |
| Negative value | Allows an element to be offset in the opposite direction. |
calc() |
Allows calculations combining compatible lengths and percentages. |
initial |
Restores the initial value, auto. |
inherit |
Uses the computed value from the parent element. |
unset |
Restores the initial value because the property is not inherited. |
revert |
Reverts the value according to the CSS cascade. |
revert-layer |
Reverts the value according to the previous cascade layer. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | auto |
| Inherited | No |
| Applies to | Positioned elements |
| Computed value | Absolute length, percentage, or auto |
| Animation type | Length, percentage, or calc() interpolation |
| Percentages | Relative to the containing block's height |
| Initial positioning requirement | A position value other than static |
| Specification | CSS Positioned Layout Module Level 3 |
Note: Although top can be declared on any element, it does not affect layout when position: static is used.
How It Works
The top property behaves differently depending on the element's positioning method.
| Position Value | Behavior of top |
|---|---|
static |
Has no effect. |
relative |
Offsets the element vertically from its normal position. |
absolute |
Specifies the inset from the top edge of its containing block. |
fixed |
Specifies the inset from the top edge of its fixed-position containing block, usually the viewport. |
sticky |
Defines a top inset threshold used during sticky positioning. |
Step 1: Establish Positioning
.example {
position: relative;
}
Step 2: Set the Top Offset
.example {
position: relative;
top: 20px;
}
The element moves 20 pixels downward from its normal position.
Step 3: Change the Offset
.example {
position: relative;
top: -20px;
}
The element moves 20 pixels upward from its normal position.
Step 4: Change the Positioning Method
.example {
position: absolute;
top: 20px;
}
The element is now positioned using the containing block rather than being offset from its normal position.
The auto Value
The auto value allows the browser to determine the top inset according to the applicable positioning rules.
Basic Example
.example {
position: absolute;
top: auto;
}
Default Behavior
Because auto is the initial value, it is usually unnecessary to specify it unless resetting another declaration.
Restoring Automatic Positioning
.example {
position: absolute;
top: 20px;
}
.example.reset {
top: auto;
}
Using auto With bottom
.example {
position: absolute;
top: auto;
bottom: 20px;
}
The element can be positioned relative to the bottom edge of its containing block.
When to Use auto
Use auto when the top inset should be determined by other positioning constraints or when removing an explicit top offset.
Length Values
Length values specify the top inset using CSS units such as pixels, rems, ems, or viewport units.
Pixel Values
.example {
position: absolute;
top: 20px;
}
rem Values
.example {
position: absolute;
top: 2rem;
}
em Values
.example {
position: relative;
top: 1em;
}
Viewport Units
.example {
position: fixed;
top: 5vh;
}
Calculated Values
.example {
position: absolute;
top: calc(50% - 25px);
}
Using clamp()
.example {
position: fixed;
top: clamp(10px, 3vh, 40px);
}
When to Use Length Values
Use lengths when a specific inset is needed. Relative units and calculated values can help adapt positioning to different font sizes and viewport dimensions.
Percentage Values
Percentage values are calculated relative to the height of the containing block.
Basic Example
.example {
position: absolute;
top: 25%;
}
Positioning Within a Container
.container {
position: relative;
height: 300px;
}
.child {
position: absolute;
top: 50%;
}
For this example, the child's top inset is 150 pixels relative to the container's containing-block height.
Centering With transform
.container {
position: relative;
height: 300px;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
The top: 50% declaration places the child's top edge halfway down the container. The transform moves it upward by half its own height.
Percentage With calc()
.child {
position: absolute;
top: calc(50% + 10px);
}
Important Consideration
Percentage positioning depends on the containing block's dimensions. If its height is indefinite, percentage resolution can be affected by the positioning and layout rules.
Negative Values
The top property accepts negative lengths and percentages.
Basic Example
.example {
position: relative;
top: -15px;
}
The element moves upward by 15 pixels from its normal position.
Absolute Positioning
.badge {
position: absolute;
top: -10px;
}
The badge can extend above the top edge of its containing block.
Negative Percentage
.example {
position: absolute;
top: -10%;
}
When to Use Negative Values
Negative values are useful for overlapping badges, decorative labels, and small positional adjustments.
Important Consideration
Negative offsets may move content outside its visible container or cause overlap with nearby elements. Check overflow and responsive layouts.
Relative Positioning
With position: relative, the top property moves an element vertically from its normal position without removing it from the document flow.
Basic Example
.relative-box {
position: relative;
top: 20px;
}
HTML Example
<div class="relative-box">Relative Positioning</div>
Browser Result
Positive Values
.relative-box {
position: relative;
top: 20px;
}
A positive value moves the element downward.
Negative Values
.relative-box {
position: relative;
top: -20px;
}
A negative value moves the element upward.
Document Flow
The original space occupied by the element remains reserved in the document flow, even after the element is visually offset.
When to Use Relative Positioning
Use relative positioning for small visual adjustments or when establishing a containing block for absolutely positioned descendants.
Absolute Positioning
With position: absolute, the top property specifies an inset from the top edge of the element's containing block.
Basic Example
.absolute-box {
position: absolute;
top: 20px;
}
Positioning Container
.container {
position: relative;
height: 200px;
}
.absolute-box {
position: absolute;
top: 20px;
left: 20px;
}
HTML Example
<div class="container">
<div class="absolute-box">Absolute Positioning</div>
</div>
Containing Block
An absolutely positioned element is commonly positioned relative to the padding box of its nearest positioned ancestor. Other CSS properties, such as transforms, can also establish a containing block.
Document Flow
Absolutely positioned elements are removed from normal document flow. Other elements do not reserve space for them.
When to Use Absolute Positioning
Use absolute positioning for badges, overlays, icons, and interface elements that must be positioned precisely within a container.
Fixed Positioning
With position: fixed, the top property specifies an inset from the fixed-position containing block, which is usually the viewport.
Basic Example
.fixed-banner {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
Fixed Notification
.notification {
position: fixed;
top: 20px;
right: 20px;
}
Viewport Positioning
When the viewport establishes the fixed-position containing block, the element remains at the specified viewport position as the document scrolls.
Containing Block Exceptions
Properties such as transform, filter, or contain can establish a fixed-position containing block on an ancestor. In these cases, the element may be positioned relative to that ancestor instead of the viewport.
Document Flow
Fixed-positioned elements are removed from normal document flow.
When to Use Fixed Positioning
Use fixed positioning for persistent notifications, floating controls, and interface elements intended to remain visible while the page scrolls.
Sticky Positioning
With position: sticky, the top property establishes a threshold that constrains the element's position as its scroll container scrolls.
Basic Example
.sticky-heading {
position: sticky;
top: 0;
}
Sticky Header With Spacing
.sticky-heading {
position: sticky;
top: 20px;
}
HTML Example
<div class="scroll-container">
<h3 class="sticky-heading">Sticky Heading</h3>
<p>Scrollable content goes here.</p>
</div>
CSS Example
.scroll-container {
height: 200px;
overflow-y: auto;
}
.sticky-heading {
position: sticky;
top: 0;
background: #dbeafe;
}
Sticky Threshold
As scrolling brings the element toward the specified inset, sticky positioning can prevent it from moving beyond that threshold while it remains within its containing block's constraints.
Scroll Container
Sticky positioning is affected by the nearest ancestor with a scrolling mechanism. Ancestor overflow settings can therefore change which scrolling area controls the sticky behavior.
When to Use Sticky Positioning
Use sticky positioning for section headings, navigation bars, table headers, and other elements that should remain visible during part of a scrolling interaction.
Using top and bottom
The top and bottom properties can work together to determine the vertical size and position of a positioned element.
Absolute Positioning With Both Insets
.container {
position: relative;
height: 300px;
}
.panel {
position: absolute;
top: 20px;
bottom: 20px;
}
If the panel's height is auto and no other sizing constraints interfere, it stretches vertically between the two specified insets.
Fixed Positioning With Both Insets
.sidebar {
position: fixed;
top: 60px;
bottom: 0;
}
The sidebar can extend from 60 pixels below the top of its containing block to the bottom edge.
Relative Positioning With Both Insets
.example {
position: relative;
top: 20px;
bottom: 10px;
}
For a relatively positioned element with both values specified, the vertical offsets are over-constrained. The top value takes precedence, and bottom is effectively determined by it.
Absolute Positioning With a Fixed Height
.panel {
position: absolute;
top: 20px;
bottom: 20px;
height: 100px;
}
When the vertical constraints cannot all be satisfied, the positioning rules determine which value is effectively adjusted. In the usual horizontal writing mode, the specified bottom inset is the constraint that yields.
Using inset
.panel {
position: absolute;
inset: 20px 10px;
}
The inset shorthand sets the top and bottom insets to 20 pixels and the right and left insets to 10 pixels.
Related CSS Properties
The top property works with several positioning and layout properties.
position
.example {
position: absolute;
}
Determines how the element is positioned.
right
.example {
right: 20px;
}
Specifies the right inset of a positioned element.
bottom
.example {
bottom: 20px;
}
Specifies the bottom inset of a positioned element.
left
.example {
left: 20px;
}
Specifies the left inset of a positioned element.
inset
.example {
inset: 20px;
}
Sets all four physical insets using shorthand syntax.
inset-block-start
.example {
inset-block-start: 20px;
}
Specifies the logical block-start inset. In horizontal top-to-bottom writing modes, this corresponds to top.
transform
.example {
transform: translateY(-50%);
}
Visually transforms an element without changing its normal layout position.
z-index
.example {
z-index: 10;
}
Controls stacking order when applicable.
Complete Example
.container {
position: relative;
height: 250px;
}
.badge {
position: absolute;
top: 12px;
right: 12px;
z-index: 2;
}
Inheritance
The top property is not inherited. Descendant elements do not automatically receive the parent's top inset.
Parent Element Example
.parent {
position: relative;
top: 20px;
}
Child Element
.child {
position: relative;
}
The child retains its own initial top value of auto, even though it may move visually with the positioned parent.
Explicit Inheritance
.child {
top: inherit;
}
Restore the Initial Value
.child {
top: initial;
}
This restores auto.
Using unset
.child {
top: unset;
}
Because the property is not inherited, unset restores the initial value.
Important Consideration
A parent's positioning can influence the child's containing block or visual location without the top property itself being inherited.
Practical Examples
Top-Right Badge
.card {
position: relative;
}
.card-badge {
position: absolute;
top: 10px;
right: 10px;
}
Centered Overlay
.overlay-container {
position: relative;
}
.overlay-content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Sticky Section Heading
.section-heading {
position: sticky;
top: 0;
background: #fff;
}
Fixed Notification
.notification {
position: fixed;
top: 20px;
right: 20px;
z-index: 1000;
}
Relative Vertical Adjustment
.icon {
position: relative;
top: 2px;
}
Full-Height Sidebar Below a Header
.sidebar {
position: fixed;
top: 70px;
bottom: 0;
width: 260px;
overflow-y: auto;
}
Responsive Top Spacing
.floating-panel {
position: fixed;
top: clamp(12px, 3vh, 36px);
right: 16px;
}
Negative Badge Offset
.card {
position: relative;
}
.card-label {
position: absolute;
top: -12px;
left: 16px;
}
Percentage-Based Positioning
.container {
position: relative;
height: 300px;
}
.marker {
position: absolute;
top: 25%;
left: 20px;
}
Logical Positioning Alternative
.example {
position: absolute;
inset-block-start: 20px;
}
Logical inset properties are useful for layouts that must adapt to different writing modes.
top Example
This example compares three different top values using absolutely positioned elements inside separate containers.
HTML Example
<div class="top-demo">
<div class="top-sample">
<strong>top: 0;</strong>
<div class="top-container">
<div class="top-box top-zero">Top 0</div>
</div>
</div>
<div class="top-sample">
<strong>top: 20px;</strong>
<div class="top-container">
<div class="top-box top-twenty">Top 20px</div>
</div>
</div>
<div class="top-sample">
<strong>top: 50px;</strong>
<div class="top-container">
<div class="top-box top-fifty">Top 50px</div>
</div>
</div>
</div>
CSS Example
.top-demo {
display: grid;
gap: 16px;
}
.top-sample strong {
display: block;
margin-bottom: 8px;
}
.top-container {
position: relative;
height: 110px;
border: 1px dashed #94a3b8;
background: #f8fafc;
}
.top-box {
position: absolute;
left: 12px;
padding: 10px 16px;
background: #dbeafe;
border: 1px solid #2563eb;
color: #1e40af;
}
.top-zero {
top: 0;
}
.top-twenty {
top: 20px;
}
.top-fifty {
top: 50px;
}
Understanding the Results
Each blue box is absolutely positioned relative to its containing block. Increasing the top value moves the box farther down from the container's top edge.
Why position: relative Is Used on the Container
The container establishes the containing block for the absolutely positioned child, keeping the offset relative to the intended example area.
Experiment With Negative Values
.top-zero {
top: -10px;
}
The first box moves above the container's top edge.
Try Percentage Values
.top-twenty {
top: 25%;
}
The box is positioned using a percentage of its containing block's height.
Accessibility Considerations
Positioned elements should remain readable, reachable, and usable at different viewport sizes and zoom levels.
Avoid Covering Important Content
Fixed and sticky elements can overlap headings, links, form controls, or other important content.
Allow Content to Reflow
Test layouts at narrow widths and high zoom levels to ensure positioned elements do not obscure essential information.
Maintain Logical Reading Order
Visual positioning does not change the document's source order. Keep the HTML order meaningful for keyboard navigation and assistive technology.
Avoid Off-Screen Interactive Controls
Negative offsets or large fixed insets can place interactive elements outside the visible area.
Account for Sticky Headers
Sticky headers can cover content reached through anchor links. Consider using scroll-margin-top on target elements.
section[id] {
scroll-margin-top: 80px;
}
Test Keyboard Navigation
Verify that positioned controls remain visible and reachable when navigating with the keyboard.
Use Responsive Insets
Relative units, media queries, and calculated values can help prevent overlap on smaller screens.
Common Mistakes
Using top Without Positioning
.example {
top: 20px;
}
The property has no effect when the element uses the default position: static.
Using Invalid Keywords
.example {
top: center;
}
Use a valid length, percentage, auto, or CSS-wide keyword.
Omitting Units
.example {
top: 20;
}
Nonzero length values require units, such as 20px.
Expecting Relative Positioning to Change Document Flow
Relative offsets move an element visually, but its original space remains reserved.
Forgetting the Containing Block
Absolutely positioned elements may appear in unexpected locations when the intended ancestor does not establish their containing block.
Assuming Fixed Positioning Always Uses the Viewport
Certain ancestor properties can establish a fixed-position containing block.
Expecting Sticky Positioning Without an Inset
.example {
position: sticky;
}
Specify an appropriate inset, such as top: 0, to enable sticky positioning along the vertical axis.
Ignoring Overflow Settings
Ancestor overflow properties can affect sticky positioning and the visibility of offset elements.
Using top for Every Layout Adjustment
Flexbox, Grid, margins, and padding are often more appropriate for ordinary document layout.
Confusing top: 50% With Vertical Centering
The declaration positions the element's top edge at the halfway point. It does not center the element by itself.
Ignoring Overlapping Content
Absolute and fixed positioning remove elements from normal flow, which can cause overlap.
Forgetting Responsive Testing
Fixed pixel offsets may not work well at different viewport sizes or zoom levels.
Important Considerations
Keep the following points in mind when using top:
- The property specifies the vertical inset of a positioned element.
- The initial value is
auto. - The property is not inherited.
- It has no effect on statically positioned elements.
- Its behavior depends on the
positionvalue. - Positive relative offsets move elements downward.
- Negative relative offsets move elements upward.
- Relative positioning preserves the element's original space in document flow.
- Absolute positioning uses a containing block.
- Absolutely positioned elements are removed from normal flow.
- Fixed positioning usually uses the viewport as its containing block.
- Some ancestor properties can change the fixed-position containing block.
- Sticky positioning uses the inset as a scrolling threshold.
- Ancestor overflow settings can affect sticky positioning.
- Length and percentage values are supported.
- Negative values are permitted.
- Percentage values are relative to the containing block's height.
- The property supports calculations using
calc(). - The
topandbottomproperties can interact through positioning constraints. - The
insetshorthand can set multiple inset properties. - The logical equivalent depends on the writing mode.
- Visual positioning does not change the HTML reading order.
- Fixed and sticky elements may cover other content.
- Test positioned elements at different screen widths and zoom levels.
- Use Flexbox or Grid for ordinary layout whenever more appropriate.
Browser Support
The CSS top property is widely available across modern browsers and has been supported for many years. Basic relative, absolute, and fixed positioning are well established, while sticky positioning and some newer positioning features have additional compatibility considerations.
For current compatibility information, visit Can I Use? and search for the CSS top property.
Basic Declaration
.example {
position: absolute;
top: 20px;
}
Relative Positioning
.example {
position: relative;
top: 10px;
}
Sticky Positioning
.example {
position: sticky;
top: 0;
}
Testing Recommendations
Test the property together with the intended positioning method, containing block, overflow settings, and responsive layout. Browser differences are more likely to involve positioning context or newer features than basic top syntax.
Specifications
The top property is defined in the CSS Positioned Layout Module Level 3 specification.
CSS Positioned Layout Module Level 3 - Inset Properties
The specification defines the physical inset properties and their behavior for positioned elements.
For additional documentation and examples, consult MDN Web Docs.
