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
Syntax Structure
position: relative; /* Anchor */
.badge {
position: absolute;
top: -8px;
right: -8px;
}
Code Example
/* 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
Expected Output
Real-World Application
Used for shopping cart counter badges, floating 'Back to Top' buttons, and sticky navigation headers.
Common Mistakes to Avoid
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.