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

CSS Position (Positioning)

CSS Position (Positioning)


position The property is used to specify the positioning method of an element.

The five common values are as follows:

Value Description Anchor
static default value; the element is laid out according to the normal document flow and is unaffected by top/right/bottom/left View example
relative offset relative to its own original position, without being removed from the document flow View example
absolute positioned relative to the nearest positioned ancestor element (otherwise relative to body), removed from the document flow View example
fixed positioned relative to the browser window, and the position does not change when the page is scrolled View example
sticky behaves like relative before scrolling to the specified position, and like fixed after that View example

Elements can adjust their position using the top/right/bottom/left top、right、bottom and left properties, but these only take effect when position positionis set (and the value is not static static). Different positioning methods affect the reference object and behavior of these properties.


static positioning

the default value for HTML elements, meaning no positioning; the element follows the normal document flow.

Statically positioned elements are not affected by top, bottom, left, or right.

Example

div

.static

{

position:

static

;

border:

3

px

solid

#73AD21

;

}

Try it »


fixed positioning

The position of the element is fixed relative to the browser window.

It will not move even if the window is scrolled:

Example

p

.pos

_

fixed

{

position:

fixed

;

top:

30

px

;

right:

5

px

;

}

Try it »

Note: Fixed positioning requires a !DOCTYPE declaration to be supported in IE7 and IE8.

Fixed positioning makes the element's position independent of the document flow, so it does not occupy space.

Fixed-positioned elements overlap with other elements.


relative positioning

The positioning of a relatively positioned element is relative to its normal position.

Example

h2

.pos

_

left

{

position:

relative

;

left:

20

px

;

}

h2

.pos

_

right

{

position:

relative

;

left:

20

px

;

}

Try it »

Move a relatively positioned element, but the space it originally occupied does not change.

Example

h2

.pos

_

top

{

position:

relative

;

top:

50

px

;

}

Try it »

Relatively positioned elements are often used as container blocks for absolutely positioned elements.


absolute positioning

The position of an absolutely positioned element is relative to the nearest positioned parent element; if the element has no positioned parent, its position is relative to:

Example

h2

{

position:

absolute

;

left:

100

px

;

top:

150

px

;

}

Try it »

absolute positioning makes the element's position independent of the document flow, so it does not occupy space.

absolutely positioned elements overlap with other elements.


sticky positioning

The English word "sticky" literally means to stick or paste, so it can be called sticky positioning.

position: sticky; positions based on the user's scroll position.

A sticky positioned element depends on the user's scrolling, switching between position:relative The difference between position:fixed relative and fixed positioning.

Its behavior is like relative positioning; position:relative; and when the page scrolls beyond the target area, its behavior is like position:fixed;fixed positioning, and it will be fixed at the target position.

The element's positioning behaves as relative before crossing a specific threshold, and fixed after that.

This specific threshold refers to one of top, right, bottom, or left. In other words, only by specifying one of the four thresholds top, right, bottom, or left can sticky positioning take effect. Otherwise, its behavior is the same as relative positioning.

**Note: **Internet Explorer, Edge 15 and earlier IE versions do not support sticky positioning. Safari requires the -webkit- prefix (see the examples below).

Example

div

.sticky

{

position:

webkit-sticky

;

/*

Safari

*/

position:

sticky

;

top:

0

;

background-color:

green

;

border:

2

px

solid

#4CAF50

;

}

Try it »


Overlapping elements

The positioning of elements is independent of the document flow, so they can cover other elements on the page

The z-index property specifies the stacking order of an element (which element should be placed in front, or behind)

An element can have a positive or negative stacking order:

Example

img

{

position:

absolute

;

left:

0

px

;

top:

0

px

;

z-index:

1

;

}

Try it »

An element with a higher stacking order is always in front of an element with a lower stacking order.

Note: If two positioned elements overlap and no z - index is specified, the element positioned last in the HTML code will be displayed on top.


More examples

Clipping the shape of an element

This example demonstrates how to set the shape of an element. The element is clipped into this shape and displayed.

How to use scrollbars to display overflowing content within an element

This example demonstrates how the overflow property creates a scrollbar and how to set an element to fit when its content is too large for the specified area.

How to set the browser to handle overflow automatically

This example demonstrates how to set the browser to handle overflow automatically.

Change the cursor

This example demonstrates how to change the cursor.


All CSS positioning properties

The numbers in the "CSS" column indicate which CSS version (CSS1 or CSS2) defined the property.

property Description Value CSS
bottom Defines the offset between the bottom margin edge of a positioned element and the bottom edge of its containing block. auto
 *length        

%
*inherit | 2 | | clip | Clips an absolutely positioned element | *shape
*auto
inherit | 2 | | cursor | Displays the cursor as the specified type | url
auto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help | 2 | | left | Defines the offset between the left margin edge of a positioned element and the left edge of its containing block. | auto
*length
%
*inherit | 2 | | overflow | Sets what happens when the content of an element overflows its area. | auto
hidden
scroll
visible
inherit | 2 | | overflow-y | Specifies how to handle content overflowing the element's content area at the top/bottom edges | auto
hidden
scroll
visible
no-display
no-content | 2 | | overflow-x | Specifies how to handle content overflowing the element's content area at the right/left edges | auto
hidden
scroll
visible
no-display
no-content | 2 | | position | Specifies the positioning type of an element | absolute
fixed
relative
static
inherit | 2 | | right | Defines the offset between the right margin edge of a positioned element and the right edge of its containing block. | auto
*length
%
*inherit | 2 | | top | Defines the offset between the top margin edge of a positioned element and the top edge of its containing block. | auto
*length
%
*inherit | 2 | | z-index | Sets the stacking order of an element | *number
*auto
inherit | 2 |