CSS position: relative Property Value
position: relativeis a very practical value in CSS positioning properties.
Relative PositioningAllows an element to be positioned relative to itsoriginal position in the normal document flowperforms the offset while retaining its space in the document flow.
This means that after the element moves, the position it originally occupiedstill retained, without affecting the layout of surrounding elements.
Word Definitions:relativeFrom Latinrelativus, meaning "relative" or "related", indicating that the position is determined relative to a certain reference point.
Basic Syntax
position: relativeNeed to be used withtop、right、bottom、leftattribute to specify the offset.
/* 语法 */
position: relative;
/* 常用组合:向上偏移 20px,向左偏移 10px */
.box {
position: relative;
top: -20px;
left: 10px;
}
Property description
relativePositioning needs to be used in combination with the following offset properties:
offset properties
| Property | Description | Positive value effect | Negative value effect |
|---|---|---|---|
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: relativewillactivate z-indexproperty, allowing you to control the stacking order of elements:
.box {
position: relative;
z-index: 10; /* 设置堆叠顺序,数值越大越在上层 */
}
Core features
Feature 1: Preserves original space
This isrelativeThe most important feature. After the element is offset, its original position in the document flowstill retained, surrounding elements will not move to fill this gap.
Feature 2: Serves as a reference for absolute positioning
relativeElements are often used aspositioning context, making theabsoluteThe element is positioned relative to it:
/* 父元素 relative 作为参照 */
.parent {
position: relative;
}
/* 子元素 absolute 相对于 parent 定位 */
.child {
position: absolute;
top: 0;
left: 0;
}
Feature 3: Activates z-index
only whenpositionis notstaticwhen,z-indexwill take effect.
Example
Let's understand deeply through examplesposition: relativeCharacteristics.
Example 1: Basic usage of relative offset
This example shows how an element is offset relative to its original position:
Example
<html>
<head>
<style>
.container {
padding: 20px;
background-color: #f5f5f5;
border: 2px solid #ddd;
}
.box {
width: 120px;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
margin: 10px;
background-color: #3498db;
border-radius: 4px;
}
/* The second box uses relative positioning offset */
.box-2 {
position: relative;
top: 20px; /* Move down 20px relative to the original position */
left: 30px; /* Move right 30px relative to the original position */
background-color: #e74c3c; /* Red, more eye-catching */
}
</style>
</head>
<body>
<div class="container">
<div class="box box-1">Box 1</div>
<div class="box box-2">Box 2 (relative)</div>
<div class="box box-3">Box 3</div>
</div>
<p>Observation: Box 2 is offset, but its original position is still retained (Box 3's position remains unchanged).</p>
</body>
</html>
Example 2: Negative offset
Negative values can move the element in the opposite direction:
Example
<html>
<head>
<style>
.container {
padding: 30px;
background-color: #f0f0f0;
border: 2px solid #ccc;
}
.box {
width: 150px;
height: 50px;
background-color: #2E7DCC;
color: white;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
margin-bottom: 5px;
}
/* Use negative values to move up and left */
.moved-up {
position: relative;
top: -20px; /* Move up 20px */
}
.moved-left {
position: relative;
left: -30px; /* Move left 30px */
}
.both {
position: relative;
top: -40px;
left: 20px;
background-color: #e74c3c;
}
</style>
</head>
<body>
<div class="container">
<div class="box">Original position</div>
<div class="box moved-up">top: -20px (move up)</div>
<div class="box moved-left">left: -30px (move left)</div>
<div class="box both">Combined offset</div>
</div>
</body>
</html>
Example 3: As a reference container for absolute positioning
relativeThe most common use is to create a positioning container, allowing child elements'absolutePositioning relative to it:
Example
<html>
<head>
<style>
/* The parent container is set to relative, creating a positioning context */
.card {
position: relative; /* Key: reference for positioning child elements */
width: 300px;
height: 200px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
overflow: hidden;
}
/* Image styles */
.card-image {
width: 100%;
height: 100%;
background-color: #3498db;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 24px;
}
/* The label uses absolute positioning relative to the .card container */
.card-badge {
position: absolute;
top: 10px;
right: 10px;
background-color: #e74c3c;
color: white;
padding: 5px 12px;
border-radius: 12px;
font-size: 12px;
font-weight: bold;
}
/* Another common use: the action button in the bottom-right corner */
.card-action {
position: absolute;
bottom: 15px;
right: 15px;
background-color: #2E7DCC;
color: white;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="card">
<div class="card-image">Card image</div>
<div class="card-badge">NEW</div>
<div class="card-action">View details</div>
</div>
<p>Since .card sets position: relative, .card-badge and .card-action will be positioned relative to the card edges, not the entire page.</p>
</body>
</html>
Example 4: Using z-index to control the stacking order
relativePositioning can be combined withz-indexAchieve the stacking effect of elements:
Example
<html>
<head>
<style>
.container {
position: relative;
height: 200px;
background-color: #f5f5f5;
border: 2px solid #ddd;
}
.box {
width: 120px;
height: 80px;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
border-radius: 4px;
}
/* First box */
.box-1 {
top: 20px;
left: 20px;
background-color: #e74c3c;
z-index: 1;
}
/* Second box */
.box-2 {
top: 40px;
left: 60px;
background-color: #2E7DCC;
z-index: 2;
}
/* Third box */
.box-3 {
top: 60px;
left: 100px;
background-color: #9b59b6;
z-index: 3;
}
</style>
</head>
<body>
<div class="container">
<div class="box box-1">z-index: 1</div>
<div class="box box-2">z-index: 2</div>
<div class="box box-3">z-index: 3</div>
</div>
<p>The larger the z-index value, the higher the element is. The purple box (z-index: 3) covers the green box (z-index: 2).</p>
</body>
</html>
Notes
Performance tip:Usage
position: relativeWhen offsetting, although the element moves visually, its original position in the document flow is still preserved. This means the browser needs to render two positions simultaneously, which may affect rendering performance. It should be used with caution on complex pages.
Using top and bottom simultaneously
WhentopandbottomWhen used together,tophas higher priority,bottomWill be ignored:
.box {
position: relative;
top: 20px;
bottom: 50px; /* 会被忽略,元素最终在 top: 20px 的位置 */
}
Using left and right simultaneously
Likewise,lefthas higher priority thanright:
.box {
position: relative;
left: 30px;
right: 50px; /* 会被忽略,元素最终在 left: 30px 的位置 */
}
Comparison with other positioning values
| position value | Whether to preserve original space | Offset reference | Common uses |
|---|---|---|---|
static |
is (unoffset) | None | Default layout |
relative |
Yes | Original position | Fine-tune position, create a positioning container |
absolute |
no | Nearest positioned ancestor element | Precisely place elements |
fixed |
no | Viewport | Fixed navigation, back to top |
sticky |
is (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, so you can userelativePrecise adjustment:
.icon {
position: relative;
top: 2px; /* 向下微调 2px,实现更好的垂直居中 */
}
Scenario 2: Slight displacement effect on hover
The button moves down a little on hover, adding a sense of interactivity:
.button:hover {
position: relative;
top: 2px;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
Scenario 3: Create a positioning container for a popup
.modal-container {
position: relative; /* 让弹窗相对于这个容器定位 */
}
.modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Summary
position: relativeis a very important positioning method in CSS layout:
- The element is relative toOriginal positionoffset, whilePreserve original space
- activate
z-index, can control the stacking order - often used as
absolutepositioningreference container topandlefthas higher priority thanbottomandright- Often used for fine-tuning element positions, creating positioning contexts, and creating interactive effects
other extensions