position
Chooses how an element is positioned: in the flow, relative, absolute, fixed, or sticky.
- Initial value
static- Inherited
- No
- Animatable
- no
- Applies to
- all elements
- JavaScript
element.style.position = "absolute"
Example
#.card {
position: relative;
}
.card .badge {
position: absolute;
top: 12px;
right: 12px;
}<div class="card stage" style="height: 110px;">
<strong>HTML and CSS course</strong>
<p>11 modules, 700 exercises</p>
<span class="badge box">New</span>
</div>Definition and usage
#The position property decides how an element’s location is calculated. By default (static), the element sits wherever it falls in the page’s normal flow, and properties like top and left do nothing.
relative shifts the element relative to its normal spot, which stays reserved for it. absolute takes the element out of the flow and positions it relative to the nearest positioned ancestor. fixed pins the element to the browser window, so it doesn’t scroll with the page.
sticky combines both behaviors: the element flows normally until scrolling brings it to a threshold set by, for example, top: 0. Then it sticks, but only within its parent.
Syntax
#position: static | relative | absolute | fixed | sticky;Values
#- Normal flow. The demo’s
topandleftare ignored. - The element shifts 30px down and to the right, and its original spot stays empty.
- The element drops out of the flow (element 3 takes its place) and positions itself relative to the positioned ancestor.
- The element pins itself to the window. Here, the window is the preview frame.
- Scroll the list in the preview: the header sticks to the top edge of its container.
Values in practice
#static
top and left are ignored..demo { position: static; }relative
.demo { position: relative; }absolute
.area { position: relative; }
.demo { position: absolute; }fixed
.demo { position: fixed; }sticky
.demo { position: sticky; top: 0; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- An element with
absolutepositions itself relative to the nearest ancestor whosepositionis something other thanstatic. The usual fix is addingposition: relativeto the parent. stickydoesn’t work when an ancestor hasoverflow: hidden, or when you don’t set a threshold, such astop: 0.
Related entries
#- topMoves a positioned element away from the top edge of its reference point.
- insetA shorthand that sets top, right, bottom, and left in a single declaration.
- z-indexDetermines which element sits on top when elements overlap.
- displayDetermines how an element behaves in the page layout and how it lays out its children.
- floatMoves an element to the left or right edge, and text wraps around it on the other side.
Spotted a mistake or missing an example? Write to us.