Layout
position
position sets whether an element stays in the normal flow, is nudged from there, or is taken out of the flow and placed where you choose. The place is set with top, right, bottom and left.
Syntax
static | relative | absolute | sticky | fixedPreview
1234
Presets
Sample
Drag the bottom-right corner to resize
Code
.box {
display: flex;
position: relative;
}
.box > :nth-child(2) {
bottom: 24px;
right: 24px;
position: absolute;
}Controls
positionabsolute
1234
About
What the values mean
static: the initial value. The element stays in the normal flow andtopand the like have no effect.relative: shifts the element from its normal place bytopand so on. Its original spot stays reserved; nothing moves in to fill it.absolute: takes the element out of the flow and places it inside its containing block. Other elements close the gap. The containing block is the nearest ancestor whose position is not static (or a box the size of the first screen of the page if there is none).fixed: takes the element out of the flow and places it relative to the browser window (viewport). It stays put when the page scrolls.sticky: the element sits in its normal place until scrolling brings it to the given offset inside a scrolling box, then it sticks there. Used for table headers, tables of contents and the like.
The preview here
The box has position: relative so it becomes the containing block, and the second item has bottom: 24px and right: 24px. With absolute the item sits 24px in from the box’s bottom right corner; with relative it moves 24px up and left from its own spot. Pick fixed and the item leaves the preview for the bottom right of the screen. sticky stays in place because this box doesn’t scroll.
Watch out
absoluteandfixedelements shrink to fit their content. Even a block no longer fills the width, and item 2 here loses the height it was stretched to in the flexbox.- Elements that are not static are painted in front of ordinary ones. Change the order with
z-index. - If an ancestor has
transformorfilter, afixedelement is placed relative to that ancestor instead of the screen. stickyneeds at least one oftopand the like to stick. An ancestor withoverflow: hiddenor similar becomes the scrolling box it sticks in, so it won’t stick when the page scrolls.