← Back to jQuery Course | Chapter 6: CSS & Dimensions | Lesson 4 of 6

Inner और Outer Dimensions

innerWidth() content और padding include करता है लेकिन border नहीं।
Syntax
javascript
$(selector).innerWidth();
$(selector).outerWidth(includeMargin);
$(selector).innerHeight();
$(selector).outerHeight(includeMargin);

innerWidth()

.innerWidth() content area plus left और right padding मिलाकर report करता है, border जुड़ने से पहले का box size देते हुए। यह width() (जो padding exclude करता है) और outerWidth() (जो border भी शामिल करता है) के बीच बैठता है।

उदाहरण: innerWidth()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="width:100px;padding:10px;border:5px solid black">Box</div>
    <script>
      console.log($("#box").innerWidth());
    </script>
  </body>
</html>

innerHeight()

innerHeight() content और padding include करता है लेकिन border नहीं, vertical direction में innerWidth() को mirror करते हुए। इसे तब इस्तेमाल करें जब आपको border की thickness की चिंता किए बिना किसी element का visible box size चाहिए।

उदाहरण: innerHeight()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="height:100px;padding:10px;border:5px solid black">Box</div>
    <script>
      console.log($("#box").innerHeight());
    </script>
  </body>
</html>

outerWidth()

outerWidth() content, padding, और borders include करता है, आपको page पर visually दिखने वाली element की पूरी rendered width देते हुए। यह वह number है जो आप तब चाहेंगे जब calculate करना हो कि कोई element असल में कितनी horizontal space occupy करता है।

उदाहरण: outerWidth()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="width:100px;padding:10px;border:5px solid black">Box</div>
    <script>
      console.log($("#box").outerWidth());
    </script>
  </body>
</html>

outerHeight()

outerHeight() content, padding, और borders include करता है, vertical dimension के लिए outerWidth() को match करते हुए। जब आप दूसरे elements को इसके पूरे visible footprint के relative position कर रहे हों तो यह सबसे accurate measurement है।

उदाहरण: outerHeight()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="height:100px;padding:10px;border:5px solid black">Box</div>
    <script>
      console.log($("#box").outerHeight());
    </script>
  </body>
</html>

Margins को Include करना

outerWidth() या outerHeight() को true pass करें ताकि measurement में element की margins भी शामिल हों। यह total layout space calculate करते समय ज़रूरी है, क्योंकि margins असर करते हैं कि कोई element अपने siblings के बीच कितनी जगह लेता है भले ही वे खुद box का हिस्सा न हों।

उदाहरण: Including Margins

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="width:100px;margin:20px">Box</div>
    <script>
      console.log($("#box").outerWidth(true));
    </script>
  </body>
</html>
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. .innerWidth() इस्तेमाल करना और border के शामिल होने की उम्मीद करना, जबकि सिर्फ padding content में जुड़ती है।
  2. यह भूल जाना कि margin शामिल करने के लिए .outerWidth(true) चाहिए, क्योंकि plain .outerWidth() इसे छोड़ देता है।
  3. layout calculate करते समय .width() और .outerWidth() को mix up करना, इसलिए sizes padding और border से off हो जाते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

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