🔲 CSS Display & Positioning

Master display types and positioning methods for complete layout control

← Back to Content Next Topic: Floating & Clear →

🎯 Why Display & Positioning Matter

The display and position properties are fundamental to CSS layout. They control how elements behave in the document flow, how they interact with other elements, and where they appear on the page.

Key Layout Control:

  • ✅ Control element behavior (block vs inline)
  • ✅ Create complex layouts and designs
  • ✅ Position elements precisely on the page
  • ✅ Create overlapping elements and layers
  • ✅ Build navigation bars, modals, and tooltips

📺 Display Property

The display property specifies how an element is displayed in the document.

1. Block

/* Block-level elements */
div {
    display: block;
}

/* Characteristics:
   - Takes full width available
   - Starts on a new line
   - Can have width and height
   - Examples: div, p, h1-h6, section, article
*/

Block Elements:

Block Element 1 (takes full width)
Block Element 2 (starts on new line)

2. Inline

/* Inline elements */
span {
    display: inline;
}

/* Characteristics:
   - Only takes up necessary width
   - Stays on the same line
   - Cannot have width and height
   - Examples: span, a, strong, em, img
*/

Inline Elements:

Inline 1 Inline 2 Inline 3 (They stay on the same line)

3. Inline-Block

/* Inline-block elements */
.box {
    display: inline-block;
    width: 100px;
    height: 100px;
}

/* Characteristics:
   - Flows inline like text
   - Can have width and height
   - Best of both worlds!
*/

Inline-Block Elements:

Box 1
Box 2
Box 3

4. None

/* Hide element completely */
.hidden {
    display: none;  /* Removes from document flow */
}

/* Compare with visibility */
.invisible {
    visibility: hidden;  /* Hidden but takes up space */
}

5. Flex

/* Flexbox container */
.container {
    display: flex;
}

/* Creates flexible layout
   - Powerful alignment options
   - Responsive by nature
   - Modern layout technique
*/

6. Grid

/* Grid container */
.container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}

/* Creates two-dimensional grid layout
   - Rows and columns
   - Complex layouts made easy
*/

📍 Position Property

The position property specifies how an element is positioned in the document.

1. Static (Default)

/* Default positioning */
div {
    position: static;
}

/* Characteristics:
   - Default value
   - Follows normal document flow
   - top, right, bottom, left have no effect
*/

2. Relative

/* Relative positioning */
.box {
    position: relative;
    top: 20px;     /* Move down 20px */
    left: 30px;    /* Move right 30px */
}

/* Characteristics:
   - Positioned relative to its normal position
   - Original space is preserved
   - Can use top, right, bottom, left
   - Creates positioning context for children
*/

Relative Positioning Example:

Normal
Relative (moved)
Normal

3. Absolute

/* Absolute positioning */
.parent {
    position: relative;  /* Create positioning context */
}

.child {
    position: absolute;
    top: 0;
    right: 0;
}

/* Characteristics:
   - Removed from normal document flow
   - Positioned relative to nearest positioned ancestor
   - If no positioned ancestor, relative to document body
   - Does not leave space in layout
*/

Absolute Positioning Example:

Parent (position: relative)
Absolute (top-right)
Absolute (bottom-left)

4. Fixed

/* Fixed positioning */
.navbar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: white;
    z-index: 1000;
}

/* Characteristics:
   - Removed from normal document flow
   - Positioned relative to viewport
   - Stays in place when scrolling
   - Perfect for navigation bars
*/

5. Sticky

/* Sticky positioning */
.header {
    position: sticky;
    top: 0;
    background: white;
}

/* Characteristics:
   - Hybrid of relative and fixed
   - Acts relative until scroll threshold
   - Then becomes fixed
   - Great for table headers and section titles
*/

🗂️ Z-Index (Stacking Order)

The z-index property controls the stacking order of positioned elements.

/* Z-index controls layering */
.bottom {
    position: relative;
    z-index: 1;
}

.middle {
    position: relative;
    z-index: 2;
}

.top {
    position: relative;
    z-index: 3;
}

/* Higher z-index = appears on top
   Only works on positioned elements (not static)
   Default z-index is auto (0)
*/

Z-Index Stacking:

z-index: 1
z-index: 2
z-index: 3 (on top)

💼 Practical Real-World Examples

Example 1: Centered Modal/Dialog

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
}

.modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 30px;
    border-radius: 10px;
    z-index: 1001;
}

Example 2: Sticky Navigation

nav {
    position: sticky;
    top: 0;
    background: white;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    z-index: 100;
}

Example 3: Badge/Notification Icon

.icon-wrapper {
    position: relative;
    display: inline-block;
}

.badge {
    position: absolute;
    top: -8px;
    right: -8px;
    background: red;
    color: white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
}

Example 4: Card Grid Layout

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
}

.card {
    display: block;
    padding: 20px;
    border: 1px solid #ddd;
}

Example 5: Tooltip

.tooltip-wrapper {
    position: relative;
    display: inline-block;
}

.tooltip {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 8px 12px;
    border-radius: 4px;
    white-space: nowrap;
    display: none;
}

.tooltip-wrapper:hover .tooltip {
    display: block;
}

🎯 Common Use Cases Summary

When to Use Each Position Value:

  • static: Default, no special positioning needed
  • relative: Small adjustments, create positioning context for children
  • absolute: Tooltips, dropdowns, overlays, badges, precise placement
  • fixed: Navigation bars, floating buttons, chat widgets, cookie notices
  • sticky: Table headers, section titles, navigation that sticks on scroll

When to Use Each Display Value:

  • block: Full-width sections, containers, main content areas
  • inline: Text styling, small inline elements
  • inline-block: Navigation items, buttons in a row, image galleries
  • none: Hide elements, toggle visibility
  • flex: Modern layouts, alignment, distribution
  • grid: Complex two-dimensional layouts

🎨 Live Examples

Display Types:

Block (full width)
Inline 1 Inline 2

Position Relative + Absolute:

Parent Container (relative)
🎯 Positioned Top-Right

📝 Test Your Knowledge - MCQ

Q1: Which display value takes full width and starts on a new line?

Q2: Which position value is used for navigation that sticks while scrolling?

Q3: What does display: none do?

Q4: Position absolute is positioned relative to:

Q5: Which property controls the stacking order of elements?

💪 Practice Questions

  1. Create three inline-block boxes that sit side by side, each 150px wide and 100px tall.
  2. Position a "badge" element at the top-right corner of a parent container using absolute positioning.
  3. Create a fixed navigation bar that stays at the top of the page when scrolling.
  4. Use position: relative to move an element 20px down and 30px right from its normal position.
  5. Create three overlapping boxes with different z-index values (1, 2, 3) to control their stacking order.

📌 Summary

  • display: block: Full width, starts on new line
  • display: inline: Flows with text, no width/height
  • display: inline-block: Flows inline but can have width/height
  • display: none: Completely removes from layout
  • display: flex: Modern flexible layout system
  • display: grid: Two-dimensional grid layout
  • position: static: Default, normal flow
  • position: relative: Relative to normal position, keeps space
  • position: absolute: Relative to positioned ancestor, removes from flow
  • position: fixed: Relative to viewport, stays on scroll
  • position: sticky: Relative until scroll threshold, then fixed
  • z-index: Controls stacking order (higher = on top)
← Back to Content Next Topic: Floating & Clear →