Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 4: Layouts, Positioning & Modern Flexbox
CSS ⏱ 12 min read

Display and Positioning Modes

Controlling how elements flow on the page and pinning them into exact coordinates is fundamental to UI architecture.

What is it?

The `display` property determines an element's flow behavior (`block`, `inline`, `inline-block`, `none`, `flex`, `grid`). The `position` property defines positioning mechanics (`static`, `relative`, `absolute`, `fixed`, `sticky`).

Why is it used?

Used to create sticky navigation headers that stay at the top when scrolling, floating modal dialogs, badges over avatars, and hidden dropdown menus.

Detailed Explanation

Positioning Modes: • `static` (default): Follows normal document flow. • `relative`: Positioned relative to its normal position. Crucially, acts as the anchor point for child absolute elements. • `absolute`: Removed from normal flow and positioned relative to its closest positioned ancestor (or page body). • `fixed`: Positioned relative to the browser viewport. Never moves when the user scrolls. • `sticky`: Toggles between relative and fixed depending on scroll position.

Syntax Structure

Syntax Blueprint
position: relative; /* Anchor */

.badge {
  position: absolute;
  top: -8px;
  right: -8px;
}

Code Example

CSS Code
/* Sticky Navbar */
.sticky-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: rgba(247, 244, 237, 0.95);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid #eceae4;
  padding: 14px 24px;
}

/* Notification Icon with Absolute Badge */
.icon-wrapper {
  position: relative;
  display: inline-block;
}

.notification-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  background-color: #ef4444;
  color: #ffffff;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 9999px;
}

Explanation of the Code

• `position: sticky; top: 0;` locks the navbar to the viewport once scrolled. • `position: relative` on `.icon-wrapper` ensures `.notification-badge` with `position: absolute` stays glued to the corner of that specific icon.

Expected Output

A sticky navbar that sticks to top on scroll, and an icon with a red counter badge pinned to its top-right corner.

Real-World Application

Used for shopping cart counter badges, floating 'Back to Top' buttons, and sticky navigation headers.

Common Mistakes to Avoid

• Using `position: absolute` without setting `position: relative` on the parent container (badge flies to the top corner of the entire window). • Overusing high `z-index` values (`z-index: 99999999`) creating stacking context chaos.

Quick Practice Challenge

Pin a 'NEW' badge to the top-right corner of a card using `position: relative` on card and `position: absolute` on badge.

Key Takeaways

• Use `position: sticky; top: 0;` for modern sticky navbars. • Always pair an `absolute` child with a `relative` parent container. • Use `z-index` to control layering along the depth axis.