CSS position: absolute Property Value
position: absoluteIt is one of the most powerful values in the CSS positioning property.
Absolute positioningMake the elementOut of normal document flow, no longer occupies space, then relative to itsnearest positioned ancestor elementPerform precise positioning.
This is the core technology for creating UI components such as popups, dropdown menus, floating cards, and more.
Word Definitions:absoluteFrom Latinabsolutusmeaning "absolute, unconditional", indicating that the position is completely independent of the document flow.
Basic Syntax
position: absoluteNeed to be used withtop、right、bottom、leftproperty to specify the position.
/* 语法 */
position: absolute;
/* 常用组合 */
.box {
position: absolute;
top: 0; /* 顶部对齐 */
left: 0; /* 左侧对齐 */
/* 或者 */
top: 50%; /* 居中开始 */
left: 50%;
transform: translate(-50%, -50%); /* 完全居中 */
}
Positioning principle
UnderstandingabsoluteThe key to positioning ispositioning contextThe concept of:
positioning context
Absolutely positioned elements willSearch upward layer by layer, find the firstnon-static positioningThe ancestor element is used as a reference. If no such ancestor element is found, then relative toInitial containing blockpositioned (usually the page).
Search process
┌─────────────────────────────────────┐ │ 初始包含块 (body 或 html) │ │ ┌─────────────────────────────┐ │ │ │ parent (position: relative) │ ← 找到!作为参照 │ │ │ ┌─────────────────────┐ │ │ │ │ │ child (absolute) │ │ │ │ │ │ top: 0, left: 0 │ │ │ │ │ └─────────────────────┘ │ │ │ └─────────────────────────────┘ │ └─────────────────────────────────────┘
Core features
Feature 1: Removed from the document flow
absoluteelementDoes not occupy document spaceThe surrounding elements ignore its existence and arrange normally. This is as if the element has "floated" out of the document.
Feature 2: Positioning relative to a positioned ancestor
"Positionable" refers topositionThe value is notstatic(can berelative、absolute、fixedorsticky)。
Feature 3: Activating z-index
andrelativeLikewise,absolutewill activatez-indexProperty.
Feature 4: margin merges with padding
Unlike the normal document flow,absoluteThe margin of the element does not collapse with the margins of adjacent elements.
Example
Let's deeply understand through examplesposition: absolutehow it works.
Example 1: The most basic absolute positioning
This example shows how an element is positioned relative to its parent container:
Example
<html>
<head>
<style>
/* The parent container needs non-static positioning to serve as a reference */
.container {
position: relative; /* Key: as a positioning reference */
width: 300px;
height: 200px;
background-color: #f5f5f5;
border: 2px solid #3498db;
}
/* Absolutely positioned element */
.absolute-box {
position: absolute;
top: 20px;
right: 20px;
width: 80px;
height: 40px;
background-color: #e74c3c;
color: white;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
Parent container (position: relative)
<div class="absolute-box">Absolute positioning</div>
</div>
<p>The red box is positioned relative to the top-right corner of the parent container (not the page).</p>
</body>
</html>
Example 2: When there is no positioned ancestor
If the parent element has no positioning set, the element will keep looking upward and ultimately be positioned relative to the page:
Example
<html>
<head>
<style>
/* Note: position: relative is not set here */
.container-no-position {
width: 300px;
height: 150px;
background-color: #f0f0f0;
border: 2px solid #ccc;
padding: 20px;
}
/* Since the parent element has no positioning, it will be positioned relative to the page */
.floating-box {
position: absolute;
top: 10px;
left: 10px;
background-color: #2E7DCC;
color: white;
padding: 10px 20px;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container-no-position">
Parent container (no positioning)
<div class="floating-box">Positioned relative to the page</div>
</div>
<p>Since the parent container does not have position set, the green box will be positioned relative to the top-left corner of the entire page (body).</p>
</body>
</html>
Example 3: Creating a centered popup box
This isabsoluteOne of the most common application scenarios: creating popups.
Example
<html>
<head>
<style>
/* Overlay: covers the entire page */
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
}
/* Modal container */
.modal {
position: relative; /* Creates a positioning context */
width: 400px;
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
overflow: hidden;
}
/* Modal header */
.modal-header {
background-color: #3498db;
color: white;
padding: 15px 20px;
font-size: 18px;
font-weight: bold;
}
/* Close button: absolutely positioned */
.modal-close {
position: absolute;
top: 10px;
right: 15px;
cursor: pointer;
font-size: 24px;
line-height: 1;
}
/* Modal body */
.modal-body {
padding: 20px;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="overlay">
<div class="modal">
<div class="modal-header">
Tooltip
<span class="modal-close">×</span>
</div>
<div class="modal-body">
This is a centered modal created using absolute positioning.
The close button is positioned in the top-right corner using position: absolute.
</div>
</div>
</div>
</body>
</html>
Example 4: Adding a text watermark to an image
Using absolute positioning, you can overlay text or labels on an image:
Example
<html>
<head>
<style>
.image-container {
position: relative; /* Creates a positioning context */
display: inline-block;
}
.image-container img {
width: 300px;
height: 200px;
display: block;
object-fit: cover;
}
/* Top-left label */
.badge {
position: absolute;
top: 10px;
left: 10px;
background-color: #e74c3c;
color: white;
padding: 4px 10px;
font-size: 12px;
font-weight: bold;
border-radius: 3px;
}
/* Bottom-right watermark */
.watermark {
position: absolute;
bottom: 10px;
right: 10px;
color: white;
font-size: 14px;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8);
}
</style>
</head>
<body>
<div class="image-container">
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='200'%3E%3Crect fill='%233498db' width='300' height='200'/%3E%3Ctext x='50%25' y='50%25' text-anchor='middle' fill='white' font-size='24'%3EEXAMPLE%3C/text%3E%3C/svg%3E" alt="Sample image">
<span class="badge">Featured</span>
<span class="watermark">example.com</span>
</div>
</body>
</html>
Example 5: Dropdown menu
Absolute positioning is a key technology for creating dropdown menus:
Example
<html>
<head>
<style>
.dropdown {
position: relative; /* Create positioning context */
display: inline-block;
}
/* Trigger button */
.dropbtn {
background-color: #3498db;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
/* Dropdown content */
.dropdown-content {
position: absolute; /* Absolute positioning */
top: 100%; /* Display below the button */
left: 0;
min-width: 160px;
background-color: white;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
border-radius: 4px;
display: none; /* Hidden by default */
}
/* Show on mouse hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Dropdown options */
.dropdown-content a {
display: block;
padding: 12px 16px;
text-decoration: none;
color: #333;
border-bottom: 1px solid #eee;
}
.dropdown-content a:hover {
background-color: #f5f5f5;
}
.dropdown-content a:last-child {
border-bottom: none;
}
</style>
</head>
<body>
<div class="dropdown">
<button class="dropbtn">dropdown menu</button>
<div class="dropdown-content">
<a href="#">Option 1</a>
<a href="#">Option 2</a>
<a href="#">Option 3</a>
</div>
</div>
<p>Hover over above to see dropdown effect.</p>
</body>
</html>
Example 6: 3x3 grid positioning
Using absolute positioning, you can easily achieve nine-grid alignment of elements:
Example
<html>
<head>
<style>
.box {
position: relative;
width: 300px;
height: 200px;
background-color: #f5f5f5;
border: 2px solid #ddd;
}
.corner {
position: absolute;
width: 80px;
height: 40px;
background-color: #3498db;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
border-radius: 4px;
}
/* Four corners */
.top-left { top: 10px; left: 10px; }
.top-right { top: 10px; right: 10px; }
.bottom-left { bottom: 10px; left: 10px; }
.bottom-right { bottom: 10px; right: 10px; }
/* Centers of the four sides */
.top-center {
top: 10px;
left: 50%;
transform: translateX(-50%);
}
.bottom-center {
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.left-center {
left: 10px;
top: 50%;
transform: translateY(-50%);
}
.right-center {
right: 10px;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="box">
<div class="corner top-left">Top left</div>
<div class="corner top-center">Top</div>
<div class="corner top-right">Top right</div>
<div class="corner left-center">Left</div>
<div class="corner right-center">Right</div>
<div class="corner bottom-left">Bottom left</div>
<div class="corner bottom-center">Bottom</div>
<div class="corner bottom-right">Bottom right</div>
</div>
</body>
</html>
Notes
Important note:Absolutely positioned elements are taken out of the document flow, which means they do not occupy any space. If all sibling elements use absolute positioning, the container may "collapse" (height becomes 0). Ensure the parent container has explicit dimensions or use other methods to support the height.
Positioning ancestor chain
Absolute positioning looks upward layer by layer for a positioned ancestor:
元素的查找顺序: 1. 首先查找父元素是否有非 static 定位 2. 如果没有,继续查找祖父元素 3. 依此类推,直到 html 元素 4. 如果都没有,则相对于初始包含块(页面)定位
Width issue
The width of absolutely positioned elementsDetermined by content by default(similar to inline elements). If you need to fill the container, you need to explicitly set the width:
.full-width {
position: absolute;
left: 0;
right: 0; /* 配合 left: 0 和 right: 0 使宽度100% */
/* 或者 */
width: 100%; /* 直接设置宽度 */
}
Comparison with other positioning values
| position value | Whether it leaves the document flow | positioning reference | Whether it moves with scrolling |
|---|---|---|---|
static |
no | Normal document flow | Yes |
relative |
No (retains space) | Original position | Yes |
absolute |
Yes | Nearest positioned ancestor | Yes |
fixed |
Yes | Viewport (browser window) | no |
sticky |
No (before fixed) | td> viewport (after fixed)Yes (after fixed, no) |
Common application scenarios
Scenario 1: Fixed header navigation
.navbar {
position: fixed;
top: 0;
width: 100%;
height: 60px;
}
Scenario 2: Image carousel indicators
.carousel {
position: relative;
}
.indicators {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
Scenario 3: Bubble positioning in a chat window
.message-bubble {
position: absolute;
left: 20px;
max-width: 70%;
}
Summary
position: absoluteIt is an indispensable technology in modern web page layout:
- elementTaken out of the document flow, does not occupy space
- relative tonearest positioned ancestor elementPositioning
- Need to be used with
top、right、bottom、leftUsage - activate
z-index, can control the stacking order - It is the core technology for creating popups, dropdown menus, and floating labels.
- The parent container needs to be set to
relativeto create a positioning context
other extensions