132

I have an element and need it's width without(!) vertical scrollbar.

Firebug tells me body width is 1280px.

Either of these work fine in Firefox:

console.log($('.element').outerWidth() );
console.log($('.element').outerWidth(true) );

$detour = $('.child-of-element').offsetParent();
console.log( $detour.innerWidth() );

They all return 1263px, which is the value I'm looking for.

However all other browser give me 1280px.

Is there a cross browser way to get a fullscreen width without(!) vertical scrollbar?

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
frequent
  • 27,643
  • 59
  • 181
  • 333
  • Do you need the vertical scrollbars? Or could you use overflow: hidden and then calculate the width? – Filip Dec 01 '11 at 10:53
  • you can refer to this site for the your question http://stackoverflow.com/questions/8794338/how-to-get-the-height-and-width-of-the-browser-viewport-without-scrollbars-using –  Feb 21 '13 at 09:32
  • you could try `width: 100%;` – www139 Jul 05 '15 at 15:38
  • are you trying to make the element the entire width of the screen not including the scroll bars? – www139 Jul 05 '15 at 15:43
  • With CSS: `width: calc(100vw - (100vw - 100%));` from https://stackoverflow.com/a/34884924/1066234 – Avatar Mar 16 '21 at 09:22

8 Answers8

192

.prop("clientWidth") and .prop("scrollWidth")

var actualInnerWidth = $("body").prop("clientWidth"); // El. width minus scrollbar width
var actualInnerWidth = $("body").prop("scrollWidth"); // El. width minus scrollbar width

in JavaScript:

var actualInnerWidth = document.body.clientWidth;     // El. width minus scrollbar width
var actualInnerWidth = document.body.scrollWidth;     // El. width minus scrollbar width

P.S: Note that to use scrollWidth reliably your element should not overflow horizontally

jsBin demo


You could also use .innerWidth() but this will work only on the body element

var innerWidth = $('body').innerWidth(); // Width PX minus scrollbar 
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • 2
    This won't work if for some reason you changed the width of body element. – Konstantin Pereiaslov Aug 21 '13 at 04:41
  • @RokoC.Buljan I changed it in CSS as a part of the design, so I don't think that would be easy. Just letting everyone else, who may get the wrong results because of it know. – Konstantin Pereiaslov Aug 23 '13 at 14:25
  • 5
    It seems this doesn't truly work on Chrome any more, the reported width is actually 20px bigger than the window itself, I have tried numerous width measurements so far nothing. – Sammaye May 30 '14 at 08:27
  • 1
    @Sammaye Sorry to hear that, works in Chrome for me. – Roko C. Buljan May 30 '14 at 09:37
  • I thought it may have been version of chrome but I just updated and it still happens. The way I am testing is I have a media query at 768 and a innerWidth measurement at 768, the JS breaks at about 786 according to Chromes little size hover over on its pages. It might be implementation differences between JS width and media query width – Sammaye May 30 '14 at 09:45
  • @Sammaye ... It's hard to visualize without a demo... Can you setup a simple test case on jsBin? – Roko C. Buljan May 30 '14 at 11:07
  • Had it wrong way round, JS is smaller than media query when reporting but here is an example: http://jsbin.com/homixuqi/1/ , if you resize it to 780 it will show 764 in JS – Sammaye May 30 '14 at 11:23
  • 2
    @Sammaye that's cause you forgot (why?) to set margin:0; to BODY or use a common CSS reset code. http://jsbin.com/homixuqi/2/edit . **So again, the code works perfectly fine**. – Roko C. Buljan May 30 '14 at 12:50
  • Ok fair enough, I thought bootstrap 3.1 had reset code but it appears not – Sammaye May 30 '14 at 13:08
  • note that this method includes the **vertical scrollbar width** in the calculated width, if there is one (tested in Chrome). – The Merovingian Sep 22 '14 at 05:33
  • 1
    This kind of works, but not for height if you needed that. Also, this is not super reliable as the size of the body can be messed with using CSS. – Naman Goel Apr 14 '15 at 14:42
  • 2
    @NamanGoel it's your job as web designer to prevent messarounds with the body width. It's not a difficult task. Also, regarding the height it kind of depends if your `body` overflows or not. if it overflows than constrain it. – Roko C. Buljan Apr 14 '15 at 15:19
  • 3
    @RokoC.Buljan I'm not disagreeing with you here. Of course no good web designer/developer to should mess with the body width and stuff. My point was about using the most reliable API available. Some javascript developers may be building plug-ins etc and may not have control of the entire page. – Naman Goel Apr 14 '15 at 15:54
  • @Fabián the jsBin demo works pretty good (Windows Ch. 66.0.3359) – Roko C. Buljan May 03 '18 at 17:35
41

You can use vanilla javascript by simply writing:

var width = el.clientWidth;

You could also use this to get the width of the document as follows:

var docWidth = document.documentElement.clientWidth || document.body.clientWidth;

Source: MDN

You can also get the width of the full window, including the scrollbar, as follows:

var fullWidth = window.innerWidth;

However this is not supported by all browsers, so as a fallback, you may want to use docWidth as above, and add on the scrollbar width.

Source: MDN

Community
  • 1
  • 1
Joshua Bambrick
  • 2,669
  • 5
  • 27
  • 35
  • 1
    This does not take into account if there is a scrollbar – Jan Aug 22 '14 at 09:28
  • 6
    `document.documentElement.clientWidth` helped me the most, because it works for height as well (`innerHeight` can be smaller if the body doesn’t fill the page, etc.) and gets the value without the scrollbar. – Sebastian Simon Aug 11 '15 at 21:24
  • 1
    document.documentElement.clientWidth is the real winner. document.body.clientWidth is ok unless you have a min-width set on your body element and the browser is currently smaller than that minimum width. – Codemonkey Jun 22 '18 at 08:48
14

The safest place to get the correct width and height without the scrollbars is from the HTML element. Try this:

var width = document.documentElement.clientWidth
var height = document.documentElement.clientHeight

Browser support is pretty decent, with IE 9 and up supporting this. For OLD IE, use one of the many fallbacks mentioned here.

Naman Goel
  • 1,525
  • 11
  • 16
12

Here are some examples which assume $element is a jQuery element:

// Element width including overflow (scrollbar)
$element[0].offsetWidth; // 1280 in your case

// Element width excluding overflow (scrollbar)
$element[0].clientWidth; // 1280 - scrollbarWidth

// Scrollbar width
$element[0].offsetWidth - $element[0].clientWidth; // 0 if no scrollbar
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
10

Try this :

$('body, html').css('overflow', 'hidden');
var screenWidth1 = $(window).width();
$('body, html').css('overflow', 'visible');
var screenWidth2 = $(window).width();
alert(screenWidth1); // Gives the screenwith without scrollbar
alert(screenWidth2); // Gives the screenwith including scrollbar

You can get the screen width by with and without scroll bar by using this code. Here, I have changed the overflow value of body and get the width with and without scrollbar.

Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87
0

I experienced a similar problem and doing width:100%; solved it for me. I came to this solution after trying an answer in this question and realizing that the very nature of an <iframe> will make these javascript measurement tools inaccurate without using some complex function. Doing 100% is a simple way to take care of it in an iframe. I don't know about your issue since I'm not sure of what HTML elements you are manipulating.

www139
  • 4,960
  • 3
  • 31
  • 56
0

None of these solutions worked for me, however I was able to fix it by taking the width and subtracting the width of the scroll bar. I'm not sure how cross-browser compatible this is.

Community
  • 1
  • 1
homebrand
  • 1,143
  • 1
  • 9
  • 9
0

Here is what I use

function windowSizes(){
    var e = window,
        a = 'inner';
    if (!('innerWidth' in window)) {
        a = 'client';
        e = document.documentElement || document.body;
    }
    return {
        width: e[a + 'Width'],
        height: e[a + 'Height']
    };  
}

$(window).on('resize', function () {
    console.log( windowSizes().width,windowSizes().height );
});
Benn
  • 4,840
  • 8
  • 65
  • 106