CSS Positioning

📍 Positioning

Position Relative Absolute Fixed Sticky Offset Properties Inset-shorthand Logical Properties Z-index

Assignment

Assignment 1 Assignment 2

1. position – Positioning Methods


📝 Definition: Sets the positioning method used for an element. Determines whether an element is positioned using CSS properties like top, right, bottom, and left. Different position values change how these offset properties work and how the element interacts with other elements in the document. Understanding the 5 position values is fundamental to CSS layout.

Default Value: static

Values: static | relative | absolute | fixed | sticky | inherit

✦ position: static (Default)

Normal document flow - offset properties are ignored

position: static (default positioning)
.static-element {
  position: static;
  /* Offset properties (top, left) don't work */
}

✦ Summary of All Values

2. position: relative


📝 Definition: Positions an element relative to its normal position in the document flow. The element still takes up its original space, and offset properties (top, right, bottom, left) move it from that position. Useful for slight adjustments and creating stacking contexts without removing elements from normal flow.

Default Value: (offset properties default to auto)

Offset Properties: top | right | bottom | left (values can be positive or negative)

✦ Relative with Top and Left

Element moved 20px down and 30px right from original position
Relative positioned
Original position ↑↑↑ (still takes up this space)
.relative {
  position: relative;
  top: 20px;
  left: 30px;
}

3. position: absolute


📝 Definition: Positions an element relative to its nearest positioned ancestor (not static). The element is removed from normal flow. Perfect for tooltips, dropdowns, modals, and overlays that should be positioned independently. If no positioned parent exists, it positions relative to the document body.

Important: Element is removed from normal document flow

Common Uses: Tooltips, dropdowns, modals, overlays

✦ Absolute Positioned Child

Element positioned inside a relative parent container
Parent (position: relative)
absolute box
.parent {
  position: relative;
  height: 150px;
}

.child {
  position: absolute;
  top: 25px;
  left: 30px;
}

4. position: fixed


📝 Definition: Fixes an element relative to the viewport (browser window), not the document. The element stays in the same position even when scrolling. Fixed positioning removes the element from normal flow. Perfect for navigation bars, floating action buttons, chat widgets, and sticky headers that need to stay visible while scrolling.

Important: Element is fixed relative to viewport, not parent

Common Uses: Navigation bars, sticky headers, floating buttons, chat widgets

✦ Fixed Positioned Element

Element stays fixed on screen even when scrolling

Normal scrollable content area. A fixed element would stay in place while this scrolls.

Content line 2
Content line 3
.fixed-button {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: #667eea;
  color: white;
  padding: 15px 20px;
  border-radius: 8px;
  z-index: 1000;
}

✦ Fixed Navigation Bar

Simulating a fixed navigation bar at the top

Logo
Home About Contact
This stays visible while scrolling content below
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: gradient;
  z-index: 999;
}

✦ Fixed Chat Widget

Simulating a fixed chat bubble in corner

💬
Chat widget in fixed position (bottom-right)
.chat-widget {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 60px;
  height: 60px;
  background: #f5576c;
  border-radius: 50%;
}

5. position: sticky


📝 Definition: Hybrid positioning that starts in normal flow but becomes fixed relative to a parent container when scrolling. The element "sticks" to a specified position (top, bottom, left, right) when that edge enters the viewport. Perfect for sticky table headers, section navigation, and headers that should stick while scrolling content. Requires a defined offset property to work.

Important: Must have top/bottom/left/right values defined and stay within parent bounds

Common Uses: Table headers, section headers, navigation tabs

✦ Sticky Header Example

Simulating sticky behavior with content

Section Header (Sticks to Top)
Item 1
Item 2
Item 3
Item 4
Item 5 (scroll to see sticky effect)
.sticky-header {
  position: sticky;
  top: 0;
  background: gradient;
  z-index: 10;
}

✦ Sticky with Bottom Offset

Element sticks to bottom when scrolling

Line 1
Line 2
Line 3
Sticky Bottom Footer
.sticky-footer {
  position: sticky;
  bottom: 0;
  background: gradient;
}

6. Offset Properties (top, right, bottom, left)


📝 Definition: Offset properties determine the position of an element relative to its reference point. The reference point depends on the position value (relative, absolute, fixed, etc.). These properties accept length values (pixels, em, rem, etc.), percentages, or auto. They can be positive or negative, allowing elements to move in any direction from their reference position.

Values: length (px, em, rem) | percentage (%) | auto

Properties: top | right | bottom | left

✦ Individual Offset Properties

Each offset property controls movement from one side
top: 10px, left: 10px
top: 10px, right: 10px
bottom: 10px, left: 10px
bottom: 10px, right: 10px
.top-left {
  position: absolute;
  top: 10px;
  left: 10px;
}

✦ Offset Stretching (Full Coverage)

All four offsets set to 0 - element stretches to fill parent
Stretched to Fill Parent
.stretch-all {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

7. inset Shorthand (top + right + bottom + left)


📝 Definition: Shorthand property that sets all four offset properties (top, right, bottom, left) at once. Follows the standard CSS margin/padding shorthand syntax: inset: 10px (all), inset: 10px 20px (vertical, horizontal), or inset: 10px 20px 30px 40px (top, right, bottom, left). Modern browsers support this property for cleaner, more concise code when positioning absolutely positioned or fixed elements.

Syntax: inset: [value] | [vertical] [horizontal] | [top] [right] [bottom] [left]

Related To: top | right | bottom | left

✦ Single Value (All Sides)

inset: 20px - all offsets set to 20px
inset: 20px
.inset-all {
  position: absolute;
  inset: 20px;
  /* Equivalent to top: 20px; right: 20px; bottom: 20px; left: 20px; */
}

✦ Two Values (Vertical, Horizontal)

inset: 10px 30px - (top/bottom: 10px, left/right: 30px)
inset: 10px 30px
.inset-vertical-horizontal {
  position: absolute;
  inset: 10px 30px;
  /* Equivalent to top: 10px; right: 30px; bottom: 10px; left: 30px; */
}

8. Logical Properties – Direction-Aware Positioning


📝 Definition: Logical properties provide direction-agnostic positioning that adapts to writing direction (LTR vs RTL languages). Instead of absolute directions (top, left, right, bottom), they use logical directions (block-start, block-end, inline-start, inline-end). This ensures layouts work correctly in different languages without requiring separate CSS. Essential for building truly multilingual, accessible websites.

Logical Properties: inset-inline-start | inset-inline-end | inset-block-start | inset-block-end

Note: Modern browsers have good support; currently maps to standard properties in most implementations

✦ Logical Direction Mapping (LTR)

In left-to-right languages, logical properties map as shown:

✦ Logical Positioning Example

Using logical properties for positioning
Logical Position
.logical-position {
  position: absolute;
  inset-block-start: 15px; /* = top in LTR */
  inset-inline-start: 25px; /* = left in LTR */
}

9. z-index – Stacking Order (Layer Control)


📝 Definition: Controls the stacking order of positioned elements along the z-axis (depth). Elements with higher z-index values appear on top of those with lower values. Only works on positioned elements (position not static). Creates stacking contexts that can affect nested elements. Essential for managing overlays, modals, dropdowns, and complex layered layouts.

Default Value: auto (0)

Values: number (integer) | auto | inherit

Important: Works only on positioned elements (position: relative, absolute, fixed, sticky)

✦ Z-index: Layering Elements

Elements with different z-index values demonstrating stacking

z: 1
z: 3
z: 2
.behind {
  position: absolute;
  z-index: 1;
  background: #667eea;
}

.middle {
  position: absolute;
  z-index: 2;
  background: #764ba2;
}

.front {
  position: absolute;
  z-index: 3;
  background: #f093fb;
}

✦ Modal Overlay Pattern

Typical z-index layering for modals

Page content (z-index: auto)

Modal Dialog

This appears on top (z-index: 1000)

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0,0,0,0.5);
  z-index: 990;
}

.modal {
  position: fixed;
  z-index: 1000;
  /* Appears above overlay */
}

✦ Typical Z-index Scale