CSS Box Sizing
In this page:
selector {
box-sizing: content-box | border-box;
}
content-box को समझना (Default)
Default रूप से, browsers content-box model इस्तेमाल करके element widths calculate करते हैं। इसका मतलब है कि आप जो भी padding या borders जोड़ते हैं वे width के बाहर जुड़ते हैं, element को specified से physically wider बनाते हुए।
- padding वाले layouts के लिए content-box इस्तेमाल करने से बचें, क्योंकि यह element dimensions को overflow करा सकता है।
- Accepted values:
- content-box — width और height सिर्फ content पर लागू होती हैं (initial value)
- border-box — width और height में padding और border शामिल होते हैं
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Understanding content-box (Default)
<style>
.box {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}
</style>
<div class="box">Renders wider than 200px: padding and border add on top</div>
border-box को समझना
border-box model borders और padding को आपकी specified width के अंदर बैठने पर मजबूर करता है, content area को fit होने के लिए shrink करते हुए। यह element sizes calculate करना और साफ़ layouts बनाना कहीं ज़्यादा आसान बनाता है।
- अपने element widths को predictable और layout calculations को simple रखने के लिए border-box इस्तेमाल करें।
- Accepted values:
- content-box — width और height सिर्फ content पर लागू होती हैं (initial value)
- border-box — width और height में padding और border शामिल होते हैं
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Understanding border-box
<style>
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}
</style>
<div class="box">Stays exactly 200px: padding and border included</div>
border-box क्यों Preferred है
responsive grid layouts बनाते समय, columns आमतौर पर percentage widths में defined होते हैं (जैसे दो 50% columns)।
अगर आप default content-box model के तहत इन columns में padding या borders जोड़ते हैं, तो वे 100% से overflow होकर टूट जाएँगे। border-box model आपके column widths को exact और predictable रखकर इसे solve करता है।
उदाहरण: Why border-box is Preferred
<style>
.col {
box-sizing: border-box;
width: 50%;
padding: 10px;
background: lightblue;
border: 1px solid #666;
float: left;
}
</style>
<div class="col">50%</div>
<div class="col">50% — stays exact, doesn't overflow 100%</div>
Global Box Reset
अपनी stylesheets में हर एक class selector पर box-sizing: border-box लिखने से बचने के लिए, आप global reset selector (*) को pseudo-elements के साथ combine करके अपने page के सभी elements पर automatically border-box लागू कर सकते हैं।
उदाहरण: The Global Box Reset
<style>
*, *::before, *::after {
box-sizing: border-box;
}
</style>
<div style="width:200px; padding:20px; border:4px solid teal; background:#e0f2f1">border-box keeps this at 200px wide.</div>
Custom Grid Cards को Size करना
border-box इस्तेमाल करना तब ज़रूरी है जब आप borders और padding वाले complex card grids बना रहे हों, अपने columns को perfectly aligned रखते हुए और layout shifts रोकते हुए।
उदाहरण: Sizing Custom Grid Cards
<style>
.card {
box-sizing: border-box;
width: 33.33%;
padding: 16px;
border: 1px solid #ddd;
float: left;
}
</style>
<div class="card">Card</div>
<div class="card">Card</div>
<div class="card">Card</div>
- default content-box model इस्तेमाल करना, जिससे 100% width और padding वाले elements overflow हो जाते हैं।
- अपने global resets में pseudo-elements (::before और ::after) पर box-sizing लागू करना भूल जाना।
- border-box इस्तेमाल करने के बजाय अपनी stylesheets में हाथ से padding sizes घटाने की कोशिश करना।
- content-box model element widths को width के बाहर padding और borders जोड़कर calculate करता है, layouts को overflow कराते हुए।
- border-box model borders और padding को specified width के अंदर बैठने पर मजबूर करता है, layout sizing को predictable बनाते हुए।
- border-box को globally लागू करने के लिए अपनी stylesheets के बिल्कुल top पर एक global box reset (* { box-sizing: border-box; }) शामिल करें।
box-sizing property और border-box sizing हर modern web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline