← Back to CSS Course | Chapter 5: Layout & Display | Lesson 12 of 14

CSS Calc Deep Dive

आप पहले से जानते हैं कि calc() basic arithmetic कर सकता है, जैसे 100% में से किसी sidebar की width घटाना। यह chapter आगे जाता है, calc() को एक छोटे formula engine की तरह treat करते हुए जिस पर आप असली layout problems के लिए भरोसा कर सकते हैं। calc() को अपनी stylesheet में सीधे built एक calculator की तरह सोचें, एक ऐसा जो pixels, percentages, viewport units, और custom properties सबको एक साथ समझता है, और उन्हें एक ही expression में combine कर सकता है, आपको numbers हाथ से पहले से calculate करने पर मजबूर करने के बजाय। यह deep dive unit types को mix करने, calc() को दूसरे calc() calls के अंदर nest करने, और इसे custom properties के साथ जोड़कर एक flexible, centrally controlled sizing system बनाने पर focus करता है।
Syntax
css
selector {
  width: calc(value1 operator value2);  /* + - * / */
}

एक Expression में Unit Types Mix करना

calc() तब shine करता है जब ऐसे unit types combine किए जाएँ जो normally interact नहीं कर सकते, जैसे किसी percentage-based width में एक fixed pixel value जोड़ना, जिससे आप ऐसे layout rules express कर सकते हैं जिन्हें वरना JavaScript चाहिए होती।

Note: किसी calc() expression के हर term को पहले अपने pixel equivalent में resolve होते हुए सोचें, फिर combine होते हुए, यह mental model mixed-unit expressions को predict करना आसान बना देता है।
Warning: calc() में हर operator के दोनों तरफ़ whitespace होनी चाहिए, calc(100%-10px) invalid है और चुपचाप लागू होने में fail हो जाता है।

उदाहरण: Mixing Unit Types in One Expression

css
<style>
.box {
  width: calc(100% - 40px);
  background: lightblue;
  padding: 10px;
}
</style>
<div class="box">Percentage and pixels combined in one expression</div>

calc() Expressions को Nest करना

calc() expressions दूसरे calc() expressions को reference कर सकते हैं या parentheses में grouped sub-expressions रख सकते हैं, जो तब उपयोगी है जब कोई value कई derived intermediate calculations पर निर्भर करती है।

Note: जहाँ possible हो nested calc() को parentheses grouping वाले एक single expression में flatten करना prefer करें, यह एक-दूसरे को wrap करने वाले कई calc() calls से पढ़ना आसान है।
Warning: गहराई से nested calc() expressions को debug करना मुश्किल हो सकता है, अगर कोई layout गलत दिखे, तो faulty term को isolate करने के लिए expression को labeled custom properties में तोड़ें।

उदाहरण: Nesting calc() Expressions

css
<style>
.box {
  width: calc(calc(100% - 20px) / 2);
  background: lightblue;
  padding: 10px;
}
</style>
<div class="box">A calc() expression built from another calc()</div>

Responsive Calculations

calc() को viewport units और min/max width constraints के साथ जोड़ना fluid layouts बनाने देता है जो fixed media query thresholds पर अचानक jump करने के बजाय breakpoints में smoothly respond करते हैं।

Note: जब किसी responsive calculation को hard minimum और maximum bounds भी चाहिए हों तो calc() को clamp() के साथ combine करें, ये एक-दूसरे को अच्छी तरह complement करते हैं।
Warning: बिना किसी minimum के font sizes के लिए सिर्फ vw-based calc() पर भरोसा करना बहुत narrow devices पर text को unreadably छोटा बना सकता है।

उदाहरण: Responsive Calculations

css
<style>
h1 {
  font-size: calc(1rem + 2vw);
}
</style>
<h1>Scales smoothly across breakpoints</h1>

Custom Properties के साथ calc()

Custom properties calc() के साथ combine होने पर कहीं ज़्यादा powerful हो जाती हैं, जिससे एक single base value एक central source of truth से पूरी stylesheet में कई derived measurements drive कर सकती है।

Note: calc()-driving custom properties को descriptively नाम दें, जैसे --cc-base-unit, ताकि उनका purpose बाद में stylesheet पढ़ने वाले किसी भी व्यक्ति के लिए साफ़ रहे।
Warning: बिना किसी fallback वाली custom property जो resolve होने में fail हो जाती है, जैसे उसके नाम में एक typo, उसे इस्तेमाल करने वाले किसी भी calc() expression को पूरी तरह invalid बना देती है।

उदाहरण: calc() With Custom Properties

css
<style>
.wrapper {
  border: 2px dashed #999;
  padding: 10px;
}
:root {
  --cc-base-unit: 8px;
}
.box {
  margin: calc(var(--cc-base-unit) * 3);
  background: lightblue;
  padding: 10px;
}
</style>
<div class="wrapper">
  <div class="box">Derived from one central custom property</div>
</div>

calc() के साथ Practical Layout Patterns

calc() के common real-world इस्तेमालों में sticky footers, एक fixed header को account में लेने वाले full-height sections, और shared gutters को घटाने वाले equal-width columns शामिल हैं, ऐसे patterns जो production websites में बार-बार दिखते हैं।

Note: किसी fixed header की height को account में लेते समय, उस height को एक custom property में store करें ताकि header की CSS और उसे reference करने वाले किसी भी calc() expression दोनों sync में रहें।
Warning: calc(100vh - Npx) mobile browsers पर inconsistently behave कर सकता है जहाँ toolbars के दिखने या छिपने से viewport height बदलती है, असली mobile devices पर सावधानी से test करें।

उदाहरण: Practical Layout Patterns With calc()

css
<style>
:root {
  --header-height: 60px;
}
.content {
  min-height: calc(100vh - var(--header-height));
  background: lightblue;
}
</style>
<div class="content">Fills the screen minus the fixed header</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. बिना ज़रूरत calc() को nest करना, calc() expressions plain arithmetic sub-expressions को किसी दूसरे calc() में wrap किए बिना भी रख सकते हैं।
  2. किसी percentage को viewport unit के साथ mix करने की कोशिश करना और एक single simple conversion की उम्मीद करना, browser combine करने से पहले हर unit को उसके अपने reference के against resolve करता है, जो अगर दोनों references अलग हों तो unexpected results दे सकता है।
  3. stylesheet में कहीं और calc() के अंदर इस्तेमाल हो रही एक custom property को overwrite करना, जिससे उसे reference करने वाला हर calc() expression चुपचाप बदल जाता है।
चैप्टर सारांश
  • calc() एक ही expression में percentages, pixels, viewport units, और custom properties को freely mix कर सकता है।
  • calc() expressions को एक-दूसरे के अंदर nest किया जा सकता है, हालांकि उन्हें एक expression में flatten करना आमतौर पर ज़्यादा साफ़ होता है।
  • shared values को custom properties में store करना और उन्हें calc() के अंदर reference करना एक maintainable, centrally controlled sizing system बनाता है।
ब्राउज़र सपोर्ट

calc() हर modern browser में supported है, और nested calc() expressions व calc() के अंदर custom property references वहाँ हर जगह supported हैं जहाँ calc() खुद supported है।

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.