Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS position: relative Property Value

CSS position: relative Property Value

CSS position Property CSS position property


position: relative is a very practical value among CSS positioning properties.

Relative positioningallows an element to be offset relative to itsoriginal position in the normal document flow, while preserving its space in the document flow.

This means that after the element moves, the position it originally occupiedis still preserved, without affecting the layout of surrounding elements.

Word meaning:relative comes from Latin relativus, meaning "relative and related," indicating that the position is determined relative to some reference point.


Basic Syntax

position: relative Needs to be used together with top、right、bottom、left property to specify the offset.



/* 语法 */

position: relative;



/* Common combination: offset up 20px, offset left 10px */

.box {

    position: relative;

    top: -20px;

    left: 10px;

}

Property description

relative Positioning needs to be used together with the following offset properties:

Offset property

property Description Effect of positive value Effect of negative value
top Offset relative to the top edge of the original position Move down Move up
bottom Offset relative to the bottom edge of the original position Move up Move down
left Offset relative to the left edge of the original position Move right Move left
right Offset relative to the right edge of the original position Move left Move right

z-index property

position: relative willActivate z-index property, allowing you to control the stacking order of elements:



.box {

    position: relative;

    z-index: 10;  /* 设置堆叠顺序,数值越大越在上层 */

}


Core Features

Feature 1: Preserves the original space

This is relative The most important feature. After an element is offset, its original position in the document flowis still preserved, and surrounding elements do not move to fill the gap.

Feature 2: Serves as a reference for absolute positioning

relative elements are often used aspositioning context, allowing the absolute elements inside to be positioned relative to it:



/* parent element relative as reference */

.parent {

    position: relative;

}



/* Child element absolute positioning relative to parent */

.child {

    position: absolute;

    top: 0;

    left: 0;

}

Feature 3: Activates z-index

Only when the position Either static , thez-index takes effect.


Example

Let us understand in depth through examples position: relative characteristic.

Example 1: Basic usage of relative offset

This example shows how an element is offset relative to its original position:

Example

   

   

       

Box 1

       

Box 2 (relative)

       

Box 3

   

   

Observation: Box 2 is offset, but its original position is still preserved (Box 3's position is unchanged).

Example 2: Negative offset

Negative values can move an element in the opposite direction:

Example

   

   

       

Original position

       

top: -20px (move up)

       

left: -30px (move left)

       

Combined offsets

   

Example 3: Serving as a reference container for absolute positioning

relative The most common use is to create a positioning container so that child elements' absolute positioning is relative to it:

Example

   

   

       

Card image

       

NEW

       

View details

   

   

Since .card has position: relative set, .card-badge and .card-action are positioned relative to the card's edges, not the entire page.

Example 4: Using z-index to control stacking order

relative Positioning can be combined with z-index to achieve a stacking effect for elements:

Example

   

   

       

z-index: 1

       

z-index: 2

       

z-index: 3

   

   

The larger the z-index value, the higher the element is stacked. The purple box (z-index: 3) covers the green box (z-index: 2).


Notes

Performance tip:Use position: relative When offsetting, although the element visually moves, its original position in the document flow is still preserved. This means the browser needs to render two positions at once, which may affect rendering performance. Use with caution in complex pages.

Using top and bottom together

When top and bottom When both are used,top has higher priority,bottom is ignored:



.box {

    position: relative;

    top: 20px;

    bottom: 50px;  /* 会被忽略,元素最终在 top: 20px 的位置 */

}

Using left and right together

Likewise,left has higher priority than right:



.box {

    position: relative;

    left: 30px;

    right: 50px;  /* 会被忽略,元素最终在 left: 30px 的位置 */

}


Comparison with other positioning values

Positioning value Whether original space is preserved Offset reference Common Use Cases
static Yes (not offset) None Default layout
relative Is Original position Fine-tuning position, creating a positioning container
absolute No Nearest positioned ancestor element Precisely place elements
fixed No viewport Fixed navigation, back to top
sticky Yes (before the threshold) Viewport (after the threshold) Fixed when scrolling

Common Application Scenarios

Scenario 1: Fine-tuning the alignment of icons and text

Sometimes icons and text are slightly misaligned; you can use relative to adjust precisely:



.icon {

    position: relative;

    top: 2px;  /* 向下微调 2px,实现更好的垂直居中 */

}

Scenario 2: Slight displacement effect on hover

The button moves down a little on hover to add a sense of interaction:



.button:hover {

    position: relative;

    top: 2px;

    box-shadow: 0 4px 8px rgba(0,0,0,0.2);

}

Scenario 3: Creating a positioning container for a pop-up



.modal-container {

    position: relative;  /* 让弹窗相对于这个容器定位 */

}



.modal {

    position: absolute;

    top: 50%;

    left: 50%;

    transform: translate(-50%, -50%);

}


Summary

position: relative is a very important positioning method in CSS layout:


CSS position Property CSS position property