Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:41:16+00:00 2026-05-26T12:41:16+00:00

I have a jQuery script, this sets an element’s width to 50% via the

  • 0

I have a jQuery script, this sets an element’s width to 50% via the normal CSS width attribute.

If I then output the outerWidth(true) of that element to get the exact pixel value, it sometimes does not correspond to $(window).width()/2 – this is based on the theme that the site has. Unfortunately I cannot bring a demo for this behavior online.

Is there any specific CSS attribute that might cause such a difference?

The problem is that the outerWidth() is reporting for example, 564px, but when I check in Safari inspector, it says 580. So the return of that function is incorrect and I’d like to be able to know what is the cause.

Demo:

http://users.telenet.be/prullen/this/

http://users.telenet.be/prullen/that/

The content (gray area when you click on the red tab) is supposed to be hidden on load via a negative margin, but due to the (incorrect?) return of jquery, it is not in one of these themes (showing slightly).

console.log calls are :

        console.log('window width / 2 = ' + $(window).width()/2);
        console.log('doc width /2  = ' + $(document).width()/2);

        console.log('width = ' + defaults.boxWidth);
        console.log('width = ' + $slider.css('width'));
        console.log('width = ' + $slider.width());
        console.log('width = ' + $slider.outerWidth());
        console.log('width = ' + $slider.outerWidth(true));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:41:17+00:00Added an answer on May 26, 2026 at 12:41 pm

    .outerWidth([includeMargin]) – Returns the width of the element, along with left and right padding, border, and (optionally) margin in pixels.


    Setting the [includeMargin] option to true adds the margins to this number:

    .outerWidth(true) – Returns the width of the element, along with left and right padding, border, and margin, in pixels.


    .width() – Returns a unit-less pixel value not including left and right padding, border, or margin.

    As per jQuery docs, the only difference between .width() and .css(width) is that the latter includes the units (‘px’) where width() simply returns the number (without the ‘px’).

    So if, by definition, width() does not include margins, padding, and borders, then neither can css(width)


    I have a jQuery script, this sets an element’s width to 50% via the
    normal CSS width attribute.

    This is setting the element itself (not including margin, padding or borders) to 50% of it’s container. If the window is the container, then the element’s width will be 50% of that.

    Example: With a window of 1160 pixels wide, your script will create an element at 50% of 1160, or 580 pixels wide (not including margin, padding or borders).

    If I then output the outerWidth(true) of that element to get the exact
    pixel value, it sometimes does not correspond to $(window).width()/2.

    When you look at the same element with outerWidth(true), now you’re adding margin, padding or borders to the number.

    Using my same example: With a window of 1160 pixels, $(window).width()/2 will be 580. Assuming your element is also 580 pixels, .outerWidth(true) will add (or subtract) margins, add border and add padding to the element itself so you are not going to get 580 unless margins, padding and border are all zero.

    So naturally, with a different “theme” (using your definition), the element is probably going to have a different padding, border and margins. I cannot say for sure since I cannot see any of your code.

    Simply use .width() in place of .outerWidth(true) to keep things consistent across all of your themes.


    EDIT:

    In the Safari console, I’m seeing a margin on the body causing what you describe. Inside your consolidated-screen-2.css sheet you have a 20 pixel left margin.

    You must take this body margin into account when you do your calculations.

    Since your JavaScript does not dynamically update and this is not in a jsFiddle, I cannot test it any further. When I set the margin to 0 in the DOM, I cannot invoke the JS to recalculate the numbers without reloading the page, which of course, wipes out my DOM over-ride.

    One solution would be to stick with .width() (ignore margins) in all your calculations.

    An alternate solution would seem to include setting the body margin to zero.


    EDIT 2:

    You keep saying it’s reporting incorrectly, but I have a semi-opaque pixel ruler utility overlaid on my screen for the following and everything is checking out…

    This page in a 1228 pixel wide window. Ruler verifies this.

    console.log('width = ' + $slider.outerWidth(true)); = 1188

    1188 plus 40 pixels (body left/right margins) = 1228 (same as ruler)

    Drawer is open and ruler verifies it is exactly 614 pixels wide (without the right border).

    console.log('width = ' + $slider.css('width')); = 594 px;

    594 plus 20 pixels (body left margin) = 614 (same as ruler)

    console.log('width = ' + $slider.width()); = 594

    594 plus 20 pixels (body left margin) = 614 (same as ruler)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jquery script which suppose to hide/show div element base on the
i have this jquery script that clicks on link add info then hides it
I have this at the top of my jQuery script: $().ajaxStart(function() { alert('ok'); });
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I have this html code <html> <head> <title>JQuery Problem 2</title> <script type=text/javascript src=jquery-1.4.min.js></script> <script
i have jquery accordion script like this.. <script type='text/javascript'> $(document).ready(function() { $('div.Syb> div').hide(); $('div.Syb>
I have this jquery script to call an external file. So far so good.
I have a jQuery script that basically is changing my css when I click
I have a jQuery search script that queries a PHP file then parses the
I have this jQuery script: <script type=text/javascript> $(document).ready(function{ $(#btnLogon).bind(click, function(){ $(#btnLogon).after('<span class=error>Please wait...</span>'); });

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.