CSS Position
The position property controls how an element is placed in the document. Combined with top, right, bottom, left, and z-index, it lets you pull boxes out of the normal flow.
Five positioning modes
What each value really does
| Value | Anchored to | Takes space? |
|---|---|---|
static | Normal flow (default). | Yes |
relative | Itself — moves visually but keeps its slot. | Yes (original slot) |
absolute | Nearest positioned ancestor (or the viewport). | No |
fixed | The viewport — stays put when scrolling. | No |
sticky | Acts relative until it would scroll off, then fixed. | Yes |
Note: For
position: absolute to anchor against a parent, that parent needs position: relative (or anything other than static).Example
Example
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Verdana, sans-serif; }
.box { background: #04AA6D; color: #fff; padding: 20px; border-radius: 6px; }
</style>
</head>
<body>
<h1>CSS Position</h1>
<div class="box">Edit the CSS on the left, then click Run »</div>
</body>
</html>
Try it Yourself »
Exercise
Pin this navbar to the top of the viewport.
.navbar { position:
; top: 0; }
It does not move when the page scrolls.
Discussion
Loading…