CSS3 provides powerful tools to control how elements are positioned on a web page. Understanding the different position values is essential for building consistent, responsive layouts. Below is an overview of the five main positioning types in CSS: static, relative, absolute, fixed, and sticky.
Component "PositionVisualizer" is not available. Please define it in the MDX components.
By default, all elements have position: static. This means they flow naturally in the document, from top to bottom, left to right, according to the HTML structure. They are not affected by top, right, bottom, or left properties.
div {
position: static;
}
position: relative keeps the element in the normal flow but allows you to offset it from its original position using top, right, bottom, and left.
div {
position: relative;
top: 10px;
left: 20px;
}
position: absolute removes the element from the normal document flow and positions it relative to the nearest ancestor that has a position other than static. If no such ancestor exists, it’s positioned relative to the <html> element.
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
left: 0;
}
position: fixed also removes the element from the flow, but it positions the element relative to the browser window (viewport). The element stays in the same place even when the user scrolls.
div {
position: fixed;
bottom: 0;
right: 0;
}
position: sticky is a hybrid of relative and fixed. The element behaves as relative until a specific scroll threshold is reached, then it switches to fixed.
div {
position: sticky;
top: 0;
}
top, left, etc.); parent container must have a height.| Position Type | Participates in Layout Flow | Offsets Allowed | Reference Context |
|---|---|---|---|
| static | Yes | No | N/A |
| relative | Yes | Yes | Self |
| absolute | No | Yes | Nearest positioned ancestor |
| fixed | No | Yes | Viewport |
| sticky | Yes → No (on scroll) | Yes | Scroll container |
Choosing the right CSS positioning strategy depends on the layout needs of your project. Mastering these five values—static, relative, absolute, fixed, and sticky—will help you build flexible and reliable web interfaces.