position PropertyThe position property in CSS is fundamental for controlling layout, stacking, and advanced UI effects. It determines how an element is placed in the document, how it interacts with other elements, and how it responds to scrolling and resizing. Understanding each value is key to mastering modern web design.
position: relative for layout tweaks and as a positioning context for children.absolute and fixed for overlays, tooltips, and UI elements that must float above content.z-index to manage stacking order, but avoid excessive stacking contexts./* Overlay Example */
.overlay-demo {
position: relative;
}
.overlay-demo .overlay {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(52,152,219,0.15);
display: flex; align-items: center; justify-content: center;
z-index: 2;
}
/* Tooltip Example */
.tooltip-demo {
position: relative;
}
.tooltip-demo .tooltip {
position: absolute;
bottom: 120%; left: 50%;
transform: translateX(-50%);
background: #23272e; color: #fff;
padding: 6px 14px; border-radius: 6px;
font-size: 0.98rem;
opacity: 0; pointer-events: none;
transition: opacity 0.2s;
z-index: 3;
}
.tooltip-demo:focus-within .tooltip,
.tooltip-demo:hover .tooltip {
opacity: 1;
pointer-events: auto;
}
/* Sticky Header Example */
.sticky-header {
position: sticky;
top: 0;
background: #3498db;
color: #fff;
padding: 10px 0;
text-align: center;
font-weight: bold;
z-index: 10;
}
Scroll down to see the header stick to the top of this card. The sticky header will highlight when it becomes fixed.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque euismod, nisi eu consectetur.
Aliquam erat volutpat. Etiam nec velit nec nulla dictum dictum. Suspendisse potenti.
Vivamus euismod, urna eu tincidunt consectetur, nisi nisl aliquam nunc, eget aliquam nisl nunc eu nisl.
More content to enable scrolling...
Even more content to enable scrolling...
position: absoluteTry scrolling, resizing, and interacting with the demos to see how position changes layout and behavior.