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

CSS position: absolute Property Value

CSS position: absolute Property Value

CSS position Property CSS position property


position: absolute is one of the most powerful values among CSS positioning properties.

Absolute positioningmakes the elementbreak out of the normal document flow, no longer occupying space, and then relative to itsnearest positioned ancestor elementfor precise positioning.

This is the core technique for making UI components such as pop-up boxes, dropdown menus, and floating cards.

Word meaning:absolute comes from Latin absolutus, meaning "absolute and unconditional," indicating that the position is completely independent of the document flow.


Basic Syntax

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



/* 语法 */

position: absolute;



/* Common Combinations */

.box {

    position: absolute;

    top: 0;      /* 顶部对齐 */

    left: 0;     /* 左侧对齐 */

    /* 或者 */

    top: 50%;    /* 居中开始 */

    left: 50%;

    transform: translate(-50%, -50%);  /* 完全居中 */

}

Positioning principle

Understand absolute The key to positioning is thepositioning contextconcept:

positioning context

An absolutely positioned element willsearch upward level by level, find the firstnon-static positionedancestor element as a reference. If no such ancestor element is found, it is positioned relative to theinitial containing block(usually the page).

Search process



┌─────────────────────────────────────┐

│ 初始包含块 (body 或 html)           │

│  ┌─────────────────────────────┐   │

│  │ parent (position: relative)  │ ← 找到!作为参照 │

│  │  ┌─────────────────────┐    │   │

│  │  │ child (absolute)     │    │   │

│  │  │ top: 0, left: 0      │    │   │

│  │  └─────────────────────┘    │   │

│  └─────────────────────────────┘   │

└─────────────────────────────────────┘


Core Features

Feature 1: Out of the document flow

absolute Elementdoes not occupy document space, surrounding elements ignore its existence and arrange normally. It is as if the element has "floated" out of the document.

Feature 2: Positioning relative to a positioned ancestor

"positionable" means the position value is not static(it can be relative、absolute、fixed or sticky)。

Feature 3: Activates z-index

and relative In the same way,absolute activates the z-index attribute.

Feature 4: margin merges with padding

Unlike the normal document flow,absolute the element's margin does not collapse with adjacent elements' margins.


Example

Let us understand in depth through examples how position: absolute it works.

Example 1: The most basic absolute positioning

This example shows how an element is positioned relative to its parent container:

Example

   

   

        Parent container (position: relative)

       

Absolute positioning

   

   

The red box is positioned relative to the top-right corner of the parent container (not the page).

Example 2: When there is no positioned ancestor

If the parent element has no positioning set, the element will keep searching upward and eventually be positioned relative to the page:

Example

   

   

        Parent container (no positioning)

       

Positioned relative to the page

   

   

Since the parent container has no position set, the green box is positioned relative to the top-left corner of the entire page (body).

Example 3: Creating a centered pop-up box

This is absolute One of the most common application scenarios: creating a pop-up window.

Example

   

   

       

   

Example 4: Adding a text watermark to an image

Using absolute positioning, you can overlay text or labels on an image:

Example

   

   

       

Sample image

       

Featured

       

RUNOOB.com

   

Example 5: Dropdown menu

Absolute positioning is the key technique for making dropdown menus:

Example

   

   

   

Hover the mouse above to see the dropdown effect

Example 6: Nine-grid positioning

Using absolute positioning, you can easily achieve nine-grid alignment of elements:

Example

   

   

       

Top-left

       

up

       

Top-right

       

Left

       

Right

       

Bottom-left

       

down

       

Bottom right

   


Notes

Important note:An absolutely positioned element breaks out of the document flow, which means it does not occupy any space. If all sibling elements use absolute positioning, the container may "collapse" (its height becomes 0). Make sure the parent container has an explicit size or use other means to expand its height.

Positioned ancestor chain

Absolute positioning searches upward level by level for a positioned ancestor:



Element search order:

1. 首先查找父元素是否有非 static 定位

2. 如果没有,继续查找祖父元素

3. 依此类推,直到 html 元素

4. 如果都没有,则相对于初始包含块(页面)定位

Width issue

The width of an absolutely positioned elementis determined by its content by default(similar to an inline element). If you need to fill the container, you need to explicitly set a width:



.full-width {

    position: absolute;

    left: 0;

    right: 0;     /* 配合 left: 0 和 right: 0 使宽度100% */

    /* 或者 */

    width: 100%;  /* 直接设置宽度 */

}


Comparison with other positioning values

Positioning value Whether it breaks out of the document flow Positioning reference Whether it moves with scrolling
static No Normal document flow Is
relative No (space is preserved) Original position Is
absolute Is Nearest positioned ancestor Is
fixed Is Viewport (browser window) No
sticky No (before fixing) Yes (no after fixing)

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: Chat window bubble positioning



.message-bubble {

    position: absolute;

    left: 20px;

    max-width: 70%;

}


Summary

position: absolute is an indispensable technique in modern web layout:


CSS position Property CSS position property