P2.4: Absolute & Sticky Positioning
Sizing element boxes is only the first step. To build professional webpages, you must master how to position those boxes on the screen. In this lesson, we will cover CSS positioning schemes and floating behaviors.
Sizing element boxes is only the first step. To build professional webpages, you must master how to position those boxes on the screen. In this lesson, we will cover CSS positioning schemes and floating behaviors.
Lesson: p2-4-positioning
Sizing element boxes is only the first step. To build professional webpages, you must master how to position those boxes on the screen. In this lesson, we will cover CSS positioning schemes and floating behaviors.
The position property determines how the browser calculates the physical coordinates of an element box. It uses box offset properties (top, bottom, left, right) to move elements from their default positions.
position: static)The default positioning. Elements render in their natural source order. Block elements start on a new line, and inline elements flow next to each other. Box offset properties (top, left, etc.) have no effect on static elements.
position: relative)Moves an element relative to its natural position in normal flow.
.moved-box {
position: relative;
top: 10px; /* Moves the box 10px down from its natural top edge */
left: 20px; /* Moves the box 20px right from its natural left edge */
}
position: absolute)Positions the element relative to its nearest positioned ancestor (any ancestor element that has a position other than static, such as position: relative).
/* Container must be positioned relative to act as coordinate root */
.container {
position: relative;
}
.close-button {
position: absolute;
top: 12px;
right: 12px;
}
position: fixed)Positions the element relative to the browser viewport.
.bottom-nav {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
}
position: sticky)A modern hybrid of relative and fixed. The element behaves like position: relative until it reaches a specified scroll threshold, where it "sticks" and behaves like position: fixed.
.table-header {
position: sticky;
top: 0; /* Sticks to the top of the viewport when scrolled to */
}
Positioning rules form the structural foundation of headers, sidebars, popups, and dropdown menus.
Average reduction in layout shifts after switching to modern positioning.
z-index)When you use relative, absolute, fixed, or sticky positioning, elements can overlap. By default, elements that appear later in the HTML source code stack on top of earlier ones.
To explicitly control this vertical stacking order, use the z-index property.
z-index only works on positioned elements (elements with position set to relative, absolute, fixed, or sticky)..sidebar {
position: fixed;
z-index: 100; /* Sits on top of main content */
}
.modal-overlay {
position: fixed;
z-index: 999; /* Sits on top of everything, including sidebar */
}
float)Takes an element out of normal flow and pushes it as far left or right of its containing block as possible. Text and other inline elements flow (wrap) around the floated box.
left, right, none.width (otherwise, the box collapses to fit its content, and adjacent text may not wrap cleanly)..thumbnail {
float: left;
width: 150px;
margin-right: 15px;
}
clear)Prevents subsequent elements from wrapping around floated boxes. It forces the element to move down below the floated box.
left (clears left floats), right (clears right floats), both (clears both directions)..footer {
clear: both; /* Forces footer to sit below floated sidebar and content blocks */
}
If a parent container only contains floated child elements, the browser treats the parent as if its height is zero. The border and background of the parent collapse, causing child elements to spill out.
To force the parent container to automatically wrap around all of its floated children, add these rules to the parent:
.container {
overflow: auto; /* Triggers the browser to calculate container height including floats */
width: 100%; /* Ensures compatibility across browsers */
}