CSS Position
positionThe property is used to specify the positioning method of an element.
The five common values are as follows:
| Value | Description | Anchor |
|---|---|---|
| static | Default value. Elements are arranged according to the normal document flow and are not affected by top/right/bottom/left. | View Example |
| relative | Offset relative to its own original position, and it does not leave the document flow. | View Example |
| absolute | Positioned relative to the nearest positioned ancestor element (otherwise relative to the body), and it leaves the document flow. | View Example |
| fixed | Positioned relative to the browser window; the position does not change when the page is scrolled. | View Example |
| sticky | Before scrolling to the specified position, it behaves as relative; after that, it behaves as fixed. | View Example |
Elements can betop、right、bottomandleftAdjust the position with these properties, but these properties only take effect whenposition(and the value is notstatic) is set.
Static positioning
The default value for HTML elements, i.e., no positioning, follows the normal document flow object.
Statically positioned elements are not affected by top, bottom, left, right.
Fixed positioning
The element's position is fixed relative to the browser window.
It will not move even if the window is scrolled:
Note:Fixed positioning requires a !DOCTYPE declaration to be supported in IE7 and IE8.
Fixed positioning makes the element's position irrelevant to 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
Try it Yourself »
Moving a relatively positioned element does not change the space it originally occupied.
Relatively positioned elements are often used as containing blocks for absolutely positioned elements.
Absolute positioning
The position of an absolutely positioned element is relative to its nearest positioned parent element. If the element has no positioned parent, then its position is relative to <html>:
Absolute positioning makes the element's position irrelevant to the document flow, so it does not occupy space.
Absolutely positioned elements overlap with other elements.
Sticky positioning
The English literal meaning of sticky is "stick" or "adhere", so it can be called sticky positioning.
position: sticky;Position based on the user's scroll position.
Sticky positioned elements depend on the user's scroll, and whenposition:relativeandposition:fixedSwitch between positions.
It behaves just likeposition:relative;And when the page scrolls beyond the target area, its behavior is likeposition:fixed;, it will be fixed at the target position.
The element's positioning behaves as relative before crossing a specific threshold, and as fixed after that.
This specific threshold refers to one of top, right, bottom, or left. In other words, sticky positioning takes effect only when one of the four thresholds top, right, bottom, or left is specified. 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 example below).
Example
Try it Yourself »
Overlapping Elements
The positioning of elements is irrelevant to 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 in front, or behind).
An element can have a positive or negative stacking order:
Elements with a higher stacking order are always in front of elements 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 in front.
More Examples
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 content that overflows inside an element
This example demonstrates how the overflow property creates a scrollbar to accommodate when an element's content is too large for the specified area.
How to set automatic overflow handling in the browser
This example demonstrates how to set the browser to automatically handle overflow.
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 | Clip an absolutely positioned element | shape auto inherit |
2 |
| cursor | Display 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 |
Specify 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 on the right/left edge. | 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 |