← Back to CSS Course | Chapter 7: Layout Utilities | Lesson 5 of 5

CSS Relative Units

CSS Units पहले से em, rem, %, और vw/vh individually introduce कर चुका है — यह topic दो relative units को cover करता है जो छूट गईं, vmin और vmax, और पूरी relative-unit family के बीच चुनने पर direct, practical guidance देता है।
Syntax
css
selector {
  font-size: numberem;  /* also rem, %, vw, vh, ch */
}

vmin: Smaller Viewport Dimension

vmin viewport के जिस भी dimension को छोटा है उसका 1% represent करता है — width या height, किसी भी समय जो भी कम हो। यह उन elements को size करने के लिए उपयोगी बनाता है जिन्हें device portrait में हो या landscape में, दोनों में comfortably fit होना चाहिए।

Note:
  • Accepted values:
  • em — parent की font size के relative
  • rem — root font size के relative
  • % — parent के relative
  • vw | vh — viewport width या height का 1%
  • ch — 0 character की width
  • vmin | vmax — छोटा या बड़ा viewport side

उदाहरण: vmin: The Smaller Viewport Dimension

css
<style>
.box {
  width: 50vmin;
  height: 50vmin;
  background: steelblue;
  color: white;
}
</style>
<div class="box">Fits comfortably in portrait or landscape</div>

vmax: Larger Viewport Dimension

vmax vmin का mirror image है — viewport के जिस भी dimension को बड़ा है उसका 1%। Practice में इसे vmin से कहीं कम इस्तेमाल किया जाता है, लेकिन यह तब उपयोगी है जब आप specifically चाहते हैं कि कोई element screen के dominant, लंबे dimension के साथ scale हो।

Note:
  • Accepted values:
  • 1vmax — बड़े viewport dimension का 1%
  • 50vmax — बड़े dimension का आधा
  • 100vmax — पूरा बड़ा dimension
  • 1vmin — छोटे dimension का 1% (comparison के लिए)

उदाहरण: vmax: The Larger Viewport Dimension

css
<style>
.box {
  width: 50vmax;
  background: steelblue;
  color: white;
  padding: 10px;
}
</style>
<div class="box">Scales with the longer viewport dimension</div>

em के बजाय rem कब चुनें

em current element के (या उसके parent के) font-size के relative है, जिसका मतलब है कि em-based sizing वाले nested elements unpredictably compound होते हैं — bugs का एक common कारण।

rem हमेशा root html element के font-size के relative है, predictable, non-compounding sizing देते हुए, यही वजह है कि ज़्यादातर modern CSS spacing और typography के लिए rem को default बनाती है।

उदाहरण: When to Choose rem Over em

css
<style>
html { font-size: 16px; }
.a { font-size: 1.5em; }
.c { font-size: 1.5rem; }
</style>
<div class="a">em compounds when nested inside other em-sized elements</div>
<div class="c">rem always stays relative to the root</div>

Percentage कब viewport Units से ज़्यादा सही होती है

Percentage viewport के बजाय parent element के size के relative है, जो इसे उन components के लिए सही choice बनाती है जिन्हें पूरी screen के बजाय अपने container के साथ scale होना चाहिए — उदाहरण के लिए, किसी flexible layout के अंदर एक sidebar width, या एक image जिसे अपने wrapping div को भरना चाहिए।

उदाहरण: When Percentage Makes More Sense Than viewport Units

css
<style>
.sidebar {
  width: 25%;
  background: steelblue;
  color: white;
  padding: 10px;
}
</style>
<div style="display:flex; background:#eee;">
  <div class="sidebar">Scales with its parent, not the whole viewport</div>
</div>

Robust Sizing के लिए Relative Units Combine करना

Production CSS अक्सर जानबूझकर relative units को mix करती है: typography और spacing के लिए rem जिन्हें user font preferences का सम्मान करना चाहिए, container-relative sizing के लिए %, और उन hero sections के लिए कम मात्रा में vw/vmin जिन्हें genuinely viewport के साथ scale होना चाहिए।

हर जगह सिर्फ एक ही unit पर निर्भर रहना आमतौर पर हर situation के लिए सही unit चुनने से बुरे results देता है।

उदाहरण: Combining Relative Units for Robust Sizing

css
<style>
h1 {
  font-size: 2rem;
}
.container {
  width: 80%;
}
.hero {
  min-height: 40vmin;
  background: linear-gradient(135deg, #667eea, #764ba2);
}
</style>
<div class="container">
  <h1>rem, %, and vmin, each used for what it's best at</h1>
  <div class="hero"></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. em (parent की font size के relative) को rem (root की font size के relative) के साथ confuse करना, इसलिए nested em values compound हो जाती हैं।
  2. font sizes के लिए vw इस्तेमाल करना, जो text को बहुत छोटा या बड़ा बना सकता है और user zoom को नज़रअंदाज़ करता है।
  3. mobile पर 100vh इस्तेमाल करना और content को browser bars के पीछे hidden पाना।
चैप्टर सारांश
  • Website layout CSS से organize किया जाता है, और gap items के बीच space जोड़ता है।
  • CSS units sizes और lengths set करते हैं।
  • Absolute और relative units अलग-अलग तरीके से behave करते हैं।
ब्राउज़र सपोर्ट

हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।

🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.