← Back to CSS Course | Chapter 11: Grid | Lesson 8 of 10

CSS Grid Item Align

justify-self और align-self justify-items और align-items पूरे container के लिए जो भी set करते हैं उसके single-item exceptions हैं।
Syntax
css
item {
  justify-self: start | center | end | stretch;
  align-self: start | center | end | stretch;
}

Item-Level Overrides क्यों मौजूद हैं

justify-items और align-items grid के हर item के लिए एक default alignment set करते हैं, लेकिन justify-self और align-self एक specific item को अपने siblings को असर किए बिना उस default से हटने देते हैं।

Note:
  • Accepted values:
  • justify-self — start | end | center | stretch
  • align-self — start | end | center | stretch | baseline
  • place-self — align फिर justify का shorthand
  • auto — container की setting inherit करता है (default)

उदाहरण: Why Item-Level Overrides Exist

css
<style>
.container {
  display: grid;
  justify-items: start;
  background: #eee;
  padding: 10px;
  gap: 10px;
}
.container div {
  background: lightblue;
  padding: 8px;
}
.special {
  justify-self: end;
  background: coral;
}
</style>
<div class="container">
  <div>Default</div>
  <div class="special">Overridden</div>
</div>

justify-self: Horizontal Override

justify-self justify-items जैसी ही values accept करता है (start, end, center, stretch) लेकिन सिर्फ उस single item पर लागू होता है जहाँ यह declared है, container level पर justify-items ने जो भी set किया हो उसे override करते हुए।

Note:
  • Accepted values:
  • auto — default, container की justify-items इस्तेमाल करता है
  • normal — grid items के लिए stretch जैसा behave करता है
  • start | end | center | left | right — horizontal positions
  • stretch — cell भरता है
  • baseline — baselines align करता है
  • self-start | self-end — logical alignment
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: justify-self: Horizontal Override

css
<style>
.container {
  display: grid;
  background: #eee;
  padding: 10px;
}
.item {
  justify-self: center;
  background: coral;
  padding: 8px;
}
</style>
<div class="container">
  <div class="item">Centered horizontally, only this item</div>
</div>

align-self: Vertical Override

align-self vertical equivalent है, container पर align-items को छुए बिना एक item को अपने siblings से अलग vertical axis पर align होने देते हुए।

Note:
  • Accepted values:
  • auto — default, container की align-items इस्तेमाल करता है
  • normal — grid items के लिए stretch जैसा behave करता है
  • start | end | center — vertical positions
  • stretch — cell भरता है
  • baseline — baselines align करता है
  • self-start | self-end — logical alignment
  • inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords

उदाहरण: align-self: Vertical Override

css
<style>
.container {
  display: grid;
  height: 150px;
  background: #eee;
}
.item {
  align-self: end;
  background: coral;
  padding: 8px;
}
</style>
<div class="container">
  <div class="item">Aligned to the bottom, only this item</div>
</div>

Common इस्तेमाल: एक Item को किसी Corner पर Pin करना

एक common pattern एक single item पर justify-self: end को align-self: start के साथ combine करना है, इसे इसके cell के top-right corner पर pin करते हुए जबकि बाकी हर item normal container alignment follow करता है।

Note:
  • Accepted values:
  • justify-self: end — cell के right edge पर pin करता है
  • align-self: end — cell के bottom edge पर pin करता है
  • place-self: end — दोनों एक साथ
  • place-self: <align> <justify> — shorthand

उदाहरण: Common Use: Pinning One Item to a Corner

css
<style>
.container {
  display: grid;
  height: 150px;
  background: #eee;
}
.badge {
  justify-self: end;
  align-self: start;
  background: coral;
  padding: 8px;
}
</style>
<div class="container">
  <div class="badge">Pinned to the top-right corner</div>
</div>

Container Defaults पर Precedence

जब एक container-level property (जैसे align-items: stretch) और एक item-level self property (जैसे align-self: center) दोनों एक ही item पर लागू होते हैं, तो item-level self property हमेशा जीतती है।

Note:
  • Accepted values:
  • align-self — align-items को override करता है
  • justify-self — justify-items को override करता है
  • place-self — दोनों का shorthand
  • auto — container पर defer करता है

उदाहरण: Precedence Over Container Defaults

css
<style>
.container {
  display: grid;
  align-items: stretch;
  height: 150px;
  background: #eee;
}
.item {
  align-self: center;
  background: coral;
  padding: 8px;
}
</style>
<div class="container">
  <div class="item">align-self wins over the container's align-items</div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. container पर justify-self set करना, जबकि यह item पर होना चाहिए।
  2. यह उम्मीद करना कि align-self किसी flex layout पर grid जैसी ही तरह काम करेगा, जबकि यह एक cross axis पर काम करता है।
  3. यह भूल जाना कि item alignment कुछ नहीं करता अगर item अपना पूरा grid area भर देता है।
ब्राउज़र सपोर्ट

CSS Grid Chrome 57+, Firefox 52+, Safari 10.1+ और Edge 16+ में supported है।

🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.