Info
Demo 2.6The
The position property
position decides what an element’s top, right, bottom and left actually mean ~ and whether it stays in the page’s normal flow at all.
The five values
static~ the default. Normal flow.top/leftdo nothing.relative~ nudged from where it would have been. Its original space is still reserved, so nothing else moves.absolute~ removed from the flow, positioned against the nearest ancestor that isn’tstatic.fixed~ removed from the flow, pinned to the browser window. Doesn’t scroll.sticky~ normal until you scroll past it, then it sticks. Needs atopvalue to work.
The trick that makes absolute make sense
absolute positions against the nearest positioned ancestor ~ meaning the nearest one with a position other than static. If there isn’t one, it goes all the way up to the page itself, which is rarely what you wanted.
So the pattern is: position: relative on the parent (with no top/left, so it doesn’t actually move), position: absolute on the child. The parent exists purely to be the thing the child measures from.
A warning
If you’re positioning things to build a layout, stop ~ you want Flexbox or Grid. position is for the exceptions: a badge in a corner, a dropdown, a sticky header.
Keyboard Shortcuts
⌘ Z / ⌘ ⇧ ZCtrl Z / Ctrl ⇧ Z
Undo / redo
Tab / ⇧ TabTab / ⇧ Tab
Indent / unindent selection
⌘ ] / ⌘ [Ctrl ] / Ctrl [
Indent / unindent lines
⌘ ACtrl A
Select all
⌘ DCtrl D
Select next occurrence of selection
⌘ /Ctrl /
Comment / uncomment lines
Ctrl SpaceCtrl Space
Autocomplete
Ctrl EnterCtrl Enter
Reset layout
⌘ ⌥ [ / ⌘ ⌥ ]Ctrl ⇧ [ / Ctrl ⇧ ]
Fold / unfold block
Code
The editor didn't load — try refreshing the page. Navigation,
shortcuts and the info panel still work in the meantime.
Settings
View
Font size
14
Code
Preview
Delay
Zoom
Share
Reset
Preview
Currently previewing:
Dimensions0 × 0
Delay
Preview paused
Click to resume
Click to resume
The preview is disabled for this demo.
Use the console below.
Use the console below.
Console
›