LearningCSS TutorialDisplay & Positioning
CHAPTER 08 · BEGINNER

CSS Display & Positioning

CSS me display decide karta hai element layout me kis tarah participate karega, aur position uski placement aur offsets ko control karta hai.

English + Hinglish24 min readPractice included

What does display do?

The display property controls an element's outer and inner layout behavior. Beginner level par block, inline, inline-block and none sabse important values hain.

display.cssCSS
.box {
  display: block;
}
Hinglish Explanation

Simple words me: browser ko batana ki element line ka kitna space lega, next element uske saath aa sakta hai ya nahi, aur width-height ka behavior kya hoga.

display: block

A block-level box normally starts on a new line and stretches across the available inline width when its width is auto.

block.cssCSS
.card {
  display: block;
  width: 320px;
  padding: 1rem;
}

Elements such as div, section and headings are block-level by default in normal HTML styling.

display: inline

Inline boxes flow with surrounding text and do not normally start a new line.

inline.cssCSS
.tag {
  display: inline;
  color: #0b7cff;
}
Important: Inline elements par normal width and height block boxes ki tarah behave nahi karte. Horizontal padding works visually, but vertical layout behavior can surprise beginners.

display: inline-block

inline-block lets an element sit inline while still accepting width and height like a box.

inline-block.cssCSS
.badge {
  display: inline-block;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
}
Use case

Badge, small label ya compact button jise text ke saath same line me rehna ho but proper padding/size chahiye, wahan inline-block useful hai.

display: none

display: none removes an element from the layout, so it takes no space.

none.cssCSS
.mobile-only {
  display: none;
}
Accessibility note: Content with display: none is also unavailable to normal assistive technology reading. Do not use it when content should remain accessible but only visually hidden.

display: none vs visibility: hidden

visibility: hidden hides the element visually but keeps its layout space. display: none removes its layout box.

visibility.cssCSS
.hidden-box {
  visibility: hidden;
}

.removed-box {
  display: none;
}

What does position do?

The position property changes how an element is placed and whether offset properties such as top, right, bottom and left affect it.

position.cssCSS
.notice {
  position: relative;
  top: 4px;
}

position: static

static is the normal default positioning for most elements. Offset properties do not move a statically positioned element.

static.cssCSS
.card {
  position: static;
}

position: relative

A relatively positioned element stays in normal flow, but can be visually offset from its original position. It also commonly establishes the containing block for absolutely positioned descendants.

relative.cssCSS
.card {
  position: relative;
}

.card-badge {
  position: absolute;
  top: 12px;
  right: 12px;
}
Most common pattern

Parent ko position: relative aur child ko position: absolute dene se child ko parent box ke reference me place karna easy ho jata hai.

position: absolute

An absolutely positioned element is removed from normal flow. Its position is calculated relative to its containing block, often the nearest positioned ancestor.

absolute.cssCSS
.card {
  position: relative;
}

.close-button {
  position: absolute;
  top: 8px;
  right: 8px;
}
Common mistake: Parent par position: relative na hone par absolute child unexpected ancestor ya initial containing block ke reference me move ho sakta hai.

position: fixed

A fixed element is generally positioned relative to the viewport and stays in the same place while the page scrolls.

fixed.cssCSS
.help-button {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
}

Fixed UI should not cover important content, especially on small screens or at high zoom.

position: sticky

A sticky element behaves like normal flow until a scroll threshold is reached, then it sticks within its scroll container.

sticky.cssCSS
.section-nav {
  position: sticky;
  top: 1rem;
}
Sticky needs an offset: Usually top is defined. Sticky behavior can also be affected by ancestor overflow and scroll containers.

Top, right, bottom and left

Offset properties position non-static elements according to their positioning context.

offsets.cssCSS
.badge {
  position: absolute;
  top: 10px;
  right: 10px;
}

Do not use many random offsets to repair a broken layout. Flexbox or Grid is usually better for normal page alignment.

The inset shorthand

inset is shorthand for top, right, bottom and left.

inset.cssCSS
.overlay {
  position: absolute;
  inset: 0;
}

This stretches the positioned box to all four edges of its containing block when sizing allows.

z-index and stacking

z-index helps control which positioned or stacking-context elements appear in front of others.

z-index.cssCSS
.header {
  position: sticky;
  top: 0;
  z-index: 10;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
}
Important

z-index: 999999 har problem ka solution nahi hai. Stacking context samajhna important hai, kyunki child ka high z-index bhi doosre stacking context ke bahar escape nahi karta.

Stacking context: beginner idea

A stacking context is like a local layering group. Certain CSS properties such as positioned elements with a non-auto z-index, transforms, opacity below 1 and others can create new stacking contexts.

Debugging tip: Agar z-index work nahi kar raha, sirf number increase karne ke bajay parent stacking contexts inspect karo.

Normal flow vs positioned layout

Normal document flow should handle most content. Absolute and fixed positioning are best for overlays, badges, floating controls and similar targeted cases—not for building the entire page layout.

Rule of thumb

Main page columns, rows aur alignment ke liye aage Flexbox/Grid use karenge. Positioning ko special placement ke liye rakho.

Accessibility and positioning

  • Fixed headers content ko hide na karein.
  • Zoom ke baad floating buttons text ko cover na karein.
  • Visual order aur keyboard/tab order ko unnecessarily mismatch mat karo.
  • Hidden content ke liye correct hiding technique choose karo.
  • Focus indicator ko overlay ke peeche mat chhupne do.
  • Sticky elements ko too tall mat banao on small screens.

Best practices

  • Normal flow ko default rakho.
  • Absolute child ke liye intentional positioned parent define karo.
  • Layout repair ke liye random top/left offsets avoid karo.
  • z-index scale consistent rakho instead of huge arbitrary numbers.
  • Fixed and sticky UI ko mobile + zoom par test karo.
  • DevTools me containing block and stacking context inspect karo.

Common beginner mistakes

  • inline element par width/height expect karna.
  • display: none aur visibility: hidden ko same samajhna.
  • Absolute child ka reference parent set na karna.
  • Entire layout absolute positioning se banana.
  • Sticky element ke saath top offset bhoolna.
  • Every z-index problem ko giant number se solve karna.
  • Fixed element se page content cover kar dena.

Practice Task

Ek “Course Card with Badge” banao aur display + positioning practice karo.

  1. Card ko block box ke roop me style karo.
  2. Category badge ko inline-block banao.
  3. Card parent ko position: relative do.
  4. “NEW” badge ko position: absolute karke top-right place karo.
  5. Ek help button ko page ke bottom-right me fixed karo.
  6. Small section navigation ko sticky with top offset se test karo.
  7. Two overlapping demo boxes bana kar z-index compare karo.
  8. display: none aur visibility: hidden ka layout difference observe karo.
  9. Browser zoom aur narrow viewport par fixed/sticky elements verify karo.